From ab3b054801afcfcbdd9d921d4b692d6dcc945bfd Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Fri, 13 Jan 2023 16:32:11 -0500 Subject: [PATCH 1/2] fix(otel): NoOpSpan updates scope When using otel as the instrumentor, the NoOpSpan needs to update the scope when it's used as a context manager. If it does not, then this differs from the usual behaviour of a span and the end user may start seeing an unexpected `None` on the scope. --- sentry_sdk/tracing.py | 8 -------- tests/tracing/test_noop_span.py | 10 +++++++--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index dc65ea5fd7..b72524f734 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -859,14 +859,6 @@ def __repr__(self): # type: () -> str return self.__class__.__name__ - def __enter__(self): - # type: () -> NoOpSpan - return self - - def __exit__(self, ty, value, tb): - # type: (Optional[Any], Optional[Any], Optional[Any]) -> None - pass - def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs): # type: (str, **Any) -> NoOpSpan return NoOpSpan() diff --git a/tests/tracing/test_noop_span.py b/tests/tracing/test_noop_span.py index 3dc148f848..80329b8f3f 100644 --- a/tests/tracing/test_noop_span.py +++ b/tests/tracing/test_noop_span.py @@ -11,10 +11,11 @@ def test_noop_start_transaction(sentry_init): sentry_init(instrumenter="otel", debug=True) - transaction = sentry_sdk.start_transaction(op="task", name="test_transaction_name") - assert isinstance(transaction, NoOpSpan) + with sentry_sdk.start_transaction(op="task", name="test_transaction_name") as transaction: + assert isinstance(transaction, NoOpSpan) + assert sentry_sdk.Hub.current.scope.span is transaction - transaction.name = "new name" + transaction.name = "new name" def test_noop_start_span(sentry_init): @@ -22,6 +23,7 @@ def test_noop_start_span(sentry_init): with sentry_sdk.start_span(op="http", description="GET /") as span: assert isinstance(span, NoOpSpan) + assert sentry_sdk.Hub.current.scope.span is span span.set_tag("http.status_code", "418") span.set_data("http.entity_type", "teapot") @@ -35,6 +37,7 @@ def test_noop_transaction_start_child(sentry_init): with transaction.start_child(op="child_task") as child: assert isinstance(child, NoOpSpan) + assert sentry_sdk.Hub.current.scope.span is child def test_noop_span_start_child(sentry_init): @@ -44,3 +47,4 @@ def test_noop_span_start_child(sentry_init): with span.start_child(op="child_task") as child: assert isinstance(child, NoOpSpan) + assert sentry_sdk.Hub.current.scope.span is child From e9d5bdb8b18c09e86924e0c053e4088c0c343bd6 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Mon, 16 Jan 2023 10:46:32 -0500 Subject: [PATCH 2/2] run black --- tests/tracing/test_noop_span.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/tracing/test_noop_span.py b/tests/tracing/test_noop_span.py index 80329b8f3f..92cba75a35 100644 --- a/tests/tracing/test_noop_span.py +++ b/tests/tracing/test_noop_span.py @@ -11,7 +11,9 @@ def test_noop_start_transaction(sentry_init): sentry_init(instrumenter="otel", debug=True) - with sentry_sdk.start_transaction(op="task", name="test_transaction_name") as transaction: + with sentry_sdk.start_transaction( + op="task", name="test_transaction_name" + ) as transaction: assert isinstance(transaction, NoOpSpan) assert sentry_sdk.Hub.current.scope.span is transaction