|
1 | 1 | package datadog.opentelemetry.shim.context.propagation; |
2 | 2 |
|
3 | 3 | import static datadog.context.propagation.Propagators.defaultPropagator; |
4 | | -import static datadog.opentelemetry.shim.trace.OtelSpanContext.fromRemote; |
5 | 4 | import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT; |
6 | | -import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext; |
7 | 5 |
|
8 | 6 | import datadog.opentelemetry.shim.context.OtelContext; |
9 | 7 | import datadog.opentelemetry.shim.trace.OtelExtractedContext; |
|
13 | 11 | import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext.Extracted; |
14 | 12 | import datadog.trace.bootstrap.instrumentation.api.TagContext; |
15 | 13 | import datadog.trace.util.PropagationUtils; |
16 | | -import io.opentelemetry.api.trace.Span; |
17 | | -import io.opentelemetry.api.trace.SpanContext; |
18 | 14 | import io.opentelemetry.api.trace.TraceState; |
19 | 15 | import io.opentelemetry.context.Context; |
20 | 16 | import io.opentelemetry.context.propagation.TextMapGetter; |
@@ -45,27 +41,25 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C |
45 | 41 | if (carrier == null) { |
46 | 42 | return context; |
47 | 43 | } |
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); |
63 | 55 | } |
64 | 56 |
|
65 | 57 | 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 |
69 | 63 | AgentSpanContext extract = OtelExtractedContext.extract(context); |
70 | 64 | return AgentSpan.fromSpanContext(extract); |
71 | 65 | } |
|
0 commit comments