Skip to content

Commit 116e341

Browse files
committed
feat(otel): Add context API support OTel propagators
1 parent 5bfab65 commit 116e341

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ public String toString() {
8989
return "OtelContext{" + "delegate=" + delegate + '}';
9090
}
9191

92+
/**
93+
* Returns the underlying context.
94+
*
95+
* @return The underlying context.
96+
*/
97+
public datadog.context.Context asContext() {
98+
return this.delegate;
99+
}
100+
92101
private static datadog.context.ContextKey delegateKey(ContextKey key) {
93102
return DELEGATE_KEYS.computeIfAbsent(key, OtelContext::mapByKeyName);
94103
}

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package datadog.opentelemetry.shim.context.propagation;
22

33
import static datadog.context.propagation.Propagators.defaultPropagator;
4-
import static datadog.opentelemetry.shim.trace.OtelSpanContext.fromRemote;
54
import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT;
6-
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext;
75

86
import datadog.opentelemetry.shim.context.OtelContext;
97
import datadog.opentelemetry.shim.trace.OtelExtractedContext;
@@ -13,8 +11,6 @@
1311
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext.Extracted;
1412
import datadog.trace.bootstrap.instrumentation.api.TagContext;
1513
import datadog.trace.util.PropagationUtils;
16-
import io.opentelemetry.api.trace.Span;
17-
import io.opentelemetry.api.trace.SpanContext;
1814
import io.opentelemetry.api.trace.TraceState;
1915
import io.opentelemetry.context.Context;
2016
import io.opentelemetry.context.propagation.TextMapGetter;
@@ -45,27 +41,25 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
4541
if (carrier == null) {
4642
return context;
4743
}
48-
Extracted extracted =
49-
extractContextAndGetSpanContext(
50-
carrier,
51-
(carrier1, classifier) -> {
52-
for (String key : getter.keys(carrier1)) {
53-
classifier.accept(key, getter.get(carrier1, key));
54-
}
55-
});
56-
if (extracted == null) {
57-
return context;
58-
} else {
59-
TraceState traceState = extractTraceState(extracted, carrier, getter);
60-
SpanContext spanContext = fromRemote(extracted, traceState);
61-
return Span.wrap(spanContext).storeInContext(OtelContext.ROOT);
62-
}
44+
datadog.context.Context extracted =
45+
defaultPropagator()
46+
.extract(
47+
convertContext(context),
48+
carrier,
49+
(carrier1, classifier) -> {
50+
for (String key : getter.keys(carrier1)) {
51+
classifier.accept(key, getter.get(carrier1, key));
52+
}
53+
});
54+
return new OtelContext(extracted);
6355
}
6456

6557
private static datadog.context.Context convertContext(Context context) {
66-
// TODO Extract baggage too
67-
// TODO Create fast path from OtelSpan --> AgentSpan delegate --> with() to inflate as full
68-
// context if baggage
58+
// Try to get the underlying context when injecting a Datadog context
59+
if (context instanceof OtelContext) {
60+
return ((OtelContext) context).asContext();
61+
}
62+
// Otherwise, fallback to extracting limited tracing context and recreating an OTel context from
6963
AgentSpanContext extract = OtelExtractedContext.extract(context);
7064
return AgentSpan.fromSpanContext(extract);
7165
}

0 commit comments

Comments
 (0)