Skip to content

Commit 17e0348

Browse files
authored
ref: Use old sampling context format in span streaming (#6848)
### Description Reverting `sampling_context` to look like it used to in transaction-based tracing. Caveat: There's a bunch of keys that are not populated in span streaming, mostly because they're not known at span start (`span_id`, `sampled`, etc.). I've kept them in as `None` as that's how the legacy path did it, too (rather than omitting them). Note: When bumping the sdk version in sentry and seer to a version that has this change, the sampling_context code needs to change there, too. #### Issues resolves #6846 #### Reminders - Please add tests to validate your changes, and lint your code using `uv run ruff`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent 2e09497 commit 17e0348

5 files changed

Lines changed: 58 additions & 67 deletions

File tree

sentry_sdk/tracing_utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,14 +1605,26 @@ def _make_sampling_decision(
16051605
# If there's a traces_sampler, use that; otherwise use traces_sample_rate
16061606
traces_sampler_defined = callable(client.options.get("traces_sampler"))
16071607
if traces_sampler_defined:
1608+
if attributes is not None:
1609+
attributes = dict(attributes)
1610+
else:
1611+
attributes = {}
1612+
16081613
sampling_context = {
1609-
"span_context": {
1610-
"name": name,
1614+
"transaction_context": {
16111615
"trace_id": propagation_context.trace_id,
1616+
"span_id": None,
16121617
"parent_span_id": propagation_context.parent_span_id,
1613-
"parent_sampled": propagation_context.parent_sampled,
1614-
"attributes": dict(attributes) if attributes else {},
1618+
"op": attributes.get("sentry.op"),
1619+
"name": name,
1620+
"description": name,
1621+
"start_timestamp": None,
1622+
"timestamp": None,
1623+
"source": attributes.get("sentry.segment.name.source"),
1624+
"sampled": None,
1625+
"data": attributes,
16151626
},
1627+
"parent_sampled": propagation_context.parent_sampled,
16161628
}
16171629

16181630
if propagation_context.custom_sampling_context:

tests/integrations/asgi/test_asgi.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -944,23 +944,13 @@ async def test_transaction_name_in_traces_sampler(
944944
"""
945945

946946
def dummy_traces_sampler(sampling_context):
947-
if span_streaming:
948-
assert sampling_context["span_context"]["name"] == expected_transaction_name
949-
assert (
950-
sampling_context["span_context"]["attributes"][
951-
"sentry.segment.name.source"
952-
]
953-
== expected_transaction_source
954-
)
955-
else:
956-
assert (
957-
sampling_context["transaction_context"]["name"]
958-
== expected_transaction_name
959-
)
960-
assert (
961-
sampling_context["transaction_context"]["source"]
962-
== expected_transaction_source
963-
)
947+
assert (
948+
sampling_context["transaction_context"]["name"] == expected_transaction_name
949+
)
950+
assert (
951+
sampling_context["transaction_context"]["source"]
952+
== expected_transaction_source
953+
)
964954

965955
sentry_init(
966956
traces_sampler=dummy_traces_sampler,

tests/integrations/wsgi/test_wsgi.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -488,37 +488,23 @@ def app(environ, start_response):
488488

489489
client.get("/dogs/are/great/")
490490

491-
if span_streaming:
492-
traces_sampler.assert_any_call(
493-
DictionaryContaining(
494-
{
495-
"span_context": DictionaryContaining(
496-
{
497-
"name": "generic WSGI request",
498-
},
499-
),
500-
"wsgi_environ": DictionaryContaining(
501-
{
502-
"PATH_INFO": "/dogs/are/great/",
503-
"REQUEST_METHOD": "GET",
504-
},
505-
),
506-
}
507-
)
508-
)
509-
else:
510-
traces_sampler.assert_any_call(
511-
DictionaryContaining(
512-
{
513-
"wsgi_environ": DictionaryContaining(
514-
{
515-
"PATH_INFO": "/dogs/are/great/",
516-
"REQUEST_METHOD": "GET",
517-
},
518-
),
519-
}
520-
)
491+
traces_sampler.assert_any_call(
492+
DictionaryContaining(
493+
{
494+
"transaction_context": DictionaryContaining(
495+
{
496+
"name": "generic WSGI request",
497+
},
498+
),
499+
"wsgi_environ": DictionaryContaining(
500+
{
501+
"PATH_INFO": "/dogs/are/great/",
502+
"REQUEST_METHOD": "GET",
503+
},
504+
),
505+
}
521506
)
507+
)
522508

523509

524510
@pytest.mark.parametrize("span_streaming", [True, False])

tests/tracing/test_sampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def traces_sampler(sampling_context):
446446

447447
def test_custom_sampling_context_update_to_context_value_persists(sentry_init):
448448
def traces_sampler(sampling_context):
449-
if sampling_context["span_context"]["attributes"]["first"] is True:
449+
if sampling_context["transaction_context"]["data"]["first"] is True:
450450
assert sampling_context["custom_value"] == 1
451451
else:
452452
assert sampling_context["custom_value"] == 2

tests/tracing/test_span_streaming.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def test_span_sampled_when_created(sentry_init, capture_items):
104104
# at start_span() time
105105

106106
def traces_sampler(sampling_context):
107-
assert "delayed_attribute" not in sampling_context["span_context"]["attributes"]
107+
assert (
108+
"delayed_attribute" not in sampling_context["transaction_context"]["data"]
109+
)
108110
return 1.0
109111

110112
sentry_init(
@@ -153,10 +155,11 @@ def test_start_span_attributes(sentry_init, capture_items):
153155

154156
def test_start_span_attributes_in_traces_sampler(sentry_init, capture_items):
155157
def traces_sampler(sampling_context):
156-
assert "attributes" in sampling_context["span_context"]
157-
assert "my_attribute" in sampling_context["span_context"]["attributes"]
158+
assert "data" in sampling_context["transaction_context"]
159+
assert "my_attribute" in sampling_context["transaction_context"]["data"]
158160
assert (
159-
sampling_context["span_context"]["attributes"]["my_attribute"] == "my_value"
161+
sampling_context["transaction_context"]["data"]["my_attribute"]
162+
== "my_value"
160163
)
161164
return 1.0
162165

@@ -188,16 +191,16 @@ def test_sampling_context(sentry_init, capture_items):
188191
def traces_sampler(sampling_context):
189192
nonlocal received_trace_id
190193

191-
assert "trace_id" in sampling_context["span_context"]
192-
received_trace_id = sampling_context["span_context"]["trace_id"]
194+
assert "trace_id" in sampling_context["transaction_context"]
195+
received_trace_id = sampling_context["transaction_context"]["trace_id"]
193196

194-
assert "parent_span_id" in sampling_context["span_context"]
195-
assert sampling_context["span_context"]["parent_span_id"] is None
197+
assert "parent_span_id" in sampling_context["transaction_context"]
198+
assert sampling_context["transaction_context"]["parent_span_id"] is None
196199

197-
assert "parent_sampled" in sampling_context["span_context"]
198-
assert sampling_context["span_context"]["parent_sampled"] is None
200+
assert "parent_sampled" in sampling_context
201+
assert sampling_context["parent_sampled"] is None
199202

200-
assert "attributes" in sampling_context["span_context"]
203+
assert "data" in sampling_context["transaction_context"]
201204

202205
return 1.0
203206

@@ -428,10 +431,10 @@ class Class:
428431

429432
def test_traces_sampler_drops_span(sentry_init, capture_items):
430433
def traces_sampler(sampling_context):
431-
assert "attributes" in sampling_context["span_context"]
432-
assert "drop" in sampling_context["span_context"]["attributes"]
434+
assert "data" in sampling_context["transaction_context"]
435+
assert "drop" in sampling_context["transaction_context"]["data"]
433436

434-
if sampling_context["span_context"]["attributes"]["drop"] is True:
437+
if sampling_context["transaction_context"]["data"]["drop"] is True:
435438
return 0.0
436439

437440
return 1.0
@@ -465,7 +468,7 @@ def test_traces_sampler_called_once_per_segment(sentry_init):
465468
def traces_sampler(sampling_context):
466469
nonlocal traces_sampler_called, span_name_in_traces_sampler
467470
traces_sampler_called += 1
468-
span_name_in_traces_sampler = sampling_context["span_context"]["name"]
471+
span_name_in_traces_sampler = sampling_context["transaction_context"]["name"]
469472
return 1.0
470473

471474
sentry_init(

0 commit comments

Comments
 (0)