Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public String toString() {
return "OtelContext{" + "delegate=" + delegate + '}';
}

/**
* Returns the underlying context.
*
* @return The underlying context.
*/
public datadog.context.Context asContext() {
return this.delegate;
}

private static datadog.context.ContextKey delegateKey(ContextKey key) {
return DELEGATE_KEYS.computeIfAbsent(key, OtelContext::mapByKeyName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package datadog.opentelemetry.shim.context.propagation;

import static datadog.context.propagation.Propagators.defaultPropagator;
import static datadog.opentelemetry.shim.trace.OtelSpanContext.fromRemote;
import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT;
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.extractContextAndGetSpanContext;

import datadog.opentelemetry.shim.context.OtelContext;
import datadog.opentelemetry.shim.trace.OtelExtractedContext;
Expand All @@ -13,8 +11,6 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext.Extracted;
import datadog.trace.bootstrap.instrumentation.api.TagContext;
import datadog.trace.util.PropagationUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
Expand Down Expand Up @@ -45,27 +41,25 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
if (carrier == null) {
return context;
}
Extracted extracted =
extractContextAndGetSpanContext(
carrier,
(carrier1, classifier) -> {
for (String key : getter.keys(carrier1)) {
classifier.accept(key, getter.get(carrier1, key));
}
});
if (extracted == null) {
return context;
} else {
TraceState traceState = extractTraceState(extracted, carrier, getter);
SpanContext spanContext = fromRemote(extracted, traceState);
return Span.wrap(spanContext).storeInContext(OtelContext.ROOT);
}
datadog.context.Context extracted =
defaultPropagator()
.extract(
convertContext(context),
carrier,
(carrier1, classifier) -> {
for (String key : getter.keys(carrier1)) {
classifier.accept(key, getter.get(carrier1, key));
}
});
return new OtelContext(extracted);
}

private static datadog.context.Context convertContext(Context context) {
// TODO Extract baggage too
// TODO Create fast path from OtelSpan --> AgentSpan delegate --> with() to inflate as full
// context if baggage
// Try to get the underlying context when injecting a Datadog context
if (context instanceof OtelContext) {
return ((OtelContext) context).asContext();
}
// Otherwise, fallback to extracting limited tracing context and recreating an OTel context from
AgentSpanContext extract = OtelExtractedContext.extract(context);
return AgentSpan.fromSpanContext(extract);
}
Expand Down
Loading