Skip to content

Commit 9ddfa93

Browse files
authored
Tread SystemExit(0) not as a span status of 'internal_error' (#4094)
Also make sure, that the span status is not set as a tag on the span. SDKs should not set tags at all by default (only users are allowed to set tags) Fixes #4065
1 parent eb93c1f commit 9ddfa93

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,6 @@ def _span_to_json(self, span):
261261
}
262262
)
263263

264-
if status:
265-
span_json.setdefault("tags", {})["status"] = status
266-
267264
if parent_span_id:
268265
span_json["parent_span_id"] = parent_span_id
269266

sentry_sdk/tracing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
_serialize_span_attribute,
2121
get_current_thread_meta,
2222
logger,
23+
should_be_treated_as_error,
2324
)
2425

2526
from typing import TYPE_CHECKING, cast
@@ -424,7 +425,7 @@ def __enter__(self):
424425

425426
def __exit__(self, ty, value, tb):
426427
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None
427-
if value is not None:
428+
if value is not None and should_be_treated_as_error(ty, value):
428429
self.set_status(SPANSTATUS.INTERNAL_ERROR)
429430
else:
430431
status_unset = (

sentry_sdk/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,6 @@ def datetime_from_isoformat(value):
18791879
return result.astimezone(timezone.utc)
18801880

18811881

1882-
# TODO-neel-potel use in span status
18831882
def should_be_treated_as_error(ty, value):
18841883
# type: (Any, Any) -> bool
18851884
if ty == SystemExit and hasattr(value, "code") and value.code in (0, None):

tests/tracing/test_integration_tests.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ def test_basic(sentry_init, capture_events, sample_rate):
3737

3838
span1, span2 = event["spans"]
3939
parent_span = event
40-
assert span1["tags"]["status"] == "internal_error"
4140
assert span1["status"] == "internal_error"
4241
assert span1["op"] == "foo"
4342
assert span1["description"] == "foodesc"
44-
assert span2["tags"]["status"] == "ok"
43+
assert span2["status"] == "ok"
4544
assert span2["op"] == "bar"
4645
assert span2["description"] == "bardesc"
4746
assert parent_span["transaction"] == "hi"
@@ -253,8 +252,8 @@ def test_non_error_exceptions(
253252
sentry_init(traces_sample_rate=1.0)
254253
events = capture_events()
255254

256-
with start_span(name="hi") as span:
257-
span.set_status(SPANSTATUS.OK)
255+
with start_span(name="hi") as root_span:
256+
root_span.set_status(SPANSTATUS.OK)
258257
with pytest.raises(exception_cls):
259258
with start_span(op="foo", name="foodesc"):
260259
raise exception_cls(exception_value)
@@ -264,7 +263,7 @@ def test_non_error_exceptions(
264263

265264
span = event["spans"][0]
266265
assert "status" not in span.get("tags", {})
267-
assert "status" not in event["tags"]
266+
assert "status" not in event.get("tags", {})
268267
assert event["contexts"]["trace"]["status"] == "ok"
269268

270269

@@ -289,5 +288,5 @@ def test_good_sysexit_doesnt_fail_transaction(
289288

290289
span = event["spans"][0]
291290
assert "status" not in span.get("tags", {})
292-
assert "status" not in event["tags"]
291+
assert "status" not in event.get("tags", {})
293292
assert event["contexts"]["trace"]["status"] == "ok"

0 commit comments

Comments
 (0)