From f8f4b495b1beb568778beb2dbd368dd97d090dd0 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Mon, 11 Nov 2024 11:54:38 +0100 Subject: [PATCH] Make it more obvious how otel allows the span status to be set --- sentry_sdk/integrations/huey.py | 3 ++- sentry_sdk/tracing.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/huey.py b/sentry_sdk/integrations/huey.py index 9a3bfb9b99..433a7c17c6 100644 --- a/sentry_sdk/integrations/huey.py +++ b/sentry_sdk/integrations/huey.py @@ -166,7 +166,8 @@ def _sentry_execute(self, task, timestamp=None): source=TRANSACTION_SOURCE_TASK, origin=HueyIntegration.origin, ) as transaction: + return_value = old_execute(self, task, timestamp) transaction.set_status(SPANSTATUS.OK) - return old_execute(self, task, timestamp) + return return_value Huey._execute = _sentry_execute diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index aba7d4f49d..2bc3c9b8a7 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1541,13 +1541,17 @@ def set_attribute(self, key, value): def set_status(self, status): # type: (str) -> None if status == SPANSTATUS.OK: - otel_status = StatusCode.OK - otel_description = None + # Do not set status if it's already set. + # We would override an error status with OK. + if self._otel_span.status.status_code == StatusCode.UNSET: + self._otel_span.set_status(StatusCode.OK, None) else: - otel_status = StatusCode.ERROR - otel_description = status + # OpenTelemetry does not allow setting and error status + # if the span is already set to OK + if self._otel_span.status.status_code == StatusCode.OK: + return - self._otel_span.set_status(otel_status, otel_description) + self._otel_span.set_status(StatusCode.ERROR, status) def set_measurement(self, name, value, unit=""): # type: (str, float, MeasurementUnit) -> None