Skip to content

Commit

Permalink
Use functools.wrap for ThreadingIntegration patches to fix attributes (
Browse files Browse the repository at this point in the history
…#2080)

Should fix compatibility with OpenCensus threading integration
  • Loading branch information
EpicWink authored May 5, 2023
1 parent 81afcea commit 2610c66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sentry_sdk/integrations/threading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

import sys
from functools import wraps
from threading import Thread, current_thread

from sentry_sdk import Hub
Expand Down Expand Up @@ -32,6 +33,7 @@ def setup_once():
# type: () -> None
old_start = Thread.start

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

def _wrap_run(parent_hub, old_run_func):
# type: (Optional[Hub], F) -> F
@wraps(old_run_func)
def run(*a, **kw):
# type: (*Any, **Any) -> Any
hub = parent_hub or Hub.current
Expand Down
25 changes: 25 additions & 0 deletions tests/integrations/threading/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from sentry_sdk import configure_scope, capture_message
from sentry_sdk.integrations.threading import ThreadingIntegration

original_start = Thread.start
original_run = Thread.run


@pytest.mark.forked
@pytest.mark.parametrize("integrations", [[ThreadingIntegration()], []])
Expand Down Expand Up @@ -114,3 +117,25 @@ def run(self):
for event in events:
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"


def test_wrapper_attributes(sentry_init):
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])

def target():
assert t.run.__name__ == "run"
assert t.run.__qualname__ == original_run.__qualname__

t = Thread(target=target)
t.start()
t.join()

assert Thread.start.__name__ == "start"
assert Thread.start.__qualname__ == original_start.__qualname__
assert t.start.__name__ == "start"
assert t.start.__qualname__ == original_start.__qualname__

assert Thread.run.__name__ == "run"
assert Thread.run.__qualname__ == original_run.__qualname__
assert t.run.__name__ == "run"
assert t.run.__qualname__ == original_run.__qualname__

0 comments on commit 2610c66

Please sign in to comment.