Skip to content

Commit f49ac98

Browse files
authored
Updating Span Link creation due to header tag propagations for invalid spans (#7799)
* adding functionality and testing for attributes in Span Links from compund extraction * running circleci tests on updated commit for system-tests * only allowing tracestate to be passed when propagation style is tracecontext * spotless * reverting default_system_tests * updating unit test to use put instead of Map.of * updating PR comments * spotless
1 parent 6eac0b8 commit f49ac98

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import datadog.trace.api.TraceConfig;
1313
import datadog.trace.api.TracePropagationStyle;
1414
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
15+
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes;
1516
import datadog.trace.bootstrap.instrumentation.api.TagContext;
1617
import datadog.trace.core.DDSpanContext;
1718
import datadog.trace.core.DDSpanLink;
@@ -240,7 +241,13 @@ public <C> TagContext extract(
240241
}
241242
} else {
242243
// Terminate extracted context and add it as span link
243-
context.addTerminatedContextLink(DDSpanLink.from((ExtractedContext) extracted));
244+
context.addTerminatedContextLink(
245+
DDSpanLink.from(
246+
(ExtractedContext) extracted,
247+
SpanAttributes.builder()
248+
.put("reason", "terminated_context")
249+
.put("context_headers", extracted.getPropagationStyle().toString())
250+
.build()));
244251
// TODO Note: Other vendor tracestate will be lost here
245252
}
246253
}

dd-trace-core/src/test/groovy/datadog/trace/core/propagation/HttpExtractorTest.groovy

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,44 @@ class HttpExtractorTest extends DDSpecification {
166166
[DATADOG, TRACECONTEXT] | "1" | "2" | null | null | W3C_TRACE_STATE_NO_P | "1" | W3C_SPAN_ID_LSTR | "0000000000000002"
167167
// spotless:on
168168
}
169+
170+
def "verify existence of span links when extracting compound http headers"() {
171+
setup:
172+
Config config = Mock(Config) {
173+
getTracePropagationStylesToExtract() >> styles
174+
}
175+
DynamicConfig dynamicConfig = DynamicConfig.create().apply()
176+
HttpCodec.Extractor extractor = HttpCodec.createExtractor(config, { dynamicConfig.captureTraceConfig() })
177+
178+
final Map<String, String> actual = [
179+
(DatadogHttpCodec.TRACE_ID_KEY.toUpperCase()): datadogTraceId,
180+
(DatadogHttpCodec.SPAN_ID_KEY.toUpperCase()) : datadogSpanId,
181+
(B3HttpCodec.TRACE_ID_KEY.toUpperCase()) : b3TraceId,
182+
(B3HttpCodec.SPAN_ID_KEY.toUpperCase()) : b3SpanId,
183+
(W3CHttpCodec.TRACE_PARENT_KEY.toUpperCase()): w3cTraceParent,
184+
(W3CHttpCodec.TRACE_STATE_KEY.toUpperCase()) : traceState
185+
]
186+
187+
when:
188+
final TagContext context = extractor.extract(actual, ContextVisitors.stringValuesMap())
189+
190+
then:
191+
def links = context.getTerminatedContextLinks()
192+
assert links.size() == expectedSpanLinks.size()
193+
for (int i = 0; i < links.size(); i++) {
194+
if (expectedSpanLinks[i] == TRACECONTEXT) {
195+
assert links[i].traceState() == W3C_TRACE_STATE_NO_P
196+
}
197+
assert links[i].attributes().asMap()["context_headers"] == expectedSpanLinks[i].toString()
198+
}
199+
200+
where:
201+
// spotless:off
202+
styles | datadogTraceId | datadogSpanId | b3TraceId | b3SpanId | w3cTraceParent | traceState | expectedSpanLinks
203+
[DATADOG, B3MULTI, TRACECONTEXT] | "1" | "2" | "1" | "b" | W3C_TRACE_PARENT | W3C_TRACE_STATE_NO_P | []
204+
[DATADOG, B3MULTI, TRACECONTEXT] | "2" | "2" | "2" | "b" | W3C_TRACE_PARENT | W3C_TRACE_STATE_NO_P | [TRACECONTEXT]
205+
[DATADOG, B3MULTI, TRACECONTEXT] | "2" | "2" | "1" | "b" | W3C_TRACE_PARENT | W3C_TRACE_STATE_NO_P | [B3MULTI, TRACECONTEXT]
206+
[TRACECONTEXT, B3MULTI, DATADOG] | "2" | "2" | "1" | "b" | W3C_TRACE_PARENT | W3C_TRACE_STATE_NO_P | [DATADOG]
207+
// spotless:on
208+
}
169209
}

0 commit comments

Comments
 (0)