Skip to content

Commit dc705f4

Browse files
sentrivanaarjenzorgdoc
authored andcommitted
fix(tracing): Keep original function signature when decorated (getsentry#3178)
Our trace decorator was leading to a change of signature of the decorated function.
1 parent 7e875d8 commit dc705f4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sentry_sdk/tracing_utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ async def func_with_tracing(*args, **kwargs):
645645
):
646646
return await func(*args, **kwargs)
647647

648+
try:
649+
func_with_tracing.__signature__ = inspect.signature(func) # type: ignore[attr-defined]
650+
except Exception:
651+
pass
652+
648653
# Synchronous case
649654
else:
650655

@@ -668,6 +673,11 @@ def func_with_tracing(*args, **kwargs):
668673
):
669674
return func(*args, **kwargs)
670675

676+
try:
677+
func_with_tracing.__signature__ = inspect.signature(func) # type: ignore[attr-defined]
678+
except Exception:
679+
pass
680+
671681
return func_with_tracing
672682

673683

tests/tracing/test_decorator.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import inspect
12
from unittest import mock
23

34
import pytest
45

6+
from sentry_sdk.tracing import trace
57
from sentry_sdk.tracing_utils import start_child_span_decorator
68
from sentry_sdk.utils import logger
79
from tests.conftest import patch_start_tracing_child
@@ -76,3 +78,38 @@ async def test_trace_decorator_async_no_trx():
7678
"test_decorator.my_async_example_function",
7779
)
7880
assert result2 == "return_of_async_function"
81+
82+
83+
def test_functions_to_trace_signature_unchanged_sync(sentry_init):
84+
sentry_init(
85+
traces_sample_rate=1.0,
86+
)
87+
88+
def _some_function(a, b, c):
89+
pass
90+
91+
@trace
92+
def _some_function_traced(a, b, c):
93+
pass
94+
95+
assert inspect.getcallargs(_some_function, 1, 2, 3) == inspect.getcallargs(
96+
_some_function_traced, 1, 2, 3
97+
)
98+
99+
100+
@pytest.mark.asyncio
101+
async def test_functions_to_trace_signature_unchanged_async(sentry_init):
102+
sentry_init(
103+
traces_sample_rate=1.0,
104+
)
105+
106+
async def _some_function(a, b, c):
107+
pass
108+
109+
@trace
110+
async def _some_function_traced(a, b, c):
111+
pass
112+
113+
assert inspect.getcallargs(_some_function, 1, 2, 3) == inspect.getcallargs(
114+
_some_function_traced, 1, 2, 3
115+
)

0 commit comments

Comments
 (0)