Skip to content

Commit 1445c73

Browse files
authored
fix(otel): NoOpSpan updates scope (#1834)
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.
1 parent de84c4c commit 1445c73

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

sentry_sdk/tracing.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -859,14 +859,6 @@ def __repr__(self):
859859
# type: () -> str
860860
return self.__class__.__name__
861861

862-
def __enter__(self):
863-
# type: () -> NoOpSpan
864-
return self
865-
866-
def __exit__(self, ty, value, tb):
867-
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None
868-
pass
869-
870862
def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
871863
# type: (str, **Any) -> NoOpSpan
872864
return NoOpSpan()

tests/tracing/test_noop_span.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111
def test_noop_start_transaction(sentry_init):
1212
sentry_init(instrumenter="otel", debug=True)
1313

14-
transaction = sentry_sdk.start_transaction(op="task", name="test_transaction_name")
15-
assert isinstance(transaction, NoOpSpan)
14+
with sentry_sdk.start_transaction(
15+
op="task", name="test_transaction_name"
16+
) as transaction:
17+
assert isinstance(transaction, NoOpSpan)
18+
assert sentry_sdk.Hub.current.scope.span is transaction
1619

17-
transaction.name = "new name"
20+
transaction.name = "new name"
1821

1922

2023
def test_noop_start_span(sentry_init):
2124
sentry_init(instrumenter="otel", debug=True)
2225

2326
with sentry_sdk.start_span(op="http", description="GET /") as span:
2427
assert isinstance(span, NoOpSpan)
28+
assert sentry_sdk.Hub.current.scope.span is span
2529

2630
span.set_tag("http.status_code", "418")
2731
span.set_data("http.entity_type", "teapot")
@@ -35,6 +39,7 @@ def test_noop_transaction_start_child(sentry_init):
3539

3640
with transaction.start_child(op="child_task") as child:
3741
assert isinstance(child, NoOpSpan)
42+
assert sentry_sdk.Hub.current.scope.span is child
3843

3944

4045
def test_noop_span_start_child(sentry_init):
@@ -44,3 +49,4 @@ def test_noop_span_start_child(sentry_init):
4449

4550
with span.start_child(op="child_task") as child:
4651
assert isinstance(child, NoOpSpan)
52+
assert sentry_sdk.Hub.current.scope.span is child

0 commit comments

Comments
 (0)