Repository: getsentry/sentry-python

Issue:
Port span status exit code logic to potel

Discussion:
codecov[bot]: ### :x: 3 Tests Failed:
| Tests completed | Failed | Passed | Skipped |
|---|---|---|---|
| 36 | 3 | 33 | 0 |
<details><summary>View the top 3 failed test(s) by shortest run time</summary>

> 
> ```python
> tests.integrations.aiohttp.test_aiohttp test_crumb_capture_client_error[403-warning]
> ```
> 
> <details><summary>Stack Traces | 0.075s run time</summary>
> 
> > 
> > ```python
> > .../integrations/aiohttp/test_aiohttp.py:576: in test_crumb_capture_client_error
> >     assert crumb["level"] == level
> > E   KeyError: 'level'
> > ```
> 
> </details>


> 
> ```python
> tests.integrations.aiohttp.test_aiohttp test_crumb_capture_client_error[500-error]
> ```
> 
> <details><summary>Stack Traces | 0.075s run time</summary>
> 
> > 
> > ```python
> > .../integrations/aiohttp/test_aiohttp.py:576: in test_crumb_capture_client_error
> >     assert crumb["level"] == level
> > E   KeyError: 'level'
> > ```
> 
> </details>


> 
> ```python
> tests.integrations.aiohttp.test_aiohttp test_crumb_capture_client_error[405-warning]
> ```
> 
> <details><summary>Stack Traces | 0.076s run time</summary>
> 
> > 
> > ```python
> > .../integrations/aiohttp/test_aiohttp.py:576: in test_crumb_capture_client_error
> >     assert crumb["level"] == level
> > E   KeyError: 'level'
> > ```
> 
> </details>

</details>

To view more test analytics, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/getsentry/sentry-python/tests/antonpirker%2Fpotel%2Fspan-status-systemexit)
<sub>📋 Got 3 mins? [Take this short survey](https://forms.gle/BpocVj23nhr2Y45G7) to help us improve Test Analytics.</sub>

Patch:
diff --git a/sentry_sdk/integrations/opentelemetry/span_processor.py b/sentry_sdk/integrations/opentelemetry/span_processor.py
index a3cf545d..bf3ff62d 100644
--- a/sentry_sdk/integrations/opentelemetry/span_processor.py
+++ b/sentry_sdk/integrations/opentelemetry/span_processor.py
@@ -261,9 +261,6 @@ class SentrySpanProcessor(SpanProcessor):
             }
         )
 
-        if status:
-            span_json.setdefault("tags", {})["status"] = status
-
         if parent_span_id:
             span_json["parent_span_id"] = parent_span_id
 
diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py
index 6bb1eeab..744003ea 100644
--- a/sentry_sdk/tracing.py
+++ b/sentry_sdk/tracing.py
@@ -20,6 +20,7 @@ from sentry_sdk.utils import (
     _serialize_span_attribute,
     get_current_thread_meta,
     logger,
+    should_be_treated_as_error,
 )
 
 from typing import TYPE_CHECKING, cast
@@ -424,7 +425,7 @@ class Span:
 
     def __exit__(self, ty, value, tb):
         # type: (Optional[Any], Optional[Any], Optional[Any]) -> None
-        if value is not None:
+        if value is not None and should_be_treated_as_error(ty, value):
             self.set_status(SPANSTATUS.INTERNAL_ERROR)
         else:
             status_unset = (
diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py
index ce143722..d7aa316e 100644
--- a/sentry_sdk/utils.py
+++ b/sentry_sdk/utils.py
@@ -1879,7 +1879,6 @@ def datetime_from_isoformat(value):
     return result.astimezone(timezone.utc)
 
 
-# TODO-neel-potel use in span status
 def should_be_treated_as_error(ty, value):
     # type: (Any, Any) -> bool
     if ty == SystemExit and hasattr(value, "code") and value.code in (0, None):


Test patch:
diff --git a/tests/tracing/test_integration_tests.py b/tests/tracing/test_integration_tests.py
index 0a96e859..22deabb6 100644
--- a/tests/tracing/test_integration_tests.py
+++ b/tests/tracing/test_integration_tests.py
@@ -37,11 +37,10 @@ def test_basic(sentry_init, capture_events, sample_rate):
 
         span1, span2 = event["spans"]
         parent_span = event
-        assert span1["tags"]["status"] == "internal_error"
         assert span1["status"] == "internal_error"
         assert span1["op"] == "foo"
         assert span1["description"] == "foodesc"
-        assert span2["tags"]["status"] == "ok"
+        assert span2["status"] == "ok"
         assert span2["op"] == "bar"
         assert span2["description"] == "bardesc"
         assert parent_span["transaction"] == "hi"
@@ -253,8 +252,8 @@ def test_non_error_exceptions(
     sentry_init(traces_sample_rate=1.0)
     events = capture_events()
 
-    with start_span(name="hi") as span:
-        span.set_status(SPANSTATUS.OK)
+    with start_span(name="hi") as root_span:
+        root_span.set_status(SPANSTATUS.OK)
         with pytest.raises(exception_cls):
             with start_span(op="foo", name="foodesc"):
                 raise exception_cls(exception_value)
@@ -264,7 +263,7 @@ def test_non_error_exceptions(
 
     span = event["spans"][0]
     assert "status" not in span.get("tags", {})
-    assert "status" not in event["tags"]
+    assert "status" not in event.get("tags", {})
     assert event["contexts"]["trace"]["status"] == "ok"
 
 
@@ -289,5 +288,5 @@ def test_good_sysexit_doesnt_fail_transaction(
 
     span = event["spans"][0]
     assert "status" not in span.get("tags", {})
-    assert "status" not in event["tags"]
+    assert "status" not in event.get("tags", {})
     assert event["contexts"]["trace"]["status"] == "ok"