Skip to content

Commit 463375e

Browse files
move into threading integration
1 parent ee64817 commit 463375e

File tree

5 files changed

+75
-184
lines changed

5 files changed

+75
-184
lines changed

sentry_sdk/integrations/concurrent.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

sentry_sdk/integrations/threading.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import warnings
33
from functools import wraps
44
from threading import Thread, current_thread
5+
from concurrent.futures import ThreadPoolExecutor, Future
56

67
import sentry_sdk
78
from sentry_sdk.integrations import Integration
@@ -24,6 +25,7 @@
2425
from sentry_sdk._types import ExcInfo
2526

2627
F = TypeVar("F", bound=Callable[..., Any])
28+
T = TypeVar("T", bound=Any)
2729

2830

2931
class ThreadingIntegration(Integration):
@@ -109,6 +111,7 @@ def sentry_start(self, *a, **kw):
109111
return old_start(self, *a, **kw)
110112

111113
Thread.start = sentry_start # type: ignore
114+
ThreadPoolExecutor.submit = _wrap_threadpool_executor_submit(ThreadPoolExecutor.submit) # type: ignore
112115

113116

114117
def _wrap_run(isolation_scope_to_use, current_scope_to_use, old_run_func):
@@ -134,6 +137,37 @@ def _run_old_run_func():
134137
return run # type: ignore
135138

136139

140+
def _wrap_threadpool_executor_submit(func):
141+
# type: (Callable[..., Future[T]]) -> Callable[..., Future[T]]
142+
"""
143+
Wrap submit call to propagate scopes on task submission.
144+
"""
145+
146+
@wraps(func)
147+
def sentry_submit(self, fn, *args, **kwargs):
148+
# type: (ThreadPoolExecutor, Callable[..., T], *Any, **Any) -> Future[T]
149+
integration = sentry_sdk.get_client().get_integration(ThreadingIntegration)
150+
if integration is None:
151+
return func(self, fn, *args, **kwargs)
152+
153+
if integration.propagate_scope:
154+
isolation_scope = sentry_sdk.get_isolation_scope().fork()
155+
current_scope = sentry_sdk.get_current_scope().fork()
156+
else:
157+
isolation_scope = None
158+
current_scope = None
159+
160+
def wrapped_fn(*args, **kwargs):
161+
# type: (*Any, **Any) -> Any
162+
with use_isolation_scope(isolation_scope):
163+
with use_scope(current_scope):
164+
return fn(*args, **kwargs)
165+
166+
return func(self, wrapped_fn, *args, **kwargs)
167+
168+
return sentry_submit
169+
170+
137171
def _capture_exception():
138172
# type: () -> ExcInfo
139173
exc_info = sys.exc_info()

tests/integrations/concurrent/test_concurrent.py

Lines changed: 0 additions & 133 deletions
This file was deleted.

tests/integrations/threading/test_threading.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,44 @@ def do_some_work(number):
276276
- op="outer-submit-4": description="Thread: main"\
277277
"""
278278
)
279+
280+
281+
def test_spans_from_threadpool(sentry_init, capture_events, render_span_tree):
282+
sentry_init(
283+
traces_sample_rate=1.0,
284+
integrations=[ThreadingIntegration()],
285+
)
286+
events = capture_events()
287+
288+
def do_some_work(number):
289+
with sentry_sdk.start_span(
290+
op=f"inner-run-{number}", name=f"Thread: child-{number}"
291+
):
292+
pass
293+
294+
with sentry_sdk.start_transaction(op="outer-trx"):
295+
with futures.ThreadPoolExecutor(max_workers=1) as executor:
296+
for number in range(5):
297+
with sentry_sdk.start_span(
298+
op=f"outer-submit-{number}", name="Thread: main"
299+
):
300+
future = executor.submit(do_some_work, number)
301+
future.result()
302+
303+
(event,) = events
304+
305+
assert render_span_tree(event) == dedent(
306+
"""\
307+
- op="outer-trx": description=null
308+
- op="outer-submit-0": description="Thread: main"
309+
- op="inner-run-0": description="Thread: child-0"
310+
- op="outer-submit-1": description="Thread: main"
311+
- op="inner-run-1": description="Thread: child-1"
312+
- op="outer-submit-2": description="Thread: main"
313+
- op="inner-run-2": description="Thread: child-2"
314+
- op="outer-submit-3": description="Thread: main"
315+
- op="inner-run-3": description="Thread: child-3"
316+
- op="outer-submit-4": description="Thread: main"
317+
- op="inner-run-4": description="Thread: child-4"\
318+
"""
319+
)

tests/test_basics.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,6 @@ def foo(event, hint):
868868
(["atexit"], "sentry.python"),
869869
(["boto3"], "sentry.python"),
870870
(["celery"], "sentry.python"),
871-
(["concurrent"], "sentry.python"),
872871
(["dedupe"], "sentry.python"),
873872
(["excepthook"], "sentry.python"),
874873
(["unraisablehook"], "sentry.python"),

0 commit comments

Comments
 (0)