Skip to content

Commit 60d6333

Browse files
authored
Fix arq tests in POTel (#3875)
Make sure OK status is set, only when there has not been a error status set before.
1 parent 92f5391 commit 60d6333

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

sentry_sdk/integrations/arq.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys
22

3+
from opentelemetry.trace.status import StatusCode
4+
35
import sentry_sdk
46
from sentry_sdk.consts import OP, SPANSTATUS
57
from sentry_sdk.integrations import DidNotEnable, Integration
@@ -116,7 +118,10 @@ async def _sentry_run_job(self, job_id, score):
116118
origin=ArqIntegration.origin,
117119
) as span:
118120
return_value = await old_run_job(self, job_id, score)
119-
span.set_status(SPANSTATUS.OK)
121+
122+
if span.status is None:
123+
span.set_status(SPANSTATUS.OK)
124+
120125
return return_value
121126

122127
Worker.run_job = _sentry_run_job

sentry_sdk/tracing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,25 @@ def set_attribute(self, key, value):
15831583

15841584
self._otel_span.set_attribute(key, _serialize_span_attribute(value))
15851585

1586+
@property
1587+
def status(self):
1588+
# type: () -> Optional[str]
1589+
"""
1590+
Return the Sentry `SPANSTATUS` corresponding to the underlying OTel status.
1591+
Because differences in possible values in OTel `StatusCode` and
1592+
Sentry `SPANSTATUS` it can not be guaranteed that the status
1593+
set in `set_status()` will be the same as the one returned here.
1594+
"""
1595+
if not hasattr(self._otel_span, "status"):
1596+
return None
1597+
1598+
if self._otel_span.status.status_code == StatusCode.UNSET:
1599+
return None
1600+
elif self._otel_span.status.status_code == StatusCode.OK:
1601+
return SPANSTATUS.OK
1602+
else:
1603+
return SPANSTATUS.UNKNOWN_ERROR
1604+
15861605
def set_status(self, status):
15871606
# type: (str) -> None
15881607
if status == SPANSTATUS.OK:

tests/integrations/opentelemetry/test_potel.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from opentelemetry import trace
33

44
import sentry_sdk
5+
from sentry_sdk.consts import SPANSTATUS
56
from tests.conftest import ApproxDict
67

78

@@ -331,3 +332,35 @@ def test_potel_span_root_span_references():
331332
with sentry_sdk.start_span(description="http") as http_span:
332333
assert not http_span.is_root_span
333334
assert http_span.root_span == request_span
335+
336+
337+
@pytest.mark.parametrize(
338+
"status_in,status_out",
339+
[
340+
(None, None),
341+
("", SPANSTATUS.UNKNOWN_ERROR),
342+
(SPANSTATUS.OK, SPANSTATUS.OK),
343+
(SPANSTATUS.ABORTED, SPANSTATUS.UNKNOWN_ERROR),
344+
(SPANSTATUS.ALREADY_EXISTS, SPANSTATUS.UNKNOWN_ERROR),
345+
(SPANSTATUS.CANCELLED, SPANSTATUS.UNKNOWN_ERROR),
346+
(SPANSTATUS.DATA_LOSS, SPANSTATUS.UNKNOWN_ERROR),
347+
(SPANSTATUS.DEADLINE_EXCEEDED, SPANSTATUS.UNKNOWN_ERROR),
348+
(SPANSTATUS.FAILED_PRECONDITION, SPANSTATUS.UNKNOWN_ERROR),
349+
(SPANSTATUS.INTERNAL_ERROR, SPANSTATUS.UNKNOWN_ERROR),
350+
(SPANSTATUS.INVALID_ARGUMENT, SPANSTATUS.UNKNOWN_ERROR),
351+
(SPANSTATUS.NOT_FOUND, SPANSTATUS.UNKNOWN_ERROR),
352+
(SPANSTATUS.OUT_OF_RANGE, SPANSTATUS.UNKNOWN_ERROR),
353+
(SPANSTATUS.PERMISSION_DENIED, SPANSTATUS.UNKNOWN_ERROR),
354+
(SPANSTATUS.RESOURCE_EXHAUSTED, SPANSTATUS.UNKNOWN_ERROR),
355+
(SPANSTATUS.UNAUTHENTICATED, SPANSTATUS.UNKNOWN_ERROR),
356+
(SPANSTATUS.UNAVAILABLE, SPANSTATUS.UNKNOWN_ERROR),
357+
(SPANSTATUS.UNIMPLEMENTED, SPANSTATUS.UNKNOWN_ERROR),
358+
(SPANSTATUS.UNKNOWN_ERROR, SPANSTATUS.UNKNOWN_ERROR),
359+
],
360+
)
361+
def test_potel_span_status(status_in, status_out):
362+
span = sentry_sdk.start_span(name="test")
363+
if status_in is not None:
364+
span.set_status(status_in)
365+
366+
assert span.status == status_out

0 commit comments

Comments
 (0)