Skip to content

Commit 806992f

Browse files
committed
update active thread id
1 parent 78ccdca commit 806992f

File tree

9 files changed

+71
-29
lines changed

9 files changed

+71
-29
lines changed

sentry_sdk/integrations/django/asgi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,13 @@ def wrap_async_view(callback):
172172
@functools.wraps(callback)
173173
async def sentry_wrapped_callback(request, *args, **kwargs):
174174
# type: (Any, *Any, **Any) -> Any
175+
current_scope = sentry_sdk.get_current_scope()
176+
if current_scope.transaction is not None:
177+
current_scope.transaction.update_active_thread()
178+
175179
sentry_scope = sentry_sdk.get_isolation_scope()
176180
if sentry_scope.profile is not None:
177181
sentry_scope.profile.update_active_thread_id()
178-
if sentry_scope.transaction is not None:
179-
sentry_scope.transaction.update_active_thread()
180182

181183
with sentry_sdk.start_span(
182184
op=OP.VIEW_RENDER,

sentry_sdk/integrations/django/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ def _wrap_sync_view(callback):
7676
@functools.wraps(callback)
7777
def sentry_wrapped_callback(request, *args, **kwargs):
7878
# type: (Any, *Any, **Any) -> Any
79+
current_scope = sentry_sdk.get_current_scope()
80+
if current_scope.transaction is not None:
81+
current_scope.transaction.update_active_thread()
82+
7983
sentry_scope = sentry_sdk.get_isolation_scope()
8084
# set the active thread id to the handler thread for sync views
8185
# this isn't necessary for async views since that runs on main
8286
if sentry_scope.profile is not None:
8387
sentry_scope.profile.update_active_thread_id()
84-
if sentry_scope.transaction is not None:
85-
sentry_scope.transaction.update_active_thread()
8688

8789
with sentry_sdk.start_span(
8890
op=OP.VIEW_RENDER,

sentry_sdk/integrations/fastapi.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ def _sentry_get_request_handler(*args, **kwargs):
8888
@wraps(old_call)
8989
def _sentry_call(*args, **kwargs):
9090
# type: (*Any, **Any) -> Any
91+
current_scope = sentry_sdk.get_current_scope()
92+
if current_scope.transaction is not None:
93+
current_scope.transaction.update_active_thread()
94+
9195
sentry_scope = sentry_sdk.get_isolation_scope()
9296
if sentry_scope.profile is not None:
9397
sentry_scope.profile.update_active_thread_id()
94-
if sentry_scope.transaction is not None:
95-
sentry_scope.transaction.update_active_thread()
98+
9699
return old_call(*args, **kwargs)
97100

98101
dependant.call = _sentry_call

sentry_sdk/integrations/quart.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import inspect
3-
import threading
43
from functools import wraps
54

65
import sentry_sdk
@@ -122,11 +121,13 @@ def decorator(old_func):
122121
@ensure_integration_enabled(QuartIntegration, old_func)
123122
def _sentry_func(*args, **kwargs):
124123
# type: (*Any, **Any) -> Any
125-
scope = sentry_sdk.get_isolation_scope()
126-
if scope.profile is not None:
127-
scope.profile.active_thread_id = (
128-
threading.current_thread().ident
129-
)
124+
current_scope = sentry_sdk.get_current_scope()
125+
if current_scope.transaction is not None:
126+
current_scope.transaction.update_active_thread()
127+
128+
sentry_scope = sentry_sdk.get_isolation_scope()
129+
if sentry_scope.profile is not None:
130+
sentry_scope.profile.update_active_thread_id()
130131

131132
return old_func(*args, **kwargs)
132133

sentry_sdk/integrations/starlette.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,13 @@ def _sentry_sync_func(*args, **kwargs):
487487
if integration is None:
488488
return old_func(*args, **kwargs)
489489

490-
sentry_scope = sentry_sdk.get_isolation_scope()
490+
current_scope = sentry_sdk.get_current_scope()
491+
if current_scope.transaction is not None:
492+
current_scope.transaction.update_active_thread()
491493

494+
sentry_scope = sentry_sdk.get_isolation_scope()
492495
if sentry_scope.profile is not None:
493496
sentry_scope.profile.update_active_thread_id()
494-
if sentry_scope.transaction is not None:
495-
sentry_scope.transaction.update_active_thread()
496497

497498
request = args[0]
498499

tests/integrations/django/asgi/test_asgi.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def test_active_thread_id(sentry_init, capture_envelopes, endpoint, applic
111111
sentry_init(
112112
integrations=[DjangoIntegration()],
113113
traces_sample_rate=1.0,
114-
_experiments={"profiles_sample_rate": 1.0},
114+
profiles_sample_rate=1.0,
115115
)
116116

117117
envelopes = capture_envelopes()
@@ -128,11 +128,19 @@ async def test_active_thread_id(sentry_init, capture_envelopes, endpoint, applic
128128

129129
data = json.loads(response["body"])
130130

131-
for profile in profiles:
132-
transactions = profile.payload.json["transactions"]
131+
for item in profiles:
132+
transactions = item.payload.json["transactions"]
133133
assert len(transactions) == 1
134134
assert str(data["active"]) == transactions[0]["active_thread_id"]
135135

136+
transactions = [item for item in envelopes[0].items if item.type == "transaction"]
137+
assert len(transactions) == 1
138+
139+
for item in transactions:
140+
transaction = item.payload.json
141+
trace_context = transaction["contexts"]["trace"]
142+
assert str(data["active"]) == trace_context["data"]["thread.id"]
143+
136144

137145
@pytest.mark.asyncio
138146
@pytest.mark.forked

tests/integrations/fastapi/test_fastapi.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def test_legacy_setup(
184184
def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, endpoint):
185185
sentry_init(
186186
traces_sample_rate=1.0,
187-
_experiments={"profiles_sample_rate": 1.0},
187+
profiles_sample_rate=1.0,
188188
)
189189
app = fastapi_app_factory()
190190
asgi_app = SentryAsgiMiddleware(app)
@@ -203,11 +203,19 @@ def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, en
203203
profiles = [item for item in envelopes[0].items if item.type == "profile"]
204204
assert len(profiles) == 1
205205

206-
for profile in profiles:
207-
transactions = profile.payload.json["transactions"]
206+
for item in profiles:
207+
transactions = item.payload.json["transactions"]
208208
assert len(transactions) == 1
209209
assert str(data["active"]) == transactions[0]["active_thread_id"]
210210

211+
transactions = [item for item in envelopes[0].items if item.type == "transaction"]
212+
assert len(transactions) == 1
213+
214+
for item in transactions:
215+
transaction = item.payload.json
216+
trace_context = transaction["contexts"]["trace"]
217+
assert str(data["active"]) == trace_context["data"]["thread.id"]
218+
211219

212220
@pytest.mark.asyncio
213221
async def test_original_request_not_scrubbed(sentry_init, capture_events):

tests/integrations/quart/test_quart.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,11 @@ async def dispatch_request(self):
523523

524524

525525
@pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"])
526+
@pytest.mark.asyncio
526527
async def test_active_thread_id(sentry_init, capture_envelopes, endpoint, app):
527528
sentry_init(
528529
traces_sample_rate=1.0,
529-
_experiments={"profiles_sample_rate": 1.0},
530+
profiles_sample_rate=1.0,
530531
)
531532

532533
envelopes = capture_envelopes()
@@ -535,19 +536,27 @@ async def test_active_thread_id(sentry_init, capture_envelopes, endpoint, app):
535536
response = await client.get(endpoint)
536537
assert response.status_code == 200
537538

538-
data = json.loads(response.content)
539+
data = json.loads(await response.get_data(as_text=True))
539540

540541
envelopes = [envelope for envelope in envelopes]
541542
assert len(envelopes) == 1
542543

543544
profiles = [item for item in envelopes[0].items if item.type == "profile"]
544-
assert len(profiles) == 1
545+
assert len(profiles) == 1, envelopes[0].items
545546

546-
for profile in profiles:
547-
transactions = profile.payload.json["transactions"]
547+
for item in profiles:
548+
transactions = item.payload.json["transactions"]
548549
assert len(transactions) == 1
549550
assert str(data["active"]) == transactions[0]["active_thread_id"]
550551

552+
transactions = [item for item in envelopes[0].items if item.type == "transaction"]
553+
assert len(transactions) == 1
554+
555+
for item in transactions:
556+
transaction = item.payload.json
557+
trace_context = transaction["contexts"]["trace"]
558+
assert str(data["active"]) == trace_context["data"]["thread.id"]
559+
551560

552561
@pytest.mark.asyncio
553562
async def test_span_origin(sentry_init, capture_events, app):

tests/integrations/starlette/test_starlette.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ def test_legacy_setup(
885885
def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, endpoint):
886886
sentry_init(
887887
traces_sample_rate=1.0,
888-
_experiments={"profiles_sample_rate": 1.0},
888+
profiles_sample_rate=1.0,
889889
)
890890
app = starlette_app_factory()
891891
asgi_app = SentryAsgiMiddleware(app)
@@ -904,11 +904,19 @@ def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, en
904904
profiles = [item for item in envelopes[0].items if item.type == "profile"]
905905
assert len(profiles) == 1
906906

907-
for profile in profiles:
908-
transactions = profile.payload.json["transactions"]
907+
for item in profiles:
908+
transactions = item.payload.json["transactions"]
909909
assert len(transactions) == 1
910910
assert str(data["active"]) == transactions[0]["active_thread_id"]
911911

912+
transactions = [item for item in envelopes[0].items if item.type == "transaction"]
913+
assert len(transactions) == 1
914+
915+
for item in transactions:
916+
transaction = item.payload.json
917+
trace_context = transaction["contexts"]["trace"]
918+
assert str(data["active"]) == trace_context["data"]["thread.id"]
919+
912920

913921
def test_original_request_not_scrubbed(sentry_init, capture_events):
914922
sentry_init(integrations=[StarletteIntegration()])

0 commit comments

Comments
 (0)