Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6865282
re-committing after formatting ran from git hooks
mtoffl01 Jul 23, 2024
526e582
Revert temp changes to ExtractedContext.java
mtoffl01 Jul 23, 2024
d17b895
Update HttpCodec.java
mtoffl01 Jul 23, 2024
9d8fa79
Update HttpCodec.java
mtoffl01 Jul 23, 2024
d2db6e4
Update HttpCodec.java
mtoffl01 Jul 23, 2024
b7aae73
Reformatted
mtoffl01 Jul 24, 2024
2c44b58
Update HttpCodec.java
mtoffl01 Jul 24, 2024
9401fc2
feat(core): Simplify W3C phase 3 implementation
PerfectSlayer Jul 26, 2024
57185ef
ran formatter
mtoffl01 Jul 30, 2024
ae3f5bb
ran formatter
mtoffl01 Jul 30, 2024
6f9d628
Ran formatter
mtoffl01 Jul 30, 2024
62c8c6a
Added unit tests
mtoffl01 Jul 31, 2024
0f343ea
removed file
mtoffl01 Jul 31, 2024
72aaba4
formatter
mtoffl01 Aug 1, 2024
d98e799
removed superfluous comment and variable
mtoffl01 Aug 1, 2024
29d67d7
feat(context): Improve W3C support
PerfectSlayer Aug 2, 2024
45ce050
feat(context): Add W3C header override test
PerfectSlayer Aug 2, 2024
3bd6bcf
fix lastParentId assignment for case when no p tag available
mtoffl01 Aug 2, 2024
d93bb1a
remove superfluous comment
mtoffl01 Aug 2, 2024
810158c
feat(context): Make sure last parent id can't be invalid
PerfectSlayer Aug 5, 2024
5703c94
feat(context): Make sure last parent id can't be invalid
PerfectSlayer Aug 5, 2024
4d447c6
feat(context): Make sure last parent id can't be invalid
PerfectSlayer Aug 6, 2024
60c9dda
Changed comment to be clearer
mtoffl01 Aug 6, 2024
e433adf
added more unit test cases for better codecov
mtoffl01 Aug 6, 2024
a16b342
Merge branch 'master' into mtoff/w3c-phase-3
mtoffl01 Aug 9, 2024
3c73b58
update TODO comment
mtoffl01 Aug 9, 2024
9774d3f
remove mistake span events changes
mtoffl01 Aug 14, 2024
8cb89a5
Clarified wording on applyTraceContextToFirstContext function
mtoffl01 Aug 15, 2024
b81ef67
Merge branch 'master' into mtoff/w3c-phase-3
mtoffl01 Aug 16, 2024
f25f61b
Update system-tests to 412461cf49ec3115b291f92ad6c7e5a0758545b6 in or…
zacharycmontoya Aug 19, 2024
85c9005
Revert "Update system-tests to 412461cf49ec3115b291f92ad6c7e5a0758545…
zacharycmontoya Aug 20, 2024
1d63cae
Merge branch 'master' into mtoff/w3c-phase-3
PerfectSlayer Aug 23, 2024
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 @@ -12,9 +12,9 @@
*/
public class ExtractedContext extends TagContext {
private final DDTraceId traceId;
private final long spanId;
private final long endToEndStartTime;
private final PropagationTags propagationTags;
private long spanId;

public ExtractedContext(
final DDTraceId traceId,
Expand Down Expand Up @@ -66,6 +66,10 @@ public final long getSpanId() {
return spanId;
}

public final void overrideSpanId(final long spanId) {
this.spanId = spanId;
}

public final long getEndToEndStartTime() {
return endToEndStartTime;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package datadog.trace.core.propagation;

import static datadog.trace.api.DDTags.PARENT_ID;
import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT;
import static datadog.trace.core.propagation.DatadogHttpCodec.SPAN_ID_KEY;

import datadog.trace.api.Config;
import datadog.trace.api.DD128bTraceId;
import datadog.trace.api.DD64bTraceId;
import datadog.trace.api.DDSpanId;
import datadog.trace.api.DDTraceId;
import datadog.trace.api.TraceConfig;
import datadog.trace.api.TracePropagationStyle;
Expand Down Expand Up @@ -233,10 +236,7 @@ public <C> TagContext extract(
if (traceIdMatch(context.getTraceId(), extractedContext.getTraceId())) {
boolean comingFromTraceContext = extracted.getPropagationStyle() == TRACECONTEXT;
if (comingFromTraceContext) {
// Propagate newly extracted W3C tracestate to first valid context
String extractedTracestate =
extractedContext.getPropagationTags().getW3CTracestate();
context.getPropagationTags().updateW3CTracestate(extractedTracestate);
applyTraceContextToFirstContext(context, extractedContext, extractionCache);
}
} else {
// Terminate extracted context and add it as span link
Expand All @@ -262,13 +262,48 @@ else if (extracted != null && partialContext == null) {
return null;
}
}

/**
* Applies span ID from W3C trace context over any other valid context previously found.
*
* @param firstContext The first valid context found.
* @param traceContext The trace context to apply.
* @param extractionCache The extraction cache to get quick access to any extra information.
* @param <C> The carrier type.
*/
private <C> void applyTraceContextToFirstContext(
ExtractedContext firstContext,
ExtractedContext traceContext,
ExtractionCache<C> extractionCache) {
// Propagate newly extracted W3C tracestate to first valid context
String extractedTracestate = traceContext.getPropagationTags().getW3CTracestate();
firstContext.getPropagationTags().updateW3CTracestate(extractedTracestate);
// Check if parent spans differ to reconcile them
if (firstContext.getSpanId() != traceContext.getSpanId()) {
// Override parent span id with W3C one
firstContext.overrideSpanId(traceContext.getSpanId());
// Add last parent ID as a span tag (check W3C first, else Datadog)
CharSequence lastParentId = traceContext.getPropagationTags().getLastParentId();
if (lastParentId == null) {
lastParentId = extractionCache.getDatadogSpanIdHex();
}
if (lastParentId != null) {
firstContext.putTag(PARENT_ID, lastParentId.toString());
}
}
}
}

private static class ExtractionCache<C>
implements AgentPropagation.KeyClassifier,
AgentPropagation.ContextVisitor<ExtractionCache<?>> {
/** Cached context key-values (even indexes are header names, odd indexes are header values). */
private final List<String> keysAndValues;
/**
* The parent span identifier from {@link DatadogHttpCodec#SPAN_ID_KEY} header formatted as 16
* hexadecimal characters, {@code null} if absent or invalid.
*/
private String datadogSpanIdHex;

public ExtractionCache(C carrier, AgentPropagation.ContextVisitor<C> getter) {
this.keysAndValues = new ArrayList<>(32);
Expand All @@ -279,9 +314,24 @@ public ExtractionCache(C carrier, AgentPropagation.ContextVisitor<C> getter) {
public boolean accept(String key, String value) {
this.keysAndValues.add(key);
this.keysAndValues.add(value);
cacheDatadogSpanId(key, value);
return true;
}

private void cacheDatadogSpanId(String key, String value) {
if (SPAN_ID_KEY.equalsIgnoreCase(key)) {
try {
// Parse numeric header value to format it as 16 hexadecimal character format
this.datadogSpanIdHex = DDSpanId.toHexStringPadded(DDSpanId.from(value));
} catch (NumberFormatException ignored) {
}
}
}

private String getDatadogSpanIdHex() {
return this.datadogSpanIdHex;
}

@Override
public void forEachKey(ExtractionCache<?> carrier, AgentPropagation.KeyClassifier classifier) {
List<String> keysAndValues = carrier.keysAndValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ static class PTags extends PropagationTags {
*/
protected volatile String error;

/**
* The last parent span id using the 16-characters zero padded hexadecimal representation,
* {@code null} if not set.
*/
private volatile CharSequence lastParentId;

public PTags(
Expand Down Expand Up @@ -271,7 +275,6 @@ public CharSequence getLastParentId() {

@Override
public void updateLastParentId(CharSequence lastParentId) {
lastParentId = "0000000000000000".equals(lastParentId) ? null : lastParentId;
if (!Objects.equals(this.lastParentId, lastParentId)) {
clearCachedHeader(W3C);
this.lastParentId = TagValue.from(lastParentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class W3CPTagsCodec extends PTagsCodec {
private static final int MIN_ALLOWED_CHAR = 32;
private static final int MAX_ALLOWED_CHAR = 126;
private static final int MAX_MEMBER_COUNT = 32;
private static final String LAST_PARENT_ZERO = "0000000000000000";

@Override
PropagationTags fromHeaderValue(PTagsFactory tagsFactory, String value) {
Expand Down Expand Up @@ -96,7 +95,7 @@ PropagationTags fromHeaderValue(PTagsFactory tagsFactory, String value) {
TagValue traceIdTagValue = null;
boolean appsecPropagationEnabled = false;
int maxUnknownSize = 0;
CharSequence lastParentId = LAST_PARENT_ZERO;
CharSequence lastParentId = null;
while (tagPos < ddMemberValueEnd) {
int tagKeyEndsAt =
validateCharsUntilSeparatorOrEnd(
Expand Down Expand Up @@ -242,7 +241,7 @@ protected int appendPrefix(StringBuilder sb, PTags ptags) {
}
// append last ParentId (p)
CharSequence lastParent = ptags.getLastParentId();
if (lastParent != null && !lastParent.equals(LAST_PARENT_ZERO)) {
if (lastParent != null) {
if (sb.length() > EMPTY_SIZE) {
sb.append(';');
}
Expand Down
Loading