Skip to content

Commit cddd78f

Browse files
committed
initial otel dropin support
1 parent ffe33cd commit cddd78f

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

dd-java-agent/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/context/OtelContext.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
66
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
77
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
8+
import datadog.trace.bootstrap.instrumentation.api.BaggageContext;
89
import io.opentelemetry.api.trace.Span;
910
import io.opentelemetry.context.Context;
1011
import io.opentelemetry.context.ContextKey;
@@ -22,22 +23,29 @@ public class OtelContext implements Context {
2223

2324
private static final String OTEL_CONTEXT_SPAN_KEY = "opentelemetry-trace-span-key";
2425
private static final String OTEL_CONTEXT_ROOT_SPAN_KEY = "opentelemetry-traces-local-root-span";
26+
private static final String OTEL_CONTEXT_BAGGAGE_KEY = "opentelemetry-baggage";
2527

2628
/** Keep track of propagated context that has not been captured on the scope stack. */
2729
private static final ThreadLocal<OtelContext> lastPropagated = new ThreadLocal<>();
2830

2931
private final Span currentSpan;
3032
private final Span rootSpan;
33+
private final BaggageContext baggageContext;
3134

3235
private final Object[] entries;
3336

3437
public OtelContext(Span currentSpan, Span rootSpan) {
35-
this(currentSpan, rootSpan, NO_ENTRIES);
38+
this(currentSpan, rootSpan, null, NO_ENTRIES);
3639
}
3740

38-
public OtelContext(Span currentSpan, Span rootSpan, Object[] entries) {
41+
public OtelContext(Span currentSpan, Span rootSpan, BaggageContext baggageContext) {
42+
this(currentSpan, rootSpan, baggageContext, NO_ENTRIES);
43+
}
44+
45+
public OtelContext(Span currentSpan, Span rootSpan, BaggageContext baggageContext, Object[] entries) {
3946
this.currentSpan = currentSpan;
4047
this.rootSpan = rootSpan;
48+
this.baggageContext = baggageContext;
4149
this.entries = entries;
4250
}
4351

@@ -49,6 +57,8 @@ public <V> V get(ContextKey<V> key) {
4957
return (V) this.currentSpan;
5058
} else if (OTEL_CONTEXT_ROOT_SPAN_KEY.equals(key.toString())) {
5159
return (V) this.rootSpan;
60+
} else if (OTEL_CONTEXT_BAGGAGE_KEY.equals(key.toString())){
61+
return (V) this.baggageContext;
5262
}
5363
for (int i = 0; i < this.entries.length; i += 2) {
5464
if (this.entries[i] == key) {
@@ -61,9 +71,11 @@ public <V> V get(ContextKey<V> key) {
6171
@Override
6272
public <V> Context with(ContextKey<V> key, V value) {
6373
if (OTEL_CONTEXT_SPAN_KEY.equals(key.toString())) {
64-
return new OtelContext((Span) value, this.rootSpan, this.entries);
74+
return new OtelContext((Span) value, this.rootSpan, null, this.entries);
6575
} else if (OTEL_CONTEXT_ROOT_SPAN_KEY.equals(key.toString())) {
66-
return new OtelContext(this.currentSpan, (Span) value, this.entries);
76+
return new OtelContext(this.currentSpan, (Span) value, null, this.entries);
77+
} else if (OTEL_CONTEXT_BAGGAGE_KEY.equals(key.toString())){
78+
return new OtelContext(null, null, this.baggageContext, this.entries);
6779
}
6880
Object[] newEntries = null;
6981
int oldEntriesLength = this.entries.length;
@@ -82,7 +94,7 @@ public <V> Context with(ContextKey<V> key, V value) {
8294
newEntries[oldEntriesLength] = key;
8395
newEntries[oldEntriesLength + 1] = value;
8496
}
85-
return new OtelContext(this.currentSpan, this.rootSpan, newEntries);
97+
return new OtelContext(this.currentSpan, this.rootSpan, null, newEntries);
8698
}
8799

88100
@Override
@@ -149,7 +161,7 @@ public static Context current() {
149161
contextEntries = ((OtelScope) wrapper).contextEntries();
150162
}
151163
}
152-
return new OtelContext(otelCurrentSpan, otelRootSpan, contextEntries);
164+
return new OtelContext(otelCurrentSpan, otelRootSpan, null, contextEntries);
153165
}
154166

155167
/** Last propagated context not on the scope stack; {@code null} if there's no such context. */
@@ -158,6 +170,10 @@ public static Context lastPropagated() {
158170
return lastPropagated.get();
159171
}
160172

173+
public static String getOtelContextBaggageKey(){
174+
return OTEL_CONTEXT_BAGGAGE_KEY;
175+
}
176+
161177
@Override
162178
public String toString() {
163179
return "OtelContext{"

dd-java-agent/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/context/propagation/AgentTextMapPropagator.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55
import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext;
77

8+
import datadog.context.propagation.Propagators;
89
import datadog.opentelemetry.shim.context.OtelContext;
910
import datadog.opentelemetry.shim.trace.OtelExtractedContext;
1011
import datadog.opentelemetry.shim.trace.OtelSpan;
1112
import datadog.trace.api.TracePropagationStyle;
1213
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1314
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
1415
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext.Extracted;
16+
import datadog.trace.bootstrap.instrumentation.api.BaggageContext;
1517
import datadog.trace.bootstrap.instrumentation.api.TagContext;
1618
import datadog.trace.util.PropagationUtils;
19+
import io.opentelemetry.api.baggage.Baggage;
1720
import io.opentelemetry.api.trace.Span;
1821
import io.opentelemetry.api.trace.SpanContext;
1922
import io.opentelemetry.api.trace.TraceState;
2023
import io.opentelemetry.context.Context;
24+
import io.opentelemetry.context.ContextKey;
2125
import io.opentelemetry.context.propagation.TextMapGetter;
2226
import io.opentelemetry.context.propagation.TextMapPropagator;
2327
import io.opentelemetry.context.propagation.TextMapSetter;
@@ -46,20 +50,25 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
4650
if (carrier == null) {
4751
return context;
4852
}
49-
Extracted extracted =
50-
extractContextAndGetSpanContext(
51-
carrier,
52-
(carrier1, classifier) -> {
53-
for (String key : getter.keys(carrier1)) {
54-
classifier.accept(key, getter.get(carrier1, key));
55-
}
56-
});
53+
54+
datadog.context.Context extracted = Propagators.defaultPropagator().extract(datadog.context.Context.root(), carrier, (carrier1, classifier) -> {
55+
for (String key : getter.keys(carrier1)) {
56+
classifier.accept(key, getter.get(carrier1, key));
57+
}
58+
});
5759
if (extracted == null) {
5860
return context;
59-
} else {
60-
TraceState traceState = extractTraceState(extracted, carrier, getter);
61-
SpanContext spanContext = fromRemote(extracted, traceState);
62-
return new OtelContext(Span.wrap(spanContext), OtelSpan.invalid());
61+
}else{
62+
AgentSpan extractedSpan = AgentSpan.fromContext(extracted);
63+
Extracted extractedSpanContext = extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();
64+
65+
SpanContext spanContext = null;
66+
if (extractedSpanContext != null){
67+
TraceState traceState = extractTraceState(extractedSpanContext, carrier, getter);
68+
spanContext = fromRemote(extractedSpanContext, traceState);
69+
}
70+
BaggageContext baggageContext = BaggageContext.fromContext(extracted);
71+
return new OtelContext(Span.wrap(spanContext), OtelSpan.invalid(), baggageContext);
6372
}
6473
}
6574

@@ -68,7 +77,8 @@ private static datadog.context.Context convertContext(Context context) {
6877
// TODO Create fast path from OtelSpan --> AgentSpan delegate --> with() to inflate as full
6978
// context if baggage
7079
AgentSpanContext extract = OtelExtractedContext.extract(context);
71-
return AgentSpan.fromSpanContext(extract);
80+
AgentSpan agentSpan = AgentSpan.fromSpanContext(extract);
81+
return agentSpan.with(BaggageContext.getContextKey(), context.get(ContextKey.named(OtelContext.getOtelContextBaggageKey())));
7282
}
7383

7484
/**

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/BaggageContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public Map<String, String> asMap() {
6363
return new HashMap<>(baggage);
6464
}
6565

66+
public static ContextKey<BaggageContext> getContextKey(){
67+
return CONTEXT_KEY;
68+
}
69+
6670
public static BaggageContext fromContext(Context context) {
6771
return context.get(CONTEXT_KEY);
6872
}

0 commit comments

Comments
 (0)