diff --git a/sentry_sdk/integrations/huggingface_hub.py b/sentry_sdk/integrations/huggingface_hub.py index 733715621d..83a338527f 100644 --- a/sentry_sdk/integrations/huggingface_hub.py +++ b/sentry_sdk/integrations/huggingface_hub.py @@ -1,22 +1,20 @@ -import sys import inspect +import sys from functools import wraps +from typing import TYPE_CHECKING import sentry_sdk from sentry_sdk.ai.monitoring import record_token_usage from sentry_sdk.ai.utils import set_data_normalized -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing_utils import set_span_errored from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, reraise, ) -from typing import TYPE_CHECKING - if TYPE_CHECKING: from typing import Any, Callable, Iterable @@ -52,9 +50,9 @@ def setup_once() -> None: ) -def _capture_exception(exc: "Any") -> None: - set_span_errored() - +def _capture_exception(exc: "Any", span: "Any" = None) -> None: + if span is not None: + span.set_status(SPANSTATUS.INTERNAL_ERROR) event, hint = event_from_exception( exc, client_options=sentry_sdk.get_client().options, @@ -130,7 +128,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": except Exception as e: exc_info = sys.exc_info() with capture_internal_exceptions(): - _capture_exception(e) + _capture_exception(e, span) span.__exit__(None, None, None) reraise(*exc_info) diff --git a/tests/integrations/huggingface_hub/test_huggingface_hub.py b/tests/integrations/huggingface_hub/test_huggingface_hub.py index e4cf6e9dcc..9dd15ca4b5 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -1,15 +1,14 @@ +import re +from typing import TYPE_CHECKING from unittest import mock + import pytest -import re import responses - from huggingface_hub import InferenceClient import sentry_sdk -from sentry_sdk.utils import package_version from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration - -from typing import TYPE_CHECKING +from sentry_sdk.utils import package_version try: from huggingface_hub.utils._errors import HfHubHTTPError @@ -834,8 +833,6 @@ def test_span_status_error( assert span["status"] == "internal_error" assert span["tags"]["status"] == "internal_error" - assert transaction["contexts"]["trace"]["status"] == "internal_error" - @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @pytest.mark.parametrize("send_default_pii", [True, False])