Skip to content

Commit d47e097

Browse files
committed
adding the parent sampled flag
1 parent 3153c60 commit d47e097

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

tests/tracing/test_trace_propagation.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
INCOMING_TRACE_ID = "771a43a4192642f0b136d5159a501700"
99
INCOMING_HEADERS = {
10-
"sentry-trace": f"{INCOMING_TRACE_ID}-1234567890abcdef-1",
10+
"sentry-trace": f"{INCOMING_TRACE_ID}-1234567890abcdef",
1111
"baggage": (
1212
f"sentry-trace_id={INCOMING_TRACE_ID}, "
1313
"sentry-public_key=frontendpublickey,"
1414
"sentry-sample_rate=0.01337,"
15-
"sentry-sampled=true,"
1615
"sentry-release=myfrontend,"
1716
"sentry-environment=bird,"
1817
"sentry-transaction=bar"
@@ -49,7 +48,7 @@ def mock_putheader_fn(self, header, value):
4948
USE_DEFAULT_TRACES_SAMPLE_RATE,
5049
None,
5150
0,
52-
1.0,
51+
1,
5352
],
5453
ids=[
5554
"traces_sample_rate=DEFAULT",
@@ -98,7 +97,7 @@ def test_no_incoming_trace_and_trace_propagation_targets_matching(
9897
USE_DEFAULT_TRACES_SAMPLE_RATE,
9998
None,
10099
0,
101-
1.0,
100+
1,
102101
],
103102
ids=[
104103
"traces_sample_rate=DEFAULT",
@@ -149,7 +148,7 @@ def test_no_incoming_trace_and_trace_propagation_targets_not_matching(
149148
USE_DEFAULT_TRACES_SAMPLE_RATE,
150149
None,
151150
0,
152-
1.0,
151+
1,
153152
],
154153
ids=[
155154
"traces_sample_rate=DEFAULT",
@@ -181,15 +180,29 @@ def test_with_incoming_trace_and_trace_propagation_targets_matching(
181180

182181
events = capture_events()
183182

184-
with sentry_sdk.continue_trace(INCOMING_HEADERS):
183+
incoming_headers = INCOMING_HEADERS.copy()
184+
if incoming_parent_sampled != "deferred":
185+
incoming_headers["sentry-trace"] += f"-{incoming_parent_sampled}"
186+
incoming_headers[
187+
"baggage"
188+
] += f',sentry-sampled={"true" if incoming_parent_sampled == "1" else "false"}' # noqa: E231
189+
190+
print("~~~~~~~~~~~~~~~~~~~~")
191+
print(incoming_headers)
192+
print("~~~~~~~~~~~~~~~~~~~~")
193+
with sentry_sdk.continue_trace(incoming_headers):
185194
with sentry_sdk.start_span(op="test", name="test"):
186195
requests.get("http://example.com")
187196

188197
# CHECK if performance data (a transaction/span) is sent to Sentry
189198
if traces_sample_rate == 1:
190199
assert len(events) == 1
191-
else:
200+
elif traces_sample_rate == 0:
201+
assert len(events) == 1
202+
elif traces_sample_rate in (None, USE_DEFAULT_TRACES_SAMPLE_RATE):
192203
assert len(events) == 0
204+
else:
205+
raise AssertionError(f"Invalid traces_sample_rate: {traces_sample_rate}")
193206

194207
outgoing_request_headers = {key: value for key, value in _mock_putheader}
195208

@@ -214,7 +227,7 @@ def test_with_incoming_trace_and_trace_propagation_targets_matching(
214227
USE_DEFAULT_TRACES_SAMPLE_RATE,
215228
None,
216229
0,
217-
1.0,
230+
1,
218231
],
219232
ids=[
220233
"traces_sample_rate=DEFAULT",
@@ -250,13 +263,22 @@ def test_with_incoming_trace_and_trace_propagation_targets_not_matching(
250263

251264
events = capture_events()
252265

253-
with sentry_sdk.continue_trace(INCOMING_HEADERS):
266+
incoming_headers = INCOMING_HEADERS.copy()
267+
if incoming_parent_sampled != "deferred":
268+
incoming_headers["sentry-trace"] += f"-{incoming_parent_sampled}"
269+
incoming_headers[
270+
"baggage"
271+
] += f',sentry-sampled={"true" if incoming_parent_sampled == "1" else "false"}' # noqa: E231
272+
273+
with sentry_sdk.continue_trace(incoming_headers):
254274
with sentry_sdk.start_span(op="test", name="test"):
255275
requests.get("http://example.com")
256276

257277
# CHECK if performance data (a transaction/span) is sent to Sentry
258278
if traces_sample_rate == 1:
259279
assert len(events) == 1
280+
elif traces_sample_rate == 0:
281+
assert len(events) == 1
260282
else:
261283
assert len(events) == 0
262284

0 commit comments

Comments
 (0)