Skip to content

Commit 8417165

Browse files
feat(starlette): Set transaction name when middleware spans are disabled (#5223)
Set the name of Starlette and FastAPI server transactions independently of the `middleware_spans` option.
1 parent 01f4edc commit 8417165

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

sentry_sdk/integrations/starlette.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,23 @@ def _enable_span_for_middleware(middleware_class):
144144
async def _create_span_call(app, scope, receive, send, **kwargs):
145145
# type: (Any, Dict[str, Any], Callable[[], Awaitable[Dict[str, Any]]], Callable[[Dict[str, Any]], Awaitable[None]], Any) -> None
146146
integration = sentry_sdk.get_client().get_integration(StarletteIntegration)
147-
if integration is None or not integration.middleware_spans:
147+
if integration is None:
148148
return await old_call(app, scope, receive, send, **kwargs)
149149

150-
middleware_name = app.__class__.__name__
151-
152150
# Update transaction name with middleware name
153151
name, source = _get_transaction_from_middleware(app, scope, integration)
152+
154153
if name is not None:
155154
sentry_sdk.get_current_scope().set_transaction_name(
156155
name,
157156
source=source,
158157
)
159158

159+
if not integration.middleware_spans:
160+
return await old_call(app, scope, receive, send, **kwargs)
161+
162+
middleware_name = app.__class__.__name__
163+
160164
with sentry_sdk.start_span(
161165
op=OP.MIDDLEWARE_STARLETTE,
162166
name=middleware_name,

tests/integrations/fastapi/test_fastapi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def dummy_traces_sampler(sampling_context):
469469
client.get(request_url)
470470

471471

472+
@pytest.mark.parametrize("middleware_spans", [False, True])
472473
@pytest.mark.parametrize(
473474
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
474475
[
@@ -488,6 +489,7 @@ def dummy_traces_sampler(sampling_context):
488489
)
489490
def test_transaction_name_in_middleware(
490491
sentry_init,
492+
middleware_spans,
491493
request_url,
492494
transaction_style,
493495
expected_transaction_name,
@@ -500,8 +502,12 @@ def test_transaction_name_in_middleware(
500502
sentry_init(
501503
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
502504
integrations=[
503-
StarletteIntegration(transaction_style=transaction_style),
504-
FastApiIntegration(transaction_style=transaction_style),
505+
StarletteIntegration(
506+
transaction_style=transaction_style, middleware_spans=middleware_spans
507+
),
508+
FastApiIntegration(
509+
transaction_style=transaction_style, middleware_spans=middleware_spans
510+
),
505511
],
506512
traces_sample_rate=1.0,
507513
)

tests/integrations/starlette/test_starlette.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ def dummy_traces_sampler(sampling_context):
10991099
client.get(request_url)
11001100

11011101

1102+
@pytest.mark.parametrize("middleware_spans", [False, True])
11021103
@pytest.mark.parametrize(
11031104
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
11041105
[
@@ -1118,6 +1119,7 @@ def dummy_traces_sampler(sampling_context):
11181119
)
11191120
def test_transaction_name_in_middleware(
11201121
sentry_init,
1122+
middleware_spans,
11211123
request_url,
11221124
transaction_style,
11231125
expected_transaction_name,
@@ -1130,7 +1132,9 @@ def test_transaction_name_in_middleware(
11301132
sentry_init(
11311133
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
11321134
integrations=[
1133-
StarletteIntegration(transaction_style=transaction_style),
1135+
StarletteIntegration(
1136+
transaction_style=transaction_style, middleware_spans=middleware_spans
1137+
),
11341138
],
11351139
traces_sample_rate=1.0,
11361140
)

0 commit comments

Comments
 (0)