Skip to content

Commit 5e48453

Browse files
authored
Normalizing Behavior of OTel Spans Created From Custom-Instrumentation and Trace Annotations (#9759)
* re-working otel spec * fixing muzzle * fixing tests * updating tests * fixing test
1 parent 522bbaf commit 5e48453

File tree

12 files changed

+56
-9
lines changed

12 files changed

+56
-9
lines changed

dd-java-agent/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/trace/OtelConventions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public final class OtelConventions {
3636
static final String SPAN_KIND_INTERNAL = "internal";
3737
static final String OPERATION_NAME_SPECIFIC_ATTRIBUTE = "operation.name";
38+
static final String SPAN_TYPE = "span.type";
3839
static final String ANALYTICS_EVENT_SPECIFIC_ATTRIBUTES = "analytics.event";
3940
static final String HTTP_RESPONSE_STATUS_CODE_ATTRIBUTE = "http.response.status_code";
4041

@@ -110,6 +111,9 @@ public static <T> boolean applyReservedAttribute(AgentSpan span, AttributeKey<T>
110111
} else if (ANALYTICS_EVENT_SPECIFIC_ATTRIBUTES.equals(name) && value instanceof String) {
111112
span.setMetric(ANALYTICS_SAMPLE_RATE, parseBoolean((String) value) ? 1 : 0);
112113
return true;
114+
} else if (SPAN_TYPE.equals(name) && value instanceof String) {
115+
span.setSpanType((CharSequence) value);
116+
return true;
113117
}
114118
case BOOLEAN:
115119
if (ANALYTICS_EVENT_SPECIFIC_ATTRIBUTES.equals(name) && value instanceof Boolean) {

dd-java-agent/instrumentation/guava-10.0/src/test/groovy/GuavaAsyncResultExtensionTest.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class GuavaAsyncResultExtensionTest extends InstrumentationSpecification {
4848
tags {
4949
defaultTags()
5050
"$Tags.COMPONENT" "opentelemetry"
51+
"$Tags.SPAN_KIND" "internal"
5152
}
5253
}
5354
}
@@ -74,6 +75,7 @@ class GuavaAsyncResultExtensionTest extends InstrumentationSpecification {
7475
tags {
7576
defaultTags()
7677
"$Tags.COMPONENT" "opentelemetry"
78+
"$Tags.SPAN_KIND" "internal"
7779
}
7880
}
7981
}
@@ -104,6 +106,7 @@ class GuavaAsyncResultExtensionTest extends InstrumentationSpecification {
104106
tags {
105107
defaultTags()
106108
"$Tags.COMPONENT" "opentelemetry"
109+
"$Tags.SPAN_KIND" "internal"
107110
errorTags(expectedException)
108111
}
109112
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
compileOnly group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: openTelemetryVersion
1919
compileOnly group: 'com.google.auto.value', name: 'auto-value-annotations', version: '1.6.6'
2020

21+
implementation project(':dd-java-agent:agent-otel:otel-shim')
2122
testImplementation group: 'io.opentelemetry', name: 'opentelemetry-api', version: openTelemetryVersion
2223
testImplementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: openTelemetryVersion
2324
latest1xDepTestImplementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: '1+'

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/src/latestDepTest/groovy/WithSpanAnnotationLatestDepTest.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class WithSpanAnnotationLatestDepTest extends WithSpanAnnotationTest {
2525
tags {
2626
defaultTags()
2727
"$Tags.COMPONENT" "opentelemetry"
28+
"$Tags.SPAN_KIND" "internal"
2829
}
2930
}
3031
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/src/main/java/datadog/trace/instrumentation/opentelemetry/annotations/WithSpanAnnotationInstrumentation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public String[] helperClassNames() {
4343
return new String[] {
4444
this.packageName + ".WithSpanDecorator",
4545
this.packageName + ".WithSpanDecorator$1", // Switch over enum generated class
46+
"datadog.opentelemetry.shim.trace.OtelConventions",
47+
"datadog.opentelemetry.shim.trace.OtelConventions$1",
4648
};
4749
}
4850

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/src/main/java/datadog/trace/instrumentation/opentelemetry/annotations/WithSpanDecorator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package datadog.trace.instrumentation.opentelemetry.annotations;
22

3+
import static datadog.opentelemetry.shim.trace.OtelConventions.toSpanKindTagValue;
34
import static datadog.trace.api.DDSpanTypes.HTTP_CLIENT;
45
import static datadog.trace.api.DDSpanTypes.HTTP_SERVER;
56
import static datadog.trace.api.DDSpanTypes.MESSAGE_CONSUMER;
67
import static datadog.trace.api.DDSpanTypes.MESSAGE_PRODUCER;
8+
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND;
79
import static java.lang.Math.min;
810

911
import datadog.trace.api.InstrumenterConfig;
@@ -55,12 +57,13 @@ protected CharSequence component() {
5557
public AgentSpan startMethodSpan(Method method) {
5658
CharSequence operationName = null;
5759
CharSequence spanType = null;
60+
SpanKind kind = null;
5861
boolean inheritContext = true;
5962

6063
WithSpan withSpanAnnotation = method.getAnnotation(WithSpan.class);
6164
if (withSpanAnnotation != null) {
6265
operationName = withSpanAnnotation.value();
63-
spanType = convertToSpanType(withSpanAnnotation.kind());
66+
kind = withSpanAnnotation.kind();
6467
if (INHERIT_CONTEXT_MH != null) {
6568
try {
6669
inheritContext = (boolean) INHERIT_CONTEXT_MH.invokeExact(withSpanAnnotation);
@@ -82,8 +85,9 @@ public AgentSpan startMethodSpan(Method method) {
8285
final AgentSpan span = spanBuilder.start();
8386
DECORATE.afterStart(span);
8487

85-
if (spanType != null) {
86-
span.setSpanType(spanType);
88+
if (kind != null) {
89+
span.setSpanType(convertToSpanType(kind));
90+
span.setTag(SPAN_KIND, toSpanKindTagValue(kind));
8791
}
8892
if (InstrumenterConfig.get().isMethodMeasured(method)) {
8993
span.setMeasured(true);

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/src/test/groovy/SpanAttributeAnnotationTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class SpanAttributeAnnotationTest extends InstrumentationSpecification {
2626
tags {
2727
defaultTags()
2828
"$Tags.COMPONENT" "opentelemetry"
29+
"$Tags.SPAN_KIND" "internal"
2930
"custom-tag" value
3031
}
3132
}
@@ -57,6 +58,7 @@ class SpanAttributeAnnotationTest extends InstrumentationSpecification {
5758
tags {
5859
defaultTags()
5960
"$Tags.COMPONENT" "opentelemetry"
61+
"$Tags.SPAN_KIND" "internal"
6062
"custom-tag1" "param1"
6163
"custom-tag2" "param2"
6264
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/src/test/groovy/WithSpanAnnotationTest.groovy

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
3030
tags {
3131
defaultTags()
3232
"$Tags.COMPONENT" "opentelemetry"
33+
"$Tags.SPAN_KIND" "internal"
3334
}
3435
}
3536
}
@@ -52,6 +53,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
5253
tags {
5354
defaultTags()
5455
"$Tags.COMPONENT" "opentelemetry"
56+
"$Tags.SPAN_KIND" "internal"
5557
}
5658
}
5759
}
@@ -75,18 +77,19 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
7577
tags {
7678
defaultTags()
7779
"$Tags.COMPONENT" "opentelemetry"
80+
"$Tags.SPAN_KIND" kindTag
7881
}
7982
}
8083
}
8184
}
8285

8386
where:
84-
kind | type
85-
'SERVER' | DDSpanTypes.HTTP_SERVER
86-
'CLIENT' | DDSpanTypes.HTTP_CLIENT
87-
'PRODUCER' | DDSpanTypes.MESSAGE_PRODUCER
88-
'CONSUMER' | DDSpanTypes.MESSAGE_CONSUMER
89-
'INTERNAL' | null
87+
kind | type | kindTag
88+
'SERVER' | DDSpanTypes.HTTP_SERVER | Tags.SPAN_KIND_SERVER
89+
'CLIENT' | DDSpanTypes.HTTP_CLIENT | Tags.SPAN_KIND_CLIENT
90+
'PRODUCER' | DDSpanTypes.MESSAGE_PRODUCER | Tags.SPAN_KIND_PRODUCER
91+
'CONSUMER' | DDSpanTypes.MESSAGE_CONSUMER | Tags.SPAN_KIND_CONSUMER
92+
'INTERNAL' | null | Tags.SPAN_KIND_INTERNAL
9093
kindName = kind.substring(0, 1) + kind.substring(1).toLowerCase()
9194
}
9295

@@ -109,6 +112,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
109112
tags {
110113
defaultTags()
111114
"$Tags.COMPONENT" "opentelemetry"
115+
"$Tags.SPAN_KIND" "internal"
112116
errorTags(error.class, error.getMessage())
113117
}
114118
}
@@ -129,6 +133,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
129133
tags {
130134
defaultTags()
131135
"$Tags.COMPONENT" "opentelemetry"
136+
"$Tags.SPAN_KIND" "internal"
132137
}
133138
}
134139
}
@@ -156,6 +161,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
156161
tags {
157162
defaultTags()
158163
"$Tags.COMPONENT" "opentelemetry"
164+
"$Tags.SPAN_KIND" "internal"
159165
}
160166
}
161167
}
@@ -186,6 +192,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
186192
tags {
187193
defaultTags()
188194
"$Tags.COMPONENT" "opentelemetry"
195+
"$Tags.SPAN_KIND" "internal"
189196
errorTags(expectedException)
190197
}
191198
}
@@ -214,6 +221,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
214221
tags {
215222
defaultTags()
216223
"$Tags.COMPONENT" "opentelemetry"
224+
"$Tags.SPAN_KIND" "internal"
217225
}
218226
}
219227
}
@@ -244,6 +252,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
244252
tags {
245253
defaultTags()
246254
"$Tags.COMPONENT" "opentelemetry"
255+
"$Tags.SPAN_KIND" "internal"
247256
errorTags(expectedException)
248257
}
249258
}
@@ -267,6 +276,7 @@ class WithSpanAnnotationTest extends InstrumentationSpecification {
267276
tags {
268277
defaultTags()
269278
"$Tags.COMPONENT" "opentelemetry"
279+
"$Tags.SPAN_KIND" "internal"
270280
}
271281
}
272282
}

dd-java-agent/instrumentation/reactive-streams/src/test/groovy/ReactiveStreamsAsyncResultExtensionTest.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ReactiveStreamsAsyncResultExtensionTest extends InstrumentationSpecificati
3333
tags {
3434
defaultTags()
3535
"$Tags.COMPONENT" "opentelemetry"
36+
"$Tags.SPAN_KIND" "internal"
3637
}
3738
}
3839
}
@@ -63,6 +64,7 @@ class ReactiveStreamsAsyncResultExtensionTest extends InstrumentationSpecificati
6364
tags {
6465
defaultTags()
6566
"$Tags.COMPONENT" "opentelemetry"
67+
"$Tags.SPAN_KIND" "internal"
6668
errorTags(expectedException)
6769
}
6870
}
@@ -92,6 +94,7 @@ class ReactiveStreamsAsyncResultExtensionTest extends InstrumentationSpecificati
9294
tags {
9395
defaultTags()
9496
"$Tags.COMPONENT" "opentelemetry"
97+
"$Tags.SPAN_KIND" "internal"
9598
}
9699
}
97100
}
@@ -121,6 +124,7 @@ class ReactiveStreamsAsyncResultExtensionTest extends InstrumentationSpecificati
121124
tags {
122125
defaultTags()
123126
"$Tags.COMPONENT" "opentelemetry"
127+
"$Tags.SPAN_KIND" "internal"
124128
}
125129
}
126130
span {
@@ -130,6 +134,7 @@ class ReactiveStreamsAsyncResultExtensionTest extends InstrumentationSpecificati
130134
tags {
131135
defaultTags()
132136
"$Tags.COMPONENT" "opentelemetry"
137+
"$Tags.SPAN_KIND" "internal"
133138
}
134139
}
135140
}

dd-java-agent/instrumentation/reactor-core-3.1/src/latestDepTest/groovy/ReactorAsyncResultExtensionTest.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ReactorAsyncResultExtensionTest extends InstrumentationSpecification {
4949
tags {
5050
defaultTags()
5151
"$Tags.COMPONENT" "opentelemetry"
52+
"$Tags.SPAN_KIND" "internal"
5253
}
5354
}
5455
}
@@ -79,6 +80,7 @@ class ReactorAsyncResultExtensionTest extends InstrumentationSpecification {
7980
tags {
8081
defaultTags()
8182
"$Tags.COMPONENT" "opentelemetry"
83+
"$Tags.SPAN_KIND" "internal"
8284
errorTags(expectedException)
8385
}
8486
}
@@ -107,6 +109,7 @@ class ReactorAsyncResultExtensionTest extends InstrumentationSpecification {
107109
tags {
108110
defaultTags()
109111
"$Tags.COMPONENT" "opentelemetry"
112+
"$Tags.SPAN_KIND" "internal"
110113
}
111114
}
112115
}
@@ -134,6 +137,7 @@ class ReactorAsyncResultExtensionTest extends InstrumentationSpecification {
134137
tags {
135138
defaultTags()
136139
"$Tags.COMPONENT" "opentelemetry"
140+
"$Tags.SPAN_KIND" "internal"
137141
}
138142
}
139143
}
@@ -164,6 +168,7 @@ class ReactorAsyncResultExtensionTest extends InstrumentationSpecification {
164168
tags {
165169
defaultTags()
166170
"$Tags.COMPONENT" "opentelemetry"
171+
"$Tags.SPAN_KIND" "internal"
167172
errorTags(expectedException)
168173
}
169174
}
@@ -192,6 +197,7 @@ class ReactorAsyncResultExtensionTest extends InstrumentationSpecification {
192197
tags {
193198
defaultTags()
194199
"$Tags.COMPONENT" "opentelemetry"
200+
"$Tags.SPAN_KIND" "internal"
195201
}
196202
}
197203
}

0 commit comments

Comments
 (0)