Skip to content

Commit 5183394

Browse files
authored
fix(tracer): fix sampling mechanism tag propagation bug (#6579)
This change fixes a bug causing `_dd.p.*` span tags not to be inherited by child spans when there is a process boundary between the parent and child spans. This makes cross-process parenting match the behavior of parenting within a single process with respect to tag inheritance. This change updates all snapshot files and test expectations affected by this change to include expectation of all relevant `_dd.p.*` tags. There are also two style changes that were required to get the CI pre_check step to pass (I'm not sure why that isn't appearing on 1.x).
1 parent b730396 commit 5183394

File tree

249 files changed

+4552
-3410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

249 files changed

+4552
-3410
lines changed

ddtrace/internal/utils/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from typing import Optional
55
from typing import Tuple
66

7+
import six
8+
79

810
class ArgumentError(Exception):
911
"""
@@ -67,3 +69,12 @@ def set_argument_value(
6769
raise ArgumentError("%s (at position %d) is invalid" % (kw, pos))
6870

6971
return args, kwargs
72+
73+
74+
def _get_metas_to_propagate(context):
75+
# type: (Any) -> List[Tuple[str, str]]
76+
metas_to_propagate = []
77+
for k, v in context._meta.items():
78+
if isinstance(k, six.string_types) and k.startswith("_dd.p."):
79+
metas_to_propagate.append((k, v))
80+
return metas_to_propagate

ddtrace/internal/utils/http.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from typing import Tuple
1313
from typing import Union
1414

15-
import six
16-
1715
from ddtrace.constants import USER_ID_KEY
1816
from ddtrace.internal import compat
1917
from ddtrace.internal.compat import parse
@@ -25,6 +23,7 @@
2523
from ddtrace.internal.http import HTTPConnection
2624
from ddtrace.internal.http import HTTPSConnection
2725
from ddtrace.internal.uds import UDSHTTPConnection
26+
from ddtrace.internal.utils import _get_metas_to_propagate
2827
from ddtrace.internal.utils.cache import cached
2928

3029

@@ -173,13 +172,8 @@ def w3c_get_dd_list_member(context):
173172
tags.append("t.usr.id:{}".format(w3c_encode_tag((_W3C_TRACESTATE_INVALID_CHARS_REGEX_VALUE, "_", usr_id))))
174173

175174
current_tags_len = sum(len(i) for i in tags)
176-
for k, v in context._meta.items():
177-
if (
178-
isinstance(k, six.string_types)
179-
and k.startswith("_dd.p.")
180-
# we've already added sampling decision and user id
181-
and k not in [SAMPLING_DECISION_TRACE_TAG_KEY, USER_ID_KEY]
182-
):
175+
for k, v in _get_metas_to_propagate(context):
176+
if k not in [SAMPLING_DECISION_TRACE_TAG_KEY, USER_ID_KEY]:
183177
# for key replace ",", "=", and characters outside the ASCII range 0x20 to 0x7E
184178
# for value replace ",", ";", "~" and characters outside the ASCII range 0x20 to 0x7E
185179
k = k.replace("_dd.p.", "t.")

ddtrace/tracer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ddtrace.internal.processor.endpoint_call_counter import EndpointCallCounterProcessor
1616
from ddtrace.internal.sampling import SpanSamplingRule
1717
from ddtrace.internal.sampling import get_span_sampling_rules
18+
from ddtrace.internal.utils import _get_metas_to_propagate
1819
from ddtrace.settings.peer_service import PeerServiceConfig
1920
from ddtrace.vendor import debtcollector
2021

@@ -718,6 +719,8 @@ def _start_span(
718719

719720
if span._local_root is None:
720721
span._local_root = span
722+
for k, v in _get_metas_to_propagate(context):
723+
span._meta[k] = v
721724
else:
722725
# this is the root span of a new trace
723726
span = Span(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
This fix resolves an issue causing span tags used by the Datadog backend not to be inherited by spans
5+
that exist in a different process from their parents.

tests/contrib/botocore/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,7 @@ def test_kinesis_put_records_json_trace_injection(self):
28532853
@mock_kinesis
28542854
@TracerTestCase.run_in_subprocess(env_overrides=dict(DD_DATA_STREAMS_ENABLED="True"))
28552855
@unittest.skipIf(BOTOCORE_VERSION < (1, 26, 31), "Kinesis didn't support streamARN till 1.26.31")
2856-
@mock.patch('time.time', mock.MagicMock(return_value=1642544540))
2856+
@mock.patch("time.time", mock.MagicMock(return_value=1642544540))
28572857
def test_kinesis_data_streams_enabled_put_records(self):
28582858
# (dict -> json string)[]
28592859
data = json.dumps({"json": "string"})

tests/contrib/django/test_django.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ def test_cache_get_many(test_spans):
12181218
"component": "django",
12191219
"django.cache.backend": "django.core.cache.backends.locmem.LocMemCache",
12201220
"django.cache.key": "missing_key another_key",
1221+
"_dd.p.dm": "-0",
12211222
}
12221223

12231224
assert_dict_issuperset(span_get_many.get_tags(), expected_meta)
@@ -1419,12 +1420,14 @@ def test_cached_view(client, test_spans):
14191420
"django.cache.key": (
14201421
"views.decorators.cache.cache_page..GET.03cdc1cc4aab71b038a6764e5fcabb82.d41d8cd98f00b204e9800998ecf8..."
14211422
),
1423+
"_dd.p.dm": "-0",
14221424
}
14231425

14241426
expected_meta_header = {
14251427
"component": "django",
14261428
"django.cache.backend": "django.core.cache.backends.locmem.LocMemCache",
14271429
"django.cache.key": "views.decorators.cache.cache_header..03cdc1cc4aab71b038a6764e5fcabb82.en-us",
1430+
"_dd.p.dm": "-0",
14281431
}
14291432

14301433
assert span_view.get_tags() == expected_meta_view
@@ -1463,6 +1466,7 @@ def test_cached_template(client, test_spans):
14631466
"component": "django",
14641467
"django.cache.backend": "django.core.cache.backends.locmem.LocMemCache",
14651468
"django.cache.key": "template.cache.users_list.d41d8cd98f00b204e9800998ecf8427e",
1469+
"_dd.p.dm": "-0",
14661470
}
14671471

14681472
assert span_template_cache.get_tags() == expected_meta

tests/contrib/flask/test_blueprint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test():
103103
self.assertEqual(span.service, "flask")
104104
self.assertEqual(span.name, "bp.test")
105105
self.assertEqual(span.resource, "/")
106-
self.assertEqual(span.get_tags(), {"component": "flask"})
106+
self.assertEqual(span.get_tags(), {"component": "flask", "_dd.p.dm": "-0"})
107107

108108
def test_blueprint_request_pin_override(self):
109109
"""
@@ -130,7 +130,7 @@ def test():
130130
self.assertEqual(span.service, "flask-bp")
131131
self.assertEqual(span.name, "bp.test")
132132
self.assertEqual(span.resource, "/")
133-
self.assertEqual(span.get_tags(), {"component": "flask"})
133+
self.assertEqual(span.get_tags(), {"component": "flask", "_dd.p.dm": "-0"})
134134

135135
def test_blueprint_request_pin_disabled(self):
136136
"""

tests/contrib/flask/test_errorhandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from . import BaseFlaskTestCase
77

88

9-
EXPECTED_METADATA = {"component": "flask"}
9+
EXPECTED_METADATA = {"component": "flask", "_dd.p.dm": "-0"}
1010

1111

1212
class FlaskErrorhandlerTestCase(BaseFlaskTestCase):

tests/contrib/flask/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
if PY2:
1414
base_exception_name = "exceptions.Exception"
1515

16-
EXPECTED_METADATA = {"component": "flask"}
16+
EXPECTED_METADATA = {"component": "flask", "_dd.p.dm": "-0"}
1717

1818

1919
class FlaskViewTestCase(BaseFlaskTestCase):

tests/contrib/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import unittest
88

99
from ddtrace.vendor import wrapt
10-
from tests.subprocesstest import run_in_subprocess
1110
from tests.subprocesstest import SubprocessTestCase
11+
from tests.subprocesstest import run_in_subprocess
1212
from tests.utils import call_program
1313

1414

0 commit comments

Comments
 (0)