Skip to content

Commit 8431c43

Browse files
authored
fix(propagation): never clobber extracted decisionmaker tags... [backport 2.7] (#8719)
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 c08e8a7 commit 8431c43

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

ddtrace/propagation/http.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,10 @@ def _extract(headers):
322322
del meta[_HIGHER_ORDER_TRACE_ID_BITS]
323323
log.warning("malformed_tid: %s. Failed to decode trace id from http headers", trace_id_hob_hex)
324324

325-
if sampling_priority == USER_KEEP:
326-
if not meta:
327-
meta = {}
325+
if not meta:
326+
meta = {}
327+
328+
if not meta.get(SAMPLING_DECISION_TRACE_TAG_KEY):
328329
meta[SAMPLING_DECISION_TRACE_TAG_KEY] = f"-{SamplingMechanism.TRACE_SAMPLING_RULE}"
329330

330331
# Try to parse values into their expected types
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/tracer/test_propagation.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def test_extract(tracer): # noqa: F811
286286
assert span.context.dd_origin == "synthetics"
287287
assert span.context._meta == {
288288
"_dd.origin": "synthetics",
289+
"_dd.p.dm": "-3",
289290
"_dd.p.test": "value",
290291
}
291292
with tracer.trace("child_span") as child_span:
@@ -295,6 +296,7 @@ def test_extract(tracer): # noqa: F811
295296
assert child_span.context.dd_origin == "synthetics"
296297
assert child_span.context._meta == {
297298
"_dd.origin": "synthetics",
299+
"_dd.p.dm": "-3",
298300
"_dd.p.test": "value",
299301
}
300302

@@ -498,6 +500,7 @@ def test_extract_unicode(tracer): # noqa: F811
498500

499501
assert span.context._meta == {
500502
"_dd.origin": "synthetics",
503+
"_dd.p.dm": "-3",
501504
"_dd.p.test": "value",
502505
}
503506
with tracer.trace("child_span") as child_span:
@@ -507,6 +510,7 @@ def test_extract_unicode(tracer): # noqa: F811
507510
assert child_span.context.dd_origin == "synthetics"
508511
assert child_span.context._meta == {
509512
"_dd.origin": "synthetics",
513+
"_dd.p.dm": "-3",
510514
"_dd.p.test": "value",
511515
}
512516

@@ -560,6 +564,7 @@ def test_WSGI_extract(tracer): # noqa: F811
560564
assert span.context._meta == {
561565
"_dd.origin": "synthetics",
562566
"_dd.p.test": "value",
567+
"_dd.p.dm": "-3",
563568
}
564569

565570

@@ -583,6 +588,7 @@ def test_extract_invalid_tags(tracer): # noqa: F811
583588
assert span.context.dd_origin == "synthetics"
584589
assert span.context._meta == {
585590
"_dd.origin": "synthetics",
591+
"_dd.p.dm": "-3",
586592
"_dd.propagation_error": "decoding_error",
587593
}
588594

@@ -607,6 +613,7 @@ def test_extract_tags_large(tracer): # noqa: F811
607613
assert span.context.dd_origin == "synthetics"
608614
assert span.context._meta == {
609615
"_dd.origin": "synthetics",
616+
"_dd.p.dm": "-3",
610617
"_dd.propagation_error": "extract_max_size",
611618
}
612619

@@ -1180,6 +1187,7 @@ def test_extract_tracecontext(headers, expected_context):
11801187
"span_id": 5678,
11811188
"sampling_priority": 1,
11821189
"dd_origin": "synthetics",
1190+
"meta": {"_dd.p.dm": "-3"},
11831191
},
11841192
),
11851193
(
@@ -1191,6 +1199,7 @@ def test_extract_tracecontext(headers, expected_context):
11911199
"span_id": 5678,
11921200
"sampling_priority": 1,
11931201
"dd_origin": "synthetics",
1202+
"meta": {"_dd.p.dm": "-3"},
11941203
},
11951204
),
11961205
(
@@ -1220,6 +1229,7 @@ def test_extract_tracecontext(headers, expected_context):
12201229
"span_id": 5678,
12211230
"sampling_priority": 1,
12221231
"dd_origin": "synthetics",
1232+
"meta": {"_dd.p.dm": "-3"},
12231233
},
12241234
),
12251235
(
@@ -1242,6 +1252,7 @@ def test_extract_tracecontext(headers, expected_context):
12421252
"span_id": 5678,
12431253
"sampling_priority": 1,
12441254
"dd_origin": "synthetics",
1255+
"meta": {"_dd.p.dm": "-3"},
12451256
},
12461257
),
12471258
(
@@ -1253,6 +1264,7 @@ def test_extract_tracecontext(headers, expected_context):
12531264
"span_id": 5678,
12541265
"sampling_priority": 1,
12551266
"dd_origin": "synthetics",
1267+
"meta": {"_dd.p.dm": "-3"},
12561268
},
12571269
),
12581270
(
@@ -1485,6 +1497,7 @@ def test_extract_tracecontext(headers, expected_context):
14851497
"span_id": 5678,
14861498
"sampling_priority": 1,
14871499
"dd_origin": "synthetics",
1500+
"meta": {"_dd.p.dm": "-3"},
14881501
"span_links": [
14891502
SpanLink(
14901503
trace_id=TRACE_ID,
@@ -1512,6 +1525,7 @@ def test_extract_tracecontext(headers, expected_context):
15121525
"span_id": 5678,
15131526
"sampling_priority": 1,
15141527
"dd_origin": "synthetics",
1528+
"meta": {"_dd.p.dm": "-3"},
15151529
"span_links": [
15161530
SpanLink(
15171531
trace_id=TRACE_ID,
@@ -1551,6 +1565,7 @@ def test_extract_tracecontext(headers, expected_context):
15511565
"span_id": 5678,
15521566
"sampling_priority": 1,
15531567
"dd_origin": "synthetics",
1568+
"meta": {"_dd.p.dm": "-3"},
15541569
"span_links": [
15551570
SpanLink(
15561571
trace_id=TRACE_ID,
@@ -1585,6 +1600,7 @@ def test_extract_tracecontext(headers, expected_context):
15851600
"span_id": 5678,
15861601
"sampling_priority": 1,
15871602
"dd_origin": "synthetics",
1603+
"meta": {"_dd.p.dm": "-3"},
15881604
},
15891605
),
15901606
(
@@ -1596,6 +1612,7 @@ def test_extract_tracecontext(headers, expected_context):
15961612
"span_id": 5678,
15971613
"sampling_priority": 1,
15981614
"dd_origin": "synthetics",
1615+
"meta": {"_dd.p.dm": "-3"},
15991616
},
16001617
),
16011618
(
@@ -1664,6 +1681,7 @@ def test_extract_tracecontext(headers, expected_context):
16641681
"span_id": 5678,
16651682
"sampling_priority": 1,
16661683
"dd_origin": "synthetics",
1684+
"meta": {"_dd.p.dm": "-3"},
16671685
},
16681686
),
16691687
# Testing that order matters
@@ -1732,7 +1750,7 @@ def test_extract_tracecontext(headers, expected_context):
17321750
"span_id": 5678,
17331751
"sampling_priority": 1,
17341752
"dd_origin": "synthetics",
1735-
"meta": {"tracestate": TRACECONTEXT_HEADERS_VALID[_HTTP_HEADER_TRACESTATE]},
1753+
"meta": {"tracestate": TRACECONTEXT_HEADERS_VALID[_HTTP_HEADER_TRACESTATE], "_dd.p.dm": "-3"},
17361754
},
17371755
),
17381756
# testing that tracestate is not added when tracecontext style comes later and does not match first style's trace-id
@@ -1745,6 +1763,7 @@ def test_extract_tracecontext(headers, expected_context):
17451763
"span_id": 5678,
17461764
"sampling_priority": 1,
17471765
"dd_origin": "synthetics",
1766+
"meta": {"_dd.p.dm": "-3"},
17481767
"span_links": [
17491768
SpanLink(
17501769
trace_id=TRACE_ID,
@@ -2045,7 +2064,7 @@ def test_DD_TRACE_PROPAGATION_STYLE_EXTRACT_overrides_DD_TRACE_PROPAGATION_STYLE
20452064
Context(
20462065
trace_id=13088165645273925489,
20472066
span_id=5678,
2048-
meta={"_dd.origin": "synthetics"},
2067+
meta={"_dd.origin": "synthetics", "_dd.p.dm": "-3"},
20492068
metrics={"_sampling_priority_v1": 1},
20502069
span_links=[
20512070
SpanLink(
@@ -2088,7 +2107,14 @@ def test_DD_TRACE_PROPAGATION_STYLE_EXTRACT_overrides_DD_TRACE_PROPAGATION_STYLE
20882107
Context(
20892108
trace_id=7277407061855694839,
20902109
span_id=5678,
2091-
meta={"_dd.origin": "synthetics", "tracestate": "dd=s:2;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE"},
2110+
# it's weird that both _dd.p.dm and tracestate.t.dm are set here. as far as i know, this is the expected
2111+
# behavior for this chaotic set of headers, specifically when STYLE_DATADOG precedes STYLE_W3C_TRACECONTEXT
2112+
# in the styles configuration
2113+
meta={
2114+
"_dd.p.dm": "-3",
2115+
"_dd.origin": "synthetics",
2116+
"tracestate": "dd=s:2;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
2117+
},
20922118
metrics={"_sampling_priority_v1": 1},
20932119
span_links=[
20942120
SpanLink(
@@ -2105,7 +2131,7 @@ def test_DD_TRACE_PROPAGATION_STYLE_EXTRACT_overrides_DD_TRACE_PROPAGATION_STYLE
21052131

21062132

21072133
@pytest.mark.parametrize("name,styles,headers,expected_context", FULL_CONTEXT_EXTRACT_FIXTURES)
2108-
def test_mutliple_context_interactions(name, styles, headers, expected_context):
2134+
def test_multiple_context_interactions(name, styles, headers, expected_context):
21092135
with override_global_config(dict(_propagation_style_extract=styles)):
21102136
context = HTTPPropagator.extract(headers)
21112137
assert context == expected_context

0 commit comments

Comments
 (0)