Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,23 @@ def _enable_span_for_middleware(middleware_class):
async def _create_span_call(app, scope, receive, send, **kwargs):
# type: (Any, Dict[str, Any], Callable[[], Awaitable[Dict[str, Any]]], Callable[[Dict[str, Any]], Awaitable[None]], Any) -> None
integration = sentry_sdk.get_client().get_integration(StarletteIntegration)
if integration is None or not integration.middleware_spans:
if integration is None:
return await old_call(app, scope, receive, send, **kwargs)

middleware_name = app.__class__.__name__

# Update transaction name with middleware name
name, source = _get_transaction_from_middleware(app, scope, integration)

if name is not None:
sentry_sdk.get_current_scope().set_transaction_name(
name,
source=source,
)

if not integration.middleware_spans:
return await old_call(app, scope, receive, send, **kwargs)

middleware_name = app.__class__.__name__

with sentry_sdk.start_span(
op=OP.MIDDLEWARE_STARLETTE,
name=middleware_name,
Expand Down
10 changes: 8 additions & 2 deletions tests/integrations/fastapi/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ def dummy_traces_sampler(sampling_context):
client.get(request_url)


@pytest.mark.parametrize("middleware_spans", [False, True])
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
[
Expand All @@ -488,6 +489,7 @@ def dummy_traces_sampler(sampling_context):
)
def test_transaction_name_in_middleware(
sentry_init,
middleware_spans,
request_url,
transaction_style,
expected_transaction_name,
Expand All @@ -500,8 +502,12 @@ def test_transaction_name_in_middleware(
sentry_init(
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[
StarletteIntegration(transaction_style=transaction_style),
FastApiIntegration(transaction_style=transaction_style),
StarletteIntegration(
transaction_style=transaction_style, middleware_spans=middleware_spans
),
FastApiIntegration(
transaction_style=transaction_style, middleware_spans=middleware_spans
),
],
traces_sample_rate=1.0,
)
Expand Down
6 changes: 5 additions & 1 deletion tests/integrations/starlette/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ def dummy_traces_sampler(sampling_context):
client.get(request_url)


@pytest.mark.parametrize("middleware_spans", [False, True])
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
[
Expand All @@ -1118,6 +1119,7 @@ def dummy_traces_sampler(sampling_context):
)
def test_transaction_name_in_middleware(
sentry_init,
middleware_spans,
request_url,
transaction_style,
expected_transaction_name,
Expand All @@ -1130,7 +1132,9 @@ def test_transaction_name_in_middleware(
sentry_init(
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[
StarletteIntegration(transaction_style=transaction_style),
StarletteIntegration(
transaction_style=transaction_style, middleware_spans=middleware_spans
),
],
traces_sample_rate=1.0,
)
Expand Down