From 2610c66f43754f556c447949db31de7867a02c7c Mon Sep 17 00:00:00 2001 From: Laurie O Date: Fri, 5 May 2023 21:38:24 +1000 Subject: [PATCH] Use functools.wrap for ThreadingIntegration patches to fix attributes (#2080) Should fix compatibility with OpenCensus threading integration --- sentry_sdk/integrations/threading.py | 3 +++ .../integrations/threading/test_threading.py | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/sentry_sdk/integrations/threading.py b/sentry_sdk/integrations/threading.py index 189731610b..499cf85e6d 100644 --- a/sentry_sdk/integrations/threading.py +++ b/sentry_sdk/integrations/threading.py @@ -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 @@ -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 @@ -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 diff --git a/tests/integrations/threading/test_threading.py b/tests/integrations/threading/test_threading.py index 67b79e2080..683a6c74dd 100644 --- a/tests/integrations/threading/test_threading.py +++ b/tests/integrations/threading/test_threading.py @@ -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()], []]) @@ -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__