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 @@ -286,8 +286,7 @@ private <C> void applyTraceContextToFirstContext(
ExtractedContext traceContext,
ExtractionCache<C> extractionCache) {
// Propagate newly extracted W3C tracestate to first valid context
String extractedTracestate = traceContext.getPropagationTags().getW3CTracestate();
firstContext.getPropagationTags().updateW3CTracestate(extractedTracestate);
firstContext.getPropagationTags().updateW3CTracestateFrom(traceContext.getPropagationTags());
// Check if parent spans differ to reconcile them
if (firstContext.getSpanId() != traceContext.getSpanId()) {
// Override parent span id with W3C one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public interface Factory {
*/
public abstract void updateW3CTracestate(String tracestate);

/** Updates the original W3C tracestate header from {@code source}. */
public void updateW3CTracestateFrom(PropagationTags source) {
updateW3CTracestate(source.getW3CTracestate());
}

/**
* Constructs a header value that includes valid propagated _dd.p.* tags and possibly a new
* sampling decision tag _dd.p.dm based on the current state. Returns null if the value length
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datadog.trace.core.propagation.ptags;

final class OtelTraceState {
private final CharSequence value;
private final int originalPosition;
private final int originalSize;

private OtelTraceState(CharSequence value, int originalPosition, int originalSize) {
this.value = value;
this.originalPosition = originalPosition;
this.originalSize = originalSize;
}

static OtelTraceState parse(CharSequence raw, int originalPosition, int originalSize) {
if (raw == null || raw.length() == 0) {
return null;
}
return new OtelTraceState(raw, originalPosition, originalSize);
}

CharSequence getValue() {
return value;
}

int length() {
return value.length();
}

int getOriginalPosition() {
return originalPosition;
}

int getOriginalSize() {
return originalSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ static class PTags extends PropagationTags {

private volatile TagValue orgPropagationMarkerTagValue;

private volatile OtelTraceState otelTraceState;

// Static cache for the most-recently-seen rate → TagValue. In steady state a service uses one
// rate, so this eliminates the char[] + String allocation on every new PTags instance.
// Writes are benign-racy: two threads computing the same rate produce equal TagValues.
Expand Down Expand Up @@ -540,7 +542,34 @@ public String getW3CTracestate() {

@Override
public void updateW3CTracestate(String tracestate) {
setW3CTracestate(tracestate, W3CPTagsCodec.extractOtelTraceState(tracestate));
}

@Override
public void updateW3CTracestateFrom(PropagationTags source) {
if (!(source instanceof PTags)) {
super.updateW3CTracestateFrom(source);
return;
}
PTags sourcePTags = (PTags) source;
setW3CTracestate(sourcePTags.tracestate, sourcePTags.getOtelTraceState());
}

private void setW3CTracestate(String tracestate, OtelTraceState otelTraceState) {
clearCachedHeader(W3C);
this.tracestate = tracestate;
this.otelTraceState = otelTraceState;
}

OtelTraceState getOtelTraceState() {
return otelTraceState;
}

void setOtelTraceState(OtelTraceState otelTraceState) {
if (this.otelTraceState != otelTraceState) {
this.otelTraceState = otelTraceState;
clearCachedHeader(W3C);
}
}

String getError() {
Expand Down
Loading