Skip to content

Commit 01f4edc

Browse files
fix(django): Set active thread ID when middleware spans are disabled (#5220)
Always set the active thread ID on Django server transactions, independent of the `middleware_spans` option.
1 parent d664465 commit 01f4edc

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

sentry_sdk/integrations/django/asgi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ async def sentry_wrapped_callback(request, *args, **kwargs):
180180
if sentry_scope.profile is not None:
181181
sentry_scope.profile.update_active_thread_id()
182182

183+
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
184+
if not integration or not integration.middleware_spans:
185+
return await callback(request, *args, **kwargs)
186+
183187
with sentry_sdk.start_span(
184188
op=OP.VIEW_RENDER,
185189
name=request.resolver_match.view_name,

sentry_sdk/integrations/django/views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def sentry_patched_make_view_atomic(self, *args, **kwargs):
4949
# efficient way to wrap views (or build a cache?)
5050

5151
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
52-
if integration is not None and integration.middleware_spans:
52+
if integration is not None:
5353
is_async_view = (
5454
iscoroutinefunction is not None
5555
and wrap_async_view is not None
@@ -86,6 +86,10 @@ def sentry_wrapped_callback(request, *args, **kwargs):
8686
if sentry_scope.profile is not None:
8787
sentry_scope.profile.update_active_thread_id()
8888

89+
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
90+
if not integration or not integration.middleware_spans:
91+
return callback(request, *args, **kwargs)
92+
8993
with sentry_sdk.start_span(
9094
op=OP.VIEW_RENDER,
9195
name=request.resolver_match.view_name,

tests/integrations/django/asgi/test_asgi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,25 @@ async def test_async_views(sentry_init, capture_events, application):
118118

119119
@pytest.mark.parametrize("application", APPS)
120120
@pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"])
121+
@pytest.mark.parametrize("middleware_spans", [False, True])
121122
@pytest.mark.asyncio
122123
@pytest.mark.forked
123124
@pytest.mark.skipif(
124125
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
125126
)
126127
async def test_active_thread_id(
127-
sentry_init, capture_envelopes, teardown_profiling, endpoint, application
128+
sentry_init,
129+
capture_envelopes,
130+
teardown_profiling,
131+
endpoint,
132+
application,
133+
middleware_spans,
128134
):
129135
with mock.patch(
130136
"sentry_sdk.profiler.transaction_profiler.PROFILE_MINIMUM_SAMPLES", 0
131137
):
132138
sentry_init(
133-
integrations=[DjangoIntegration()],
139+
integrations=[DjangoIntegration(middleware_spans=middleware_spans)],
134140
traces_sample_rate=1.0,
135141
profiles_sample_rate=1.0,
136142
)

0 commit comments

Comments
 (0)