Skip to content

Commit 6eacecc

Browse files
ericapisaniclaude
andcommitted
fix(starlette): Set request body data on segment under streaming
Under span streaming with middleware_spans=True, sentry_sdk.get_current_span() returns the innermost middleware span rather than the segment, so http.request.body.data was being attached to a middleware child span. Route the attribute through current_span._segment so it always lands on the segment, mirroring the _update_active_thread pattern in _sentry_sync_func. Also deduplicate the sentry_sdk.get_client() call in _sentry_async_func, and parametrize the three body-data streaming tests over middleware_spans=[False, True]. Refs PY-2362 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 50352ea commit 6eacecc

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

sentry_sdk/integrations/starlette.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,8 @@ def _sentry_request_response(func: "Callable[[Any], Any]") -> "ASGIApp":
469469
if is_coroutine:
470470

471471
async def _sentry_async_func(*args: "Any", **kwargs: "Any") -> "Any":
472-
integration = sentry_sdk.get_client().get_integration(
473-
StarletteIntegration
474-
)
472+
client = sentry_sdk.get_client()
473+
integration = client.get_integration(StarletteIntegration)
475474
if integration is None:
476475
return await old_func(*args, **kwargs)
477476

@@ -511,7 +510,6 @@ def event_processor(
511510
_make_request_event_processor(request, integration)
512511
)
513512

514-
client = sentry_sdk.get_client()
515513
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
516514
if is_span_streaming_enabled:
517515
current_span = sentry_sdk.get_current_span()
@@ -523,7 +521,7 @@ def event_processor(
523521
and not isinstance(current_span, NoOpStreamedSpan)
524522
):
525523
data = info["data"]
526-
current_span.set_attribute(
524+
current_span._segment.set_attribute(
527525
"http.request.body.data",
528526
_serialize_body_data(data),
529527
)

tests/integrations/starlette/test_starlette.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,10 +1066,13 @@ async def _handler(request):
10661066
)
10671067

10681068

1069-
def test_request_body_data_scrubs_pii_span_streaming(sentry_init, capture_items):
1069+
@pytest.mark.parametrize("middleware_spans", [False, True])
1070+
def test_request_body_data_scrubs_pii_span_streaming(
1071+
sentry_init, capture_items, middleware_spans
1072+
):
10701073
sentry_init(
10711074
auto_enabling_integrations=False,
1072-
integrations=[StarletteIntegration()],
1075+
integrations=[StarletteIntegration(middleware_spans=middleware_spans)],
10731076
traces_sample_rate=1.0,
10741077
_experiments={"trace_lifecycle": "stream"},
10751078
)
@@ -1107,12 +1110,13 @@ async def _read_json(request):
11071110
STARLETTE_VERSION < (0, 21),
11081111
reason="Requires Starlette >= 0.21, because earlier versions use a requests-based TestClient which does not support the 'content' kwarg",
11091112
)
1113+
@pytest.mark.parametrize("middleware_spans", [False, True])
11101114
def test_request_body_data_annotated_value_top_level_span_streaming(
1111-
sentry_init, capture_items
1115+
sentry_init, capture_items, middleware_spans
11121116
):
11131117
sentry_init(
11141118
auto_enabling_integrations=False,
1115-
integrations=[StarletteIntegration()],
1119+
integrations=[StarletteIntegration(middleware_spans=middleware_spans)],
11161120
traces_sample_rate=1.0,
11171121
_experiments={"trace_lifecycle": "stream"},
11181122
)
@@ -1140,14 +1144,15 @@ async def _read_body(request):
11401144
assert "!raw" in attr
11411145

11421146

1147+
@pytest.mark.parametrize("middleware_spans", [False, True])
11431148
def test_request_body_data_annotated_value_nested_span_streaming(
1144-
sentry_init, capture_items
1149+
sentry_init, capture_items, middleware_spans
11451150
):
11461151
pytest.importorskip("multipart")
11471152

11481153
sentry_init(
11491154
auto_enabling_integrations=False,
1150-
integrations=[StarletteIntegration()],
1155+
integrations=[StarletteIntegration(middleware_spans=middleware_spans)],
11511156
traces_sample_rate=1.0,
11521157
_experiments={"trace_lifecycle": "stream"},
11531158
)

0 commit comments

Comments
 (0)