Skip to content

Commit 2610c66

Browse files
authored
Use functools.wrap for ThreadingIntegration patches to fix attributes (#2080)
Should fix compatibility with OpenCensus threading integration
1 parent 81afcea commit 2610c66

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

sentry_sdk/integrations/threading.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
import sys
4+
from functools import wraps
45
from threading import Thread, current_thread
56

67
from sentry_sdk import Hub
@@ -32,6 +33,7 @@ def setup_once():
3233
# type: () -> None
3334
old_start = Thread.start
3435

36+
@wraps(old_start)
3537
def sentry_start(self, *a, **kw):
3638
# type: (Thread, *Any, **Any) -> Any
3739
hub = Hub.current
@@ -58,6 +60,7 @@ def sentry_start(self, *a, **kw):
5860

5961
def _wrap_run(parent_hub, old_run_func):
6062
# type: (Optional[Hub], F) -> F
63+
@wraps(old_run_func)
6164
def run(*a, **kw):
6265
# type: (*Any, **Any) -> Any
6366
hub = parent_hub or Hub.current

tests/integrations/threading/test_threading.py

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from sentry_sdk import configure_scope, capture_message
88
from sentry_sdk.integrations.threading import ThreadingIntegration
99

10+
original_start = Thread.start
11+
original_run = Thread.run
12+
1013

1114
@pytest.mark.forked
1215
@pytest.mark.parametrize("integrations", [[ThreadingIntegration()], []])
@@ -114,3 +117,25 @@ def run(self):
114117
for event in events:
115118
(exception,) = event["exception"]["values"]
116119
assert exception["type"] == "ZeroDivisionError"
120+
121+
122+
def test_wrapper_attributes(sentry_init):
123+
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])
124+
125+
def target():
126+
assert t.run.__name__ == "run"
127+
assert t.run.__qualname__ == original_run.__qualname__
128+
129+
t = Thread(target=target)
130+
t.start()
131+
t.join()
132+
133+
assert Thread.start.__name__ == "start"
134+
assert Thread.start.__qualname__ == original_start.__qualname__
135+
assert t.start.__name__ == "start"
136+
assert t.start.__qualname__ == original_start.__qualname__
137+
138+
assert Thread.run.__name__ == "run"
139+
assert Thread.run.__qualname__ == original_run.__qualname__
140+
assert t.run.__name__ == "run"
141+
assert t.run.__qualname__ == original_run.__qualname__

0 commit comments

Comments
 (0)