Skip to content

Commit e6ab681

Browse files
authored
fix(propagation): never clobber extracted decisionmaker tags... [backport 2.6] (#8720)
This pull request resolves [AIT-9945] by removing the condition gating the logic that avoids clobbering the `_dd.p.dm` tag when it's present on propagated headers. ## Checklist - [x] Change(s) are motivated and described in the PR description [AIT-9945]: https://datadoghq.atlassian.net/browse/AIT-9945?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent f226ae6 commit e6ab681

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

ddtrace/propagation/http.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
from ..internal.constants import W3C_TRACEPARENT_KEY
4040
from ..internal.constants import W3C_TRACESTATE_KEY
4141
from ..internal.logger import get_logger
42+
from ..internal.sampling import SAMPLING_DECISION_TRACE_TAG_KEY
43+
from ..internal.sampling import SamplingMechanism
4244
from ..internal.sampling import validate_sampling_decision
4345
from ..span import _get_64_highest_order_bits_as_hex
4446
from ..span import _get_64_lowest_order_bits_as_int
@@ -321,6 +323,12 @@ def _extract(headers):
321323
del meta[_HIGHER_ORDER_TRACE_ID_BITS]
322324
log.warning("malformed_tid: %s. Failed to decode trace id from http headers", trace_id_hob_hex)
323325

326+
if not meta:
327+
meta = {}
328+
329+
if not meta.get(SAMPLING_DECISION_TRACE_TAG_KEY):
330+
meta[SAMPLING_DECISION_TRACE_TAG_KEY] = f"-{SamplingMechanism.TRACE_SAMPLING_RULE}"
331+
324332
# Try to parse values into their expected types
325333
try:
326334
if sampling_priority is not None:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
propagation: This fix resolves an issue where the sampling decision-maker tag in tracestate propagation headers
5+
was clobbered by a default value.

tests/snapshots/tests.contrib.wsgi.test_wsgi.test_distributed_tracing_nested.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"error": 0,
1111
"meta": {
1212
"_dd.base_service": "",
13+
"_dd.p.dm": "-3",
1314
"_dd.p.tid": "654a694400000000",
1415
"component": "wsgi",
1516
"http.method": "GET",

tests/tracer/test_propagation.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def test_extract(tracer): # noqa: F811
284284
assert span.context.dd_origin == "synthetics"
285285
assert span.context._meta == {
286286
"_dd.origin": "synthetics",
287+
"_dd.p.dm": "-3",
287288
"_dd.p.test": "value",
288289
}
289290
with tracer.trace("child_span") as child_span:
@@ -293,6 +294,7 @@ def test_extract(tracer): # noqa: F811
293294
assert child_span.context.dd_origin == "synthetics"
294295
assert child_span.context._meta == {
295296
"_dd.origin": "synthetics",
297+
"_dd.p.dm": "-3",
296298
"_dd.p.test": "value",
297299
}
298300

@@ -464,6 +466,7 @@ def test_extract_unicode(tracer): # noqa: F811
464466

465467
assert span.context._meta == {
466468
"_dd.origin": "synthetics",
469+
"_dd.p.dm": "-3",
467470
"_dd.p.test": "value",
468471
}
469472
with tracer.trace("child_span") as child_span:
@@ -473,6 +476,7 @@ def test_extract_unicode(tracer): # noqa: F811
473476
assert child_span.context.dd_origin == "synthetics"
474477
assert child_span.context._meta == {
475478
"_dd.origin": "synthetics",
479+
"_dd.p.dm": "-3",
476480
"_dd.p.test": "value",
477481
}
478482

@@ -526,6 +530,7 @@ def test_WSGI_extract(tracer): # noqa: F811
526530
assert span.context._meta == {
527531
"_dd.origin": "synthetics",
528532
"_dd.p.test": "value",
533+
"_dd.p.dm": "-3",
529534
}
530535

531536

@@ -549,6 +554,7 @@ def test_extract_invalid_tags(tracer): # noqa: F811
549554
assert span.context.dd_origin == "synthetics"
550555
assert span.context._meta == {
551556
"_dd.origin": "synthetics",
557+
"_dd.p.dm": "-3",
552558
"_dd.propagation_error": "decoding_error",
553559
}
554560

@@ -573,6 +579,7 @@ def test_extract_tags_large(tracer): # noqa: F811
573579
assert span.context.dd_origin == "synthetics"
574580
assert span.context._meta == {
575581
"_dd.origin": "synthetics",
582+
"_dd.p.dm": "-3",
576583
"_dd.propagation_error": "extract_max_size",
577584
}
578585

@@ -1124,6 +1131,7 @@ def test_extract_tracecontext(headers, expected_context):
11241131
"span_id": 5678,
11251132
"sampling_priority": 1,
11261133
"dd_origin": "synthetics",
1134+
"meta": {"_dd.p.dm": "-3"},
11271135
},
11281136
),
11291137
(
@@ -1135,6 +1143,7 @@ def test_extract_tracecontext(headers, expected_context):
11351143
"span_id": 5678,
11361144
"sampling_priority": 1,
11371145
"dd_origin": "synthetics",
1146+
"meta": {"_dd.p.dm": "-3"},
11381147
},
11391148
),
11401149
(
@@ -1152,6 +1161,7 @@ def test_extract_tracecontext(headers, expected_context):
11521161
"span_id": 5678,
11531162
"sampling_priority": 1,
11541163
"dd_origin": "synthetics",
1164+
"meta": {"_dd.p.dm": "-3"},
11551165
},
11561166
),
11571167
(
@@ -1174,6 +1184,7 @@ def test_extract_tracecontext(headers, expected_context):
11741184
"span_id": 5678,
11751185
"sampling_priority": 1,
11761186
"dd_origin": "synthetics",
1187+
"meta": {"_dd.p.dm": "-3"},
11771188
},
11781189
),
11791190
(
@@ -1185,6 +1196,7 @@ def test_extract_tracecontext(headers, expected_context):
11851196
"span_id": 5678,
11861197
"sampling_priority": 1,
11871198
"dd_origin": "synthetics",
1199+
"meta": {"_dd.p.dm": "-3"},
11881200
},
11891201
),
11901202
(
@@ -1417,6 +1429,7 @@ def test_extract_tracecontext(headers, expected_context):
14171429
"span_id": 5678,
14181430
"sampling_priority": 1,
14191431
"dd_origin": "synthetics",
1432+
"meta": {"_dd.p.dm": "-3"},
14201433
"span_links": [
14211434
SpanLink(
14221435
trace_id=TRACE_ID,
@@ -1444,6 +1457,7 @@ def test_extract_tracecontext(headers, expected_context):
14441457
"span_id": 5678,
14451458
"sampling_priority": 1,
14461459
"dd_origin": "synthetics",
1460+
"meta": {"_dd.p.dm": "-3"},
14471461
"span_links": [
14481462
SpanLink(
14491463
trace_id=TRACE_ID,
@@ -1483,6 +1497,7 @@ def test_extract_tracecontext(headers, expected_context):
14831497
"span_id": 5678,
14841498
"sampling_priority": 1,
14851499
"dd_origin": "synthetics",
1500+
"meta": {"_dd.p.dm": "-3"},
14861501
"span_links": [
14871502
SpanLink(
14881503
trace_id=TRACE_ID,
@@ -1517,6 +1532,7 @@ def test_extract_tracecontext(headers, expected_context):
15171532
"span_id": 5678,
15181533
"sampling_priority": 1,
15191534
"dd_origin": "synthetics",
1535+
"meta": {"_dd.p.dm": "-3"},
15201536
},
15211537
),
15221538
(
@@ -1528,6 +1544,7 @@ def test_extract_tracecontext(headers, expected_context):
15281544
"span_id": 5678,
15291545
"sampling_priority": 1,
15301546
"dd_origin": "synthetics",
1547+
"meta": {"_dd.p.dm": "-3"},
15311548
},
15321549
),
15331550
(
@@ -1596,6 +1613,7 @@ def test_extract_tracecontext(headers, expected_context):
15961613
"span_id": 5678,
15971614
"sampling_priority": 1,
15981615
"dd_origin": "synthetics",
1616+
"meta": {"_dd.p.dm": "-3"},
15991617
},
16001618
),
16011619
# Testing that order matters
@@ -1664,7 +1682,7 @@ def test_extract_tracecontext(headers, expected_context):
16641682
"span_id": 5678,
16651683
"sampling_priority": 1,
16661684
"dd_origin": "synthetics",
1667-
"meta": {"tracestate": TRACECONTEXT_HEADERS_VALID[_HTTP_HEADER_TRACESTATE]},
1685+
"meta": {"tracestate": TRACECONTEXT_HEADERS_VALID[_HTTP_HEADER_TRACESTATE], "_dd.p.dm": "-3"},
16681686
},
16691687
),
16701688
# testing that tracestate is not added when tracecontext style comes later and does not match first style's trace-id
@@ -1677,6 +1695,7 @@ def test_extract_tracecontext(headers, expected_context):
16771695
"span_id": 5678,
16781696
"sampling_priority": 1,
16791697
"dd_origin": "synthetics",
1698+
"meta": {"_dd.p.dm": "-3"},
16801699
"span_links": [
16811700
SpanLink(
16821701
trace_id=TRACE_ID,
@@ -1958,7 +1977,7 @@ def test_DD_TRACE_PROPAGATION_STYLE_EXTRACT_overrides_DD_TRACE_PROPAGATION_STYLE
19581977
Context(
19591978
trace_id=13088165645273925489,
19601979
span_id=5678,
1961-
meta={"_dd.origin": "synthetics"},
1980+
meta={"_dd.origin": "synthetics", "_dd.p.dm": "-3"},
19621981
metrics={"_sampling_priority_v1": 1},
19631982
span_links=[
19641983
SpanLink(
@@ -2001,7 +2020,14 @@ def test_DD_TRACE_PROPAGATION_STYLE_EXTRACT_overrides_DD_TRACE_PROPAGATION_STYLE
20012020
Context(
20022021
trace_id=7277407061855694839,
20032022
span_id=5678,
2004-
meta={"_dd.origin": "synthetics", "tracestate": "dd=s:2;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE"},
2023+
# it's weird that both _dd.p.dm and tracestate.t.dm are set here. as far as i know, this is the expected
2024+
# behavior for this chaotic set of headers, specifically when STYLE_DATADOG precedes STYLE_W3C_TRACECONTEXT
2025+
# in the styles configuration
2026+
meta={
2027+
"_dd.p.dm": "-3",
2028+
"_dd.origin": "synthetics",
2029+
"tracestate": "dd=s:2;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
2030+
},
20052031
metrics={"_sampling_priority_v1": 1},
20062032
span_links=[
20072033
SpanLink(
@@ -2018,7 +2044,7 @@ def test_DD_TRACE_PROPAGATION_STYLE_EXTRACT_overrides_DD_TRACE_PROPAGATION_STYLE
20182044

20192045

20202046
@pytest.mark.parametrize("name,styles,headers,expected_context", FULL_CONTEXT_EXTRACT_FIXTURES)
2021-
def test_mutliple_context_interactions(name, styles, headers, expected_context):
2047+
def test_multiple_context_interactions(name, styles, headers, expected_context):
20222048
with override_global_config(dict(_propagation_style_extract=styles)):
20232049
context = HTTPPropagator.extract(headers)
20242050
assert context == expected_context

0 commit comments

Comments
 (0)