Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arq tests in POTel #3875

Open
wants to merge 5 commits into
base: potel-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sentry_sdk/integrations/arq.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys

from opentelemetry.trace.status import StatusCode

import sentry_sdk
from sentry_sdk.consts import OP, SPANSTATUS
from sentry_sdk.integrations import DidNotEnable, Integration
Expand Down Expand Up @@ -116,7 +118,10 @@ async def _sentry_run_job(self, job_id, score):
origin=ArqIntegration.origin,
) as span:
return_value = await old_run_job(self, job_id, score)
span.set_status(SPANSTATUS.OK)

if span.status is None:
span.set_status(SPANSTATUS.OK)

return return_value

Worker.run_job = _sentry_run_job
Expand Down
24 changes: 24 additions & 0 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,30 @@ def set_attribute(self, key, value):

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

@property
def status(self):
# type: () -> Optional[str]
"""
Return the Sentry `SPANSTATUS` corresponding to the underlying OTel status.
Because differences in possible values in OTel `StatusCode` and
Sentry `SPANSTATUS` it can not be guaranteed that the status
set in `set_status()` will be the same as the one returned here.
"""
if not hasattr(self._otel_span, "status"):
return None

if self._otel_span.status.status_code == StatusCode.UNSET:
return None
elif self._otel_span.status.status_code == StatusCode.OK:
return SPANSTATUS.OK
else:
return SPANSTATUS.UNKNOWN_ERROR

@status.setter
def status(self, value):
# type: (Optional[Any]) -> None
raise NotImplementedError("Use set_status instead")
Comment on lines +1605 to +1608
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's just get rid of this altogether.


def set_status(self, status):
# type: (str) -> None
if status == SPANSTATUS.OK:
Expand Down
33 changes: 33 additions & 0 deletions tests/integrations/opentelemetry/test_potel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from opentelemetry import trace

import sentry_sdk
from sentry_sdk.consts import SPANSTATUS
from tests.conftest import ApproxDict


Expand Down Expand Up @@ -331,3 +332,35 @@ def test_potel_span_root_span_references():
with sentry_sdk.start_span(description="http") as http_span:
assert not http_span.is_root_span
assert http_span.root_span == request_span


@pytest.mark.parametrize(
"status_in,status_out",
[
(None, None),
("", SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.OK, SPANSTATUS.OK),
(SPANSTATUS.ABORTED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.ALREADY_EXISTS, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.CANCELLED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.DATA_LOSS, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.DEADLINE_EXCEEDED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.FAILED_PRECONDITION, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.INTERNAL_ERROR, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.INVALID_ARGUMENT, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.NOT_FOUND, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.OUT_OF_RANGE, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.PERMISSION_DENIED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.RESOURCE_EXHAUSTED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.UNAUTHENTICATED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.UNAVAILABLE, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.UNIMPLEMENTED, SPANSTATUS.UNKNOWN_ERROR),
(SPANSTATUS.UNKNOWN_ERROR, SPANSTATUS.UNKNOWN_ERROR),
],
)
def test_potel_span_status(status_in, status_out):
span = sentry_sdk.start_span(name="test")
if status_in is not None:
span.set_status(status_in)

assert span.status == status_out
Loading