Skip to content

Commit ef7020c

Browse files
committed
Do complex logic only when the FF is enabled
1 parent 40545c1 commit ef7020c

File tree

2 files changed

+120
-12
lines changed

2 files changed

+120
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package datadog.trace.api.naming;
2+
3+
import static java.util.concurrent.TimeUnit.MICROSECONDS;
4+
import static java.util.concurrent.TimeUnit.SECONDS;
5+
6+
import datadog.trace.api.Config;
7+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
8+
import datadog.trace.core.BlackholeWriter;
9+
import datadog.trace.core.CoreTracer;
10+
import datadog.trace.core.TraceCounters;
11+
import java.util.WeakHashMap;
12+
import java.util.function.Supplier;
13+
import org.openjdk.jmh.annotations.Benchmark;
14+
import org.openjdk.jmh.annotations.BenchmarkMode;
15+
import org.openjdk.jmh.annotations.Fork;
16+
import org.openjdk.jmh.annotations.Level;
17+
import org.openjdk.jmh.annotations.Measurement;
18+
import org.openjdk.jmh.annotations.Mode;
19+
import org.openjdk.jmh.annotations.OutputTimeUnit;
20+
import org.openjdk.jmh.annotations.Param;
21+
import org.openjdk.jmh.annotations.Scope;
22+
import org.openjdk.jmh.annotations.Setup;
23+
import org.openjdk.jmh.annotations.State;
24+
import org.openjdk.jmh.annotations.Warmup;
25+
import org.openjdk.jmh.infra.Blackhole;
26+
27+
/**
28+
* Benchmark (pinThreadServiceName) Mode Cnt Score Error Units
29+
* MessagingNamingBenchmark.complexSupplierServiceName false avgt 3 0.710 ± 0.040 us/op
30+
* MessagingNamingBenchmark.complexSupplierServiceName true avgt 3 0.718 ± 0.105 us/op
31+
* MessagingNamingBenchmark.constantServiceName false avgt 3 0.668 ± 0.024 us/op
32+
* MessagingNamingBenchmark.constantSupplierServiceName false avgt 3 0.666 ± 0.044 us/op
33+
*/
34+
@State(Scope.Benchmark)
35+
@Warmup(iterations = 1, time = 15, timeUnit = SECONDS)
36+
@Measurement(iterations = 3, time = 30, timeUnit = SECONDS)
37+
@BenchmarkMode(Mode.AverageTime)
38+
@OutputTimeUnit(MICROSECONDS)
39+
@Fork(value = 1)
40+
public class MessagingNamingBenchmark {
41+
42+
CoreTracer tracer;
43+
44+
WeakHashMap<ClassLoader, String> weakCache;
45+
46+
private static final Supplier<String> constantSupplier = () -> "constant";
47+
48+
private final Supplier<String> complexSupplier =
49+
() -> {
50+
String ret = weakCache.get(Thread.currentThread().getContextClassLoader());
51+
if (ret == null) {
52+
ret = Config.get().getServiceName();
53+
}
54+
return ret;
55+
};
56+
57+
@Param({"false", "true"})
58+
boolean pinThreadServiceName;
59+
60+
@Setup(Level.Iteration)
61+
public void init(Blackhole blackhole) {
62+
tracer =
63+
CoreTracer.builder()
64+
.writer(new BlackholeWriter(blackhole, new TraceCounters(), 0))
65+
.strictTraceWrites(false)
66+
.build();
67+
weakCache = new WeakHashMap<>();
68+
if (pinThreadServiceName) {
69+
weakCache.put(Thread.currentThread().getContextClassLoader(), constantSupplier.get());
70+
}
71+
}
72+
73+
@Benchmark
74+
public void constantServiceName(Blackhole blackhole) {
75+
final AgentSpan span = tracer.startSpan("", "");
76+
span.setServiceName("constant");
77+
span.finish();
78+
blackhole.consume(span);
79+
}
80+
81+
@Benchmark
82+
public void constantSupplierServiceName(Blackhole blackhole) {
83+
final AgentSpan span = tracer.startSpan("", "");
84+
span.setServiceName(constantSupplier.get());
85+
span.finish();
86+
blackhole.consume(span);
87+
}
88+
89+
@Benchmark
90+
public void complexSupplierServiceName(Blackhole blackhole) {
91+
final AgentSpan span = tracer.startSpan("", "");
92+
span.setServiceName(complexSupplier.get());
93+
span.finish();
94+
blackhole.consume(span);
95+
}
96+
}

internal-api/src/main/java/datadog/trace/api/naming/v0/MessagingNamingV0.java

+24-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@
88
import javax.annotation.Nonnull;
99

1010
class MessagingNamingV0 implements NamingSchema.ForMessaging {
11+
12+
private static class ClassloaderDependentNamingSupplier implements Supplier<String> {
13+
private static final ClassloaderDependentNamingSupplier INSTANCE =
14+
new ClassloaderDependentNamingSupplier();
15+
private final String configServiceName = Config.get().getServiceName();
16+
17+
@Override
18+
public String get() {
19+
final String contextual = ClassloaderServiceNames.maybeGetForCurrentThread();
20+
if (contextual != null) {
21+
ServiceNameCollector.get().addService(contextual);
22+
return contextual;
23+
}
24+
return configServiceName;
25+
}
26+
}
27+
1128
private final boolean allowInferredServices;
1229

1330
public MessagingNamingV0(final boolean allowInferredServices) {
@@ -48,20 +65,15 @@ public Supplier<String> inboundService(
4865
if (allowInferredServices) {
4966
if (useLegacyTracing) {
5067
ServiceNameCollector.get().addService(messagingSystem);
51-
return () -> messagingSystem;
52-
} else {
53-
return () -> {
54-
final String contextual = ClassloaderServiceNames.maybeGetForCurrentThread();
55-
if (contextual != null) {
56-
ServiceNameCollector.get().addService(contextual);
57-
return contextual;
58-
}
59-
return Config.get().getServiceName();
60-
};
68+
return messagingSystem::toString;
69+
} else if (Config.get().isJeeSplitByDeployment()) {
70+
// in this particular case we're narrowing the service name from the context classloader.
71+
// this is more expensive so we're doing only if that feature is enabled.
72+
return ClassloaderDependentNamingSupplier.INSTANCE;
6173
}
62-
} else {
63-
return () -> null;
74+
return Config.get()::getServiceName;
6475
}
76+
return null;
6577
}
6678

6779
@Override

0 commit comments

Comments
 (0)