Skip to content

Commit e55536f

Browse files
authored
Merge branch 'master' into dougqh/client-kind-entry-reuse
2 parents 7dd81c0 + 8fbdc86 commit e55536f

6 files changed

Lines changed: 50 additions & 15 deletions

File tree

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/BaseDecorator.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import datadog.trace.api.Config;
88
import datadog.trace.api.DDTags;
99
import datadog.trace.api.Functions;
10+
import datadog.trace.api.TagMap;
1011
import datadog.trace.api.cache.QualifiedClassNameCache;
1112
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1213
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
@@ -40,17 +41,29 @@ public String apply(Class<?> clazz) {
4041
Functions.PrefixJoin.of("."));
4142

4243
protected final boolean traceAnalyticsEnabled;
43-
protected final Double traceAnalyticsSampleRate;
44+
protected final double traceAnalyticsSampleRate;
45+
46+
private final TagMap.Entry traceAnalyticsEntry;
47+
48+
// Deliberately not volatile, reading null and repeating the calculation is safe
49+
private TagMap.Entry cachedComponentEntry = null;
4450

4551
protected BaseDecorator() {
4652
final Config config = Config.get();
4753
final String[] instrumentationNames = instrumentationNames();
54+
4855
this.traceAnalyticsEnabled =
4956
instrumentationNames.length > 0
5057
&& config.isTraceAnalyticsIntegrationEnabled(
5158
traceAnalyticsDefault(), instrumentationNames);
59+
5260
this.traceAnalyticsSampleRate =
5361
(double) config.getInstrumentationAnalyticsSampleRate(instrumentationNames);
62+
63+
this.traceAnalyticsEntry =
64+
this.traceAnalyticsEnabled
65+
? TagMap.Entry.create(DDTags.ANALYTICS_SAMPLE_RATE, traceAnalyticsSampleRate)
66+
: null;
5467
}
5568

5669
protected abstract String[] instrumentationNames();
@@ -59,6 +72,20 @@ protected BaseDecorator() {
5972

6073
protected abstract CharSequence component();
6174

75+
/** Caches the component TagMap.Entry, so it isn't recreated for every trace */
76+
protected final TagMap.Entry componentEntry() {
77+
// DQH = Tried calling component() in the constructor, but that had issues with static
78+
// field ordering. That was caught be an integration test, but I didn't want to risk
79+
// breaking other integrations where the test is not as thorough.
80+
81+
// This approach while more complicated doesn't have any field initialization ordering issues.
82+
TagMap.Entry componentEntry = cachedComponentEntry;
83+
if (componentEntry == null) {
84+
cachedComponentEntry = componentEntry = TagMap.Entry.create(Tags.COMPONENT, component());
85+
}
86+
return componentEntry;
87+
}
88+
6289
protected boolean traceAnalyticsDefault() {
6390
return false;
6491
}
@@ -67,12 +94,17 @@ public AgentSpan afterStart(final AgentSpan span) {
6794
if (spanType() != null) {
6895
span.setSpanType(spanType());
6996
}
97+
98+
span.setTag(componentEntry());
99+
100+
// DQH - Could retrieve the value from componentEntry and cast to avoid the virtual call,
101+
// unclear which option is better here
70102
final CharSequence component = component();
71-
span.setTag(Tags.COMPONENT, component);
72103
span.context().setIntegrationName(component);
73-
if (traceAnalyticsEnabled) {
74-
span.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, traceAnalyticsSampleRate);
75-
}
104+
105+
// null handled by setMetric
106+
span.setMetric(traceAnalyticsEntry);
107+
76108
return span;
77109
}
78110

dd-java-agent/agent-bootstrap/src/test/groovy/datadog/trace/bootstrap/instrumentation/decorator/BaseDecoratorTest.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.trace.bootstrap.instrumentation.decorator
22

3-
3+
import datadog.trace.api.TagMap
44
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
55
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext
66
import datadog.trace.bootstrap.instrumentation.api.ErrorPriorities
@@ -25,12 +25,13 @@ class BaseDecoratorTest extends DDSpecification {
2525

2626
then:
2727
1 * span.setSpanType(decorator.spanType())
28-
1 * span.setTag(Tags.COMPONENT, "test-component")
28+
1 * span.setTag(TagMap.Entry.create(Tags.COMPONENT, "test-component"))
2929
1 * span.context() >> spanContext
3030
1 * spanContext.setIntegrationName("test-component")
3131
_ * span.setTag(_)
3232
_ * span.setTag(_, _) // Want to allow other calls from child implementations.
3333
_ * span.setMeasured(true)
34+
_ * span.setMetric(_)
3435
_ * span.setMetric(_, _)
3536
_ * span.setServiceName(_, _)
3637
_ * span.setOperationName(_)

dd-java-agent/agent-bootstrap/src/test/groovy/datadog/trace/bootstrap/instrumentation/decorator/ClientDecoratorTest.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ class ClientDecoratorTest extends BaseDecoratorTest {
2323
1 * span.setServiceName(serviceName, "test-component")
2424
}
2525
1 * span.setMeasured(true)
26-
1 * span.setTag(Tags.COMPONENT, "test-component")
26+
1 * span.setTag(TagMap.Entry.create(Tags.COMPONENT, "test-component"))
2727
1 * span.context() >> spanContext
2828
1 * spanContext.setIntegrationName("test-component")
2929
1 * span.setTag(TagMap.Entry.create(Tags.SPAN_KIND, "client"))
3030
1 * span.setSpanType(decorator.spanType())
31-
1 * span.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, 1.0)
31+
1 * span.setMetric(TagMap.Entry.create(DDTags.ANALYTICS_SAMPLE_RATE, 1.0))
32+
_ * span.setTag(_)
3233
_ * span.setTag(_, _) // Want to allow other calls from child implementations.
3334
_ * span.setTag(_)
3435
_ * span.setServiceName(_)

dd-java-agent/agent-bootstrap/src/test/groovy/datadog/trace/bootstrap/instrumentation/decorator/DatabaseClientDecoratorTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class DatabaseClientDecoratorTest extends ClientDecoratorTest {
2727
1 * span.setServiceName(serviceName, "test-component")
2828
}
2929
1 * span.setMeasured(true)
30-
1 * span.setTag(Tags.COMPONENT, "test-component")
30+
1 * span.setTag(TagMap.Entry.create(Tags.COMPONENT, "test-component"))
3131
1 * span.context() >> spanContext
3232
1 * spanContext.setIntegrationName("test-component")
3333
1 * span.setTag(TagMap.Entry.create(Tags.SPAN_KIND, "client"))
3434
1 * span.setSpanType("test-type")
35-
1 * span.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, 1.0)
35+
1 * span.setMetric(TagMap.Entry.create(DDTags.ANALYTICS_SAMPLE_RATE, 1.0))
3636
0 * _
3737

3838
where:

dd-java-agent/agent-bootstrap/src/test/groovy/datadog/trace/bootstrap/instrumentation/decorator/ServerDecoratorTest.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.trace.bootstrap.instrumentation.decorator
22

3-
3+
import datadog.trace.api.TagMap
44
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
55
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext
66

@@ -23,13 +23,15 @@ class ServerDecoratorTest extends BaseDecoratorTest {
2323

2424
then:
2525
1 * span.setTag(LANGUAGE_TAG_KEY, LANGUAGE_TAG_VALUE)
26-
1 * span.setTag(COMPONENT, "test-component")
26+
1 * span.setTag(TagMap.Entry.create(COMPONENT, "test-component"))
2727
1 * span.context() >> spanContext
2828
1 * spanContext.setIntegrationName("test-component")
2929
1 * span.setTag(SPAN_KIND, "server")
3030
1 * span.setSpanType(decorator.spanType())
3131
if (decorator.traceAnalyticsEnabled) {
32-
1 * span.setMetric(ANALYTICS_SAMPLE_RATE, 1.0)
32+
1 * span.setMetric(TagMap.Entry.create(ANALYTICS_SAMPLE_RATE, 1.0))
33+
} else {
34+
1 * span.setMetric(null)
3335
}
3436
0 * _
3537
}

internal-api/src/test/java/datadog/trace/api/TagMapEntryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ public void createDouble(double value) {
497497
}
498498

499499
@ParameterizedTest
500-
@DisplayName("newDoubleEntry: double")
501500
@ValueSource(
502501
doubles = {Double.MIN_VALUE, Float.MIN_VALUE, -1D, 0D, 1D, Math.E, Math.PI, Double.MAX_VALUE})
503502
public void newDoubleEntry(double value) {

0 commit comments

Comments
 (0)