Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracing): Keep original function signature when decorated #3178

Merged
merged 9 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Loading