1
1
package datadog .trace .instrumentation .opentelemetry14 .trace ;
2
2
3
3
import static datadog .trace .api .DDTags .ANALYTICS_SAMPLE_RATE ;
4
+ import static datadog .trace .bootstrap .instrumentation .api .Tags .SPAN_KIND ;
4
5
import static datadog .trace .bootstrap .instrumentation .api .Tags .SPAN_KIND_CLIENT ;
5
6
import static datadog .trace .bootstrap .instrumentation .api .Tags .SPAN_KIND_CONSUMER ;
6
7
import static datadog .trace .bootstrap .instrumentation .api .Tags .SPAN_KIND_PRODUCER ;
13
14
import static java .util .Locale .ROOT ;
14
15
15
16
import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
17
+ import datadog .trace .bootstrap .instrumentation .api .Tags ;
16
18
import io .opentelemetry .api .trace .SpanKind ;
17
19
import javax .annotation .Nullable ;
18
20
import org .slf4j .Logger ;
@@ -22,20 +24,17 @@ public final class OtelConventions {
22
24
static final String SPAN_KIND_INTERNAL = "internal" ;
23
25
private static final Logger LOGGER = LoggerFactory .getLogger (OtelConventions .class );
24
26
private static final String OPERATION_NAME_SPECIFIC_ATTRIBUTE = "operation.name" ;
25
- private static final String SERVICE_NAME_SPECIFIC_ATTRIBUTE = "service.name" ;
26
- private static final String RESOURCE_NAME_SPECIFIC_ATTRIBUTE = "resource.name" ;
27
- private static final String SPAN_TYPE_SPECIFIC_ATTRIBUTES = "span.type" ;
28
27
private static final String ANALYTICS_EVENT_SPECIFIC_ATTRIBUTES = "analytics.event" ;
29
28
30
29
private OtelConventions () {}
31
30
32
31
/**
33
- * Convert OpenTelemetry {@link SpanKind} to Datadog span type .
32
+ * Convert OpenTelemetry {@link SpanKind} to {@link Tags#SPAN_KIND} value .
34
33
*
35
34
* @param spanKind The OpenTelemetry span kind to convert.
36
- * @return The related Datadog span type .
35
+ * @return The {@link Tags#SPAN_KIND} value .
37
36
*/
38
- public static String toSpanType (SpanKind spanKind ) {
37
+ public static String toSpanKindTagValue (SpanKind spanKind ) {
39
38
switch (spanKind ) {
40
39
case CLIENT :
41
40
return SPAN_KIND_CLIENT ;
@@ -53,16 +52,16 @@ public static String toSpanType(SpanKind spanKind) {
53
52
}
54
53
55
54
/**
56
- * Convert Datadog span type to OpenTelemetry {@link SpanKind}.
55
+ * Convert {@link Tags#SPAN_KIND} value to OpenTelemetry {@link SpanKind}.
57
56
*
58
- * @param spanType The span type to convert.
57
+ * @param spanKind The {@link Tags#SPAN_KIND} value to convert.
59
58
* @return The related OpenTelemetry {@link SpanKind}.
60
59
*/
61
- public static SpanKind toSpanKind (String spanType ) {
62
- if (spanType == null ) {
60
+ public static SpanKind toOtelSpanKind (String spanKind ) {
61
+ if (spanKind == null ) {
63
62
return INTERNAL ;
64
63
}
65
- switch (spanType ) {
64
+ switch (spanKind ) {
66
65
case SPAN_KIND_CLIENT :
67
66
return CLIENT ;
68
67
case SPAN_KIND_SERVER :
@@ -77,26 +76,17 @@ public static SpanKind toSpanKind(String spanType) {
77
76
}
78
77
79
78
public static void applyConventions (AgentSpan span ) {
80
- String serviceName = getStringAttribute (span , SERVICE_NAME_SPECIFIC_ATTRIBUTE );
81
- if (serviceName != null ) {
82
- span .setServiceName (serviceName );
83
- }
84
- String resourceName = getStringAttribute (span , RESOURCE_NAME_SPECIFIC_ATTRIBUTE );
85
- if (resourceName != null ) {
86
- span .setResourceName (resourceName );
87
- }
88
- String spanType = getStringAttribute (span , SPAN_TYPE_SPECIFIC_ATTRIBUTES );
89
- if (spanType != null ) {
90
- span .setSpanType (spanType );
91
- }
79
+ // The following attributes are already handled by tag interceptors:
80
+ // - service.name
81
+ // - resource.name
82
+ // - span.type
83
+ // TODO Remaining questions:
84
+ // - Do the attributes must be added has tag or intercepted?
85
+ // - Does operation name method blocked if operation.name is present?
92
86
Boolean analyticsEvent = getBooleanAttribute (span , ANALYTICS_EVENT_SPECIFIC_ATTRIBUTES );
93
87
if (analyticsEvent != null ) {
94
88
span .setMetric (ANALYTICS_SAMPLE_RATE , analyticsEvent ? 1 : 0 );
95
89
}
96
- applyOperationName (span );
97
- }
98
-
99
- private static void applyOperationName (AgentSpan span ) {
100
90
String operationName = getStringAttribute (span , OPERATION_NAME_SPECIFIC_ATTRIBUTE );
101
91
if (operationName == null ) {
102
92
operationName = computeOperationName (span );
@@ -105,7 +95,9 @@ private static void applyOperationName(AgentSpan span) {
105
95
}
106
96
107
97
private static String computeOperationName (AgentSpan span ) {
108
- SpanKind spanKind = toSpanKind (span .getSpanType ());
98
+ Object spanKingTag = span .getTag (SPAN_KIND );
99
+ SpanKind spanKind =
100
+ spanKingTag instanceof String ? toOtelSpanKind ((String ) spanKingTag ) : INTERNAL ;
109
101
/*
110
102
* HTTP convention: https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/http/
111
103
*/
0 commit comments