Skip to content

Commit

Permalink
fix(tracing): Keep original function signature when decorated (#3178)
Browse files Browse the repository at this point in the history
Our trace decorator was leading to a change of signature of the decorated function.
  • Loading branch information
sentrivana authored Jun 18, 2024
1 parent 56d2cc6 commit 85e4f1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ async def func_with_tracing(*args, **kwargs):
):
return await func(*args, **kwargs)

try:
func_with_tracing.__signature__ = inspect.signature(func) # type: ignore[attr-defined]
except Exception:
pass

# Synchronous case
else:

Expand All @@ -668,6 +673,11 @@ def func_with_tracing(*args, **kwargs):
):
return func(*args, **kwargs)

try:
func_with_tracing.__signature__ = inspect.signature(func) # type: ignore[attr-defined]
except Exception:
pass

return func_with_tracing


Expand Down
37 changes: 37 additions & 0 deletions tests/tracing/test_decorator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import inspect
from unittest import mock

import pytest

from sentry_sdk.tracing import trace
from sentry_sdk.tracing_utils import start_child_span_decorator
from sentry_sdk.utils import logger
from tests.conftest import patch_start_tracing_child
Expand Down Expand Up @@ -76,3 +78,38 @@ async def test_trace_decorator_async_no_trx():
"test_decorator.my_async_example_function",
)
assert result2 == "return_of_async_function"


def test_functions_to_trace_signature_unchanged_sync(sentry_init):
sentry_init(
traces_sample_rate=1.0,
)

def _some_function(a, b, c):
pass

@trace
def _some_function_traced(a, b, c):
pass

assert inspect.getcallargs(_some_function, 1, 2, 3) == inspect.getcallargs(
_some_function_traced, 1, 2, 3
)


@pytest.mark.asyncio
async def test_functions_to_trace_signature_unchanged_async(sentry_init):
sentry_init(
traces_sample_rate=1.0,
)

async def _some_function(a, b, c):
pass

@trace
async def _some_function_traced(a, b, c):
pass

assert inspect.getcallargs(_some_function, 1, 2, 3) == inspect.getcallargs(
_some_function_traced, 1, 2, 3
)

0 comments on commit 85e4f1e

Please sign in to comment.