From 784ba6a52016e7d90aca6ca70df15de32e9448fd Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 10:51:39 +0200 Subject: [PATCH 1/7] Override wrapper signature --- sentry_sdk/tracing_utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index fac51f4848..8da9be970a 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -645,6 +645,8 @@ async def func_with_tracing(*args, **kwargs): ): return await func(*args, **kwargs) + func_with_tracing.__signature__ = func.__signature__ + # Synchronous case else: @@ -668,6 +670,8 @@ def func_with_tracing(*args, **kwargs): ): return func(*args, **kwargs) + func_with_tracing.__signature__ = func.__signature__ + return func_with_tracing From 1eac0bcc3914fc0700528025762f20d427041058 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 12:54:27 +0200 Subject: [PATCH 2/7] fix --- sentry_sdk/tracing_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 8da9be970a..78c2c7365b 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -645,7 +645,7 @@ async def func_with_tracing(*args, **kwargs): ): return await func(*args, **kwargs) - func_with_tracing.__signature__ = func.__signature__ + func_with_tracing.__signature__ = inspect.signature(func) # Synchronous case else: @@ -670,7 +670,7 @@ def func_with_tracing(*args, **kwargs): ): return func(*args, **kwargs) - func_with_tracing.__signature__ = func.__signature__ + func_with_tracing.__signature__ = inspect.signature(func) return func_with_tracing From fe075b55e6e6e0c8bd3f6a5f2e7d91bc8fbd0259 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 13:16:55 +0200 Subject: [PATCH 3/7] fix valueerrors --- sentry_sdk/tracing_utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 78c2c7365b..b1e1db57ac 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -645,7 +645,10 @@ async def func_with_tracing(*args, **kwargs): ): return await func(*args, **kwargs) - func_with_tracing.__signature__ = inspect.signature(func) + try: + func_with_tracing.__signature__ = inspect.signature(func) + except Exception: + pass # Synchronous case else: @@ -670,7 +673,10 @@ def func_with_tracing(*args, **kwargs): ): return func(*args, **kwargs) - func_with_tracing.__signature__ = inspect.signature(func) + try: + func_with_tracing.__signature__ = inspect.signature(func) + except Exception: + pass return func_with_tracing From a5bc9552c371dd48ac6131b3d7cd38aed5b90c4d Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 13:25:43 +0200 Subject: [PATCH 4/7] test --- tests/test_basics.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_basics.py b/tests/test_basics.py index 8727e27f35..58b9e94245 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -1,3 +1,4 @@ +import inspect import logging import os import sys @@ -18,6 +19,7 @@ last_event_id, add_breadcrumb, isolation_scope, + tracing, Hub, Scope, ) @@ -681,6 +683,25 @@ def test_functions_to_trace(sentry_init, capture_events): assert event["spans"][2]["description"] == "tests.test_basics._hello_world" +def _some_function(a, b, c): + pass + + +@tracing.trace +def _some_function_traced(a, b, c): + pass + + +def test_functions_to_trace_signature_unchanged(sentry_init): + sentry_init( + traces_sample_rate=1.0, + ) + + assert inspect.getcallargs(_some_function, 1, 2, 3) == inspect.getcallargs( + _some_function_traced, 1, 2, 3 + ) + + class WorldGreeter: def __init__(self, word): self.word = word From 3bf021f00250eae756aa0ed32883697725c86ff9 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 13:27:02 +0200 Subject: [PATCH 5/7] better test --- tests/test_basics.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/test_basics.py b/tests/test_basics.py index 58b9e94245..f50486ac0f 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -683,20 +683,18 @@ def test_functions_to_trace(sentry_init, capture_events): assert event["spans"][2]["description"] == "tests.test_basics._hello_world" -def _some_function(a, b, c): - pass - - -@tracing.trace -def _some_function_traced(a, b, c): - pass - - def test_functions_to_trace_signature_unchanged(sentry_init): sentry_init( traces_sample_rate=1.0, ) + def _some_function(a, b, c): + pass + + @tracing.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 ) From 72db5b3d410cc88304748facef0a3dc692c26d70 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 13:28:50 +0200 Subject: [PATCH 6/7] mypy --- sentry_sdk/tracing_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index b1e1db57ac..146ec859e2 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -646,7 +646,7 @@ async def func_with_tracing(*args, **kwargs): return await func(*args, **kwargs) try: - func_with_tracing.__signature__ = inspect.signature(func) + func_with_tracing.__signature__ = inspect.signature(func) # type: ignore[attr-defined] except Exception: pass @@ -674,7 +674,7 @@ def func_with_tracing(*args, **kwargs): return func(*args, **kwargs) try: - func_with_tracing.__signature__ = inspect.signature(func) + func_with_tracing.__signature__ = inspect.signature(func) # type: ignore[attr-defined] except Exception: pass From dd2c77748de8bb40d9fc956d27e0692037e741e8 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 18 Jun 2024 13:35:45 +0200 Subject: [PATCH 7/7] add async test, move to test_deco --- tests/test_basics.py | 19 ----------------- tests/tracing/test_decorator.py | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/tests/test_basics.py b/tests/test_basics.py index f50486ac0f..8727e27f35 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -1,4 +1,3 @@ -import inspect import logging import os import sys @@ -19,7 +18,6 @@ last_event_id, add_breadcrumb, isolation_scope, - tracing, Hub, Scope, ) @@ -683,23 +681,6 @@ def test_functions_to_trace(sentry_init, capture_events): assert event["spans"][2]["description"] == "tests.test_basics._hello_world" -def test_functions_to_trace_signature_unchanged(sentry_init): - sentry_init( - traces_sample_rate=1.0, - ) - - def _some_function(a, b, c): - pass - - @tracing.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 - ) - - class WorldGreeter: def __init__(self, word): self.word = word diff --git a/tests/tracing/test_decorator.py b/tests/tracing/test_decorator.py index 0f9ebf23b5..6c2d337285 100644 --- a/tests/tracing/test_decorator.py +++ b/tests/tracing/test_decorator.py @@ -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 @@ -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 + )