Skip to content

Commit

Permalink
Do complex logic only when the FF is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
amarziali committed Dec 16, 2024
1 parent 40545c1 commit ef7020c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package datadog.trace.api.naming;

import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import datadog.trace.api.Config;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.core.BlackholeWriter;
import datadog.trace.core.CoreTracer;
import datadog.trace.core.TraceCounters;
import java.util.WeakHashMap;
import java.util.function.Supplier;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

/**
* Benchmark (pinThreadServiceName) Mode Cnt Score Error Units
* MessagingNamingBenchmark.complexSupplierServiceName false avgt 3 0.710 ± 0.040 us/op
* MessagingNamingBenchmark.complexSupplierServiceName true avgt 3 0.718 ± 0.105 us/op
* MessagingNamingBenchmark.constantServiceName false avgt 3 0.668 ± 0.024 us/op
* MessagingNamingBenchmark.constantSupplierServiceName false avgt 3 0.666 ± 0.044 us/op
*/
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 15, timeUnit = SECONDS)
@Measurement(iterations = 3, time = 30, timeUnit = SECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MICROSECONDS)
@Fork(value = 1)
public class MessagingNamingBenchmark {

CoreTracer tracer;

WeakHashMap<ClassLoader, String> weakCache;

private static final Supplier<String> constantSupplier = () -> "constant";

private final Supplier<String> complexSupplier =
() -> {
String ret = weakCache.get(Thread.currentThread().getContextClassLoader());
if (ret == null) {
ret = Config.get().getServiceName();
}
return ret;
};

@Param({"false", "true"})
boolean pinThreadServiceName;

@Setup(Level.Iteration)
public void init(Blackhole blackhole) {
tracer =
CoreTracer.builder()
.writer(new BlackholeWriter(blackhole, new TraceCounters(), 0))
.strictTraceWrites(false)
.build();
weakCache = new WeakHashMap<>();
if (pinThreadServiceName) {
weakCache.put(Thread.currentThread().getContextClassLoader(), constantSupplier.get());
}
}

@Benchmark
public void constantServiceName(Blackhole blackhole) {
final AgentSpan span = tracer.startSpan("", "");
span.setServiceName("constant");
span.finish();
blackhole.consume(span);
}

@Benchmark
public void constantSupplierServiceName(Blackhole blackhole) {
final AgentSpan span = tracer.startSpan("", "");
span.setServiceName(constantSupplier.get());
span.finish();
blackhole.consume(span);
}

@Benchmark
public void complexSupplierServiceName(Blackhole blackhole) {
final AgentSpan span = tracer.startSpan("", "");
span.setServiceName(complexSupplier.get());
span.finish();
blackhole.consume(span);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
import javax.annotation.Nonnull;

class MessagingNamingV0 implements NamingSchema.ForMessaging {

private static class ClassloaderDependentNamingSupplier implements Supplier<String> {
private static final ClassloaderDependentNamingSupplier INSTANCE =
new ClassloaderDependentNamingSupplier();
private final String configServiceName = Config.get().getServiceName();

@Override
public String get() {
final String contextual = ClassloaderServiceNames.maybeGetForCurrentThread();
if (contextual != null) {
ServiceNameCollector.get().addService(contextual);
return contextual;
}
return configServiceName;
}
}

private final boolean allowInferredServices;

public MessagingNamingV0(final boolean allowInferredServices) {
Expand Down Expand Up @@ -48,20 +65,15 @@ public Supplier<String> inboundService(
if (allowInferredServices) {
if (useLegacyTracing) {
ServiceNameCollector.get().addService(messagingSystem);
return () -> messagingSystem;
} else {
return () -> {
final String contextual = ClassloaderServiceNames.maybeGetForCurrentThread();
if (contextual != null) {
ServiceNameCollector.get().addService(contextual);
return contextual;
}
return Config.get().getServiceName();
};
return messagingSystem::toString;
} else if (Config.get().isJeeSplitByDeployment()) {
// in this particular case we're narrowing the service name from the context classloader.
// this is more expensive so we're doing only if that feature is enabled.
return ClassloaderDependentNamingSupplier.INSTANCE;
}
} else {
return () -> null;
return Config.get()::getServiceName;
}
return null;
}

@Override
Expand Down

0 comments on commit ef7020c

Please sign in to comment.