Skip to content

Commit 8fc43f0

Browse files
set thread id
1 parent 285de05 commit 8fc43f0

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

sentry_sdk/integrations/django/asgi.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from sentry_sdk.consts import OP
1818
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1919
from sentry_sdk.scope import should_send_default_pii
20+
from sentry_sdk.traces import StreamedSpan
2021
from sentry_sdk.tracing_utils import has_span_streaming_enabled
2122
from sentry_sdk.utils import (
2223
capture_internal_exceptions,
@@ -169,19 +170,26 @@ def wrap_async_view(callback: "Any") -> "Any":
169170
async def sentry_wrapped_callback(
170171
request: "Any", *args: "Any", **kwargs: "Any"
171172
) -> "Any":
173+
client = sentry_sdk.get_client()
174+
span_streaming = has_span_streaming_enabled(client.options)
172175
current_scope = sentry_sdk.get_current_scope()
173-
if current_scope.transaction is not None:
174-
current_scope.transaction.update_active_thread()
176+
if span_streaming:
177+
current_span = current_scope.streamed_span
178+
if type(current_span) is StreamedSpan:
179+
segment = current_span._segment
180+
segment._update_active_thread()
181+
else:
182+
if current_scope.transaction is not None:
183+
current_scope.transaction.update_active_thread()
175184

176185
sentry_scope = sentry_sdk.get_isolation_scope()
177186
if sentry_scope.profile is not None:
178187
sentry_scope.profile.update_active_thread_id()
179188

180-
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
189+
integration = client.get_integration(DjangoIntegration)
181190
if not integration or not integration.middleware_spans:
182191
return await callback(request, *args, **kwargs)
183192

184-
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
185193
if span_streaming:
186194
with sentry_sdk.traces.start_span(
187195
name=request.resolver_match.view_name,

sentry_sdk/integrations/django/views.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import sentry_sdk
55
from sentry_sdk.consts import OP
6+
from sentry_sdk.traces import StreamedSpan
67
from sentry_sdk.tracing_utils import has_span_streaming_enabled
78

89
if TYPE_CHECKING:
@@ -84,21 +85,28 @@ def _wrap_sync_view(callback: "Any") -> "Any":
8485

8586
@functools.wraps(callback)
8687
def sentry_wrapped_callback(request: "Any", *args: "Any", **kwargs: "Any") -> "Any":
88+
client = sentry_sdk.get_client()
89+
span_streaming = has_span_streaming_enabled(client.options)
8790
current_scope = sentry_sdk.get_current_scope()
88-
if current_scope.transaction is not None:
89-
current_scope.transaction.update_active_thread()
91+
if span_streaming:
92+
current_span = current_scope.streamed_span
93+
if type(current_span) is StreamedSpan:
94+
segment = current_span._segment
95+
segment._update_active_thread()
96+
else:
97+
if current_scope.transaction is not None:
98+
current_scope.transaction.update_active_thread()
9099

91100
sentry_scope = sentry_sdk.get_isolation_scope()
92101
# set the active thread id to the handler thread for sync views
93102
# this isn't necessary for async views since that runs on main
94103
if sentry_scope.profile is not None:
95104
sentry_scope.profile.update_active_thread_id()
96105

97-
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
106+
integration = client.get_integration(DjangoIntegration)
98107
if not integration or not integration.middleware_spans:
99108
return callback(request, *args, **kwargs)
100109

101-
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
102110
if span_streaming:
103111
with sentry_sdk.traces.start_span(
104112
name=request.resolver_match.view_name,

tests/integrations/django/asgi/test_asgi.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,13 @@ async def test_active_thread_id(
228228

229229
data = json.loads(response["body"])
230230

231-
spans = [item for item in items if item.type == "span"]
231+
sentry_sdk.flush()
232+
spans = [item.payload for item in items if item.type == "span"]
232233

233234
for span in spans:
234-
trace_context = span["contexts"]["trace"]
235-
assert str(data["active"]) == trace_context["attributes"]["thread.id"]
235+
if span["is_segment"] is False:
236+
continue
237+
assert str(data["active"]) == span["attributes"]["thread.id"]
236238
else:
237239
with mock.patch(
238240
"sentry_sdk.profiler.transaction_profiler.PROFILE_MINIMUM_SAMPLES", 0

0 commit comments

Comments
 (0)