Skip to content

Commit b64e583

Browse files
fix(openai): Stop setting transaction status when child span fails
1 parent 624cfbf commit b64e583

2 files changed

Lines changed: 4 additions & 11 deletions

File tree

sentry_sdk/integrations/openai.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from sentry_sdk.consts import SPANDATA
2727
from sentry_sdk.integrations import DidNotEnable, Integration
2828
from sentry_sdk.scope import should_send_default_pii
29-
from sentry_sdk.tracing_utils import set_span_errored
3029
from sentry_sdk.utils import (
3130
capture_internal_exceptions,
3231
event_from_exception,
@@ -133,11 +132,6 @@ def count_tokens(self: "OpenAIIntegration", s: str) -> int:
133132

134133

135134
def _capture_exception(exc: "Any") -> None:
136-
# Close an eventually open span
137-
# We need to do this by hand because we are not using the start_span context manager
138-
current_span = sentry_sdk.get_current_span()
139-
set_span_errored(current_span)
140-
141135
event, hint = event_from_exception(
142136
exc,
143137
client_options=sentry_sdk.get_client().options,
@@ -697,7 +691,7 @@ def _new_sync_chat_completion(f: "Any", *args: "Any", **kwargs: "Any") -> "Any":
697691
exc_info = sys.exc_info()
698692
with capture_internal_exceptions():
699693
_capture_exception(exc)
700-
span.__exit__(None, None, None)
694+
span.__exit__(*exc_info)
701695
reraise(*exc_info)
702696

703697
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
@@ -781,7 +775,7 @@ async def _new_async_chat_completion(f: "Any", *args: "Any", **kwargs: "Any") ->
781775
exc_info = sys.exc_info()
782776
with capture_internal_exceptions():
783777
_capture_exception(exc)
784-
span.__exit__(None, None, None)
778+
span.__exit__(*exc_info)
785779
reraise(*exc_info)
786780

787781
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
@@ -1280,7 +1274,7 @@ def _new_sync_responses_create(f: "Any", *args: "Any", **kwargs: "Any") -> "Any"
12801274
exc_info = sys.exc_info()
12811275
with capture_internal_exceptions():
12821276
_capture_exception(exc)
1283-
span.__exit__(None, None, None)
1277+
span.__exit__(*exc_info)
12841278
reraise(*exc_info)
12851279

12861280
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.
@@ -1354,7 +1348,7 @@ async def _new_async_responses_create(f: "Any", *args: "Any", **kwargs: "Any") -
13541348
exc_info = sys.exc_info()
13551349
with capture_internal_exceptions():
13561350
_capture_exception(exc)
1357-
span.__exit__(None, None, None)
1351+
span.__exit__(*exc_info)
13581352
reraise(*exc_info)
13591353

13601354
# Attribute check to fail gracefully if the attribute is not present in future `openai` versions.

tests/integrations/openai/test_openai.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,6 @@ def test_span_status_error(sentry_init, capture_events):
15081508
assert error["level"] == "error"
15091509
assert transaction["spans"][0]["status"] == "internal_error"
15101510
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
1511-
assert transaction["contexts"]["trace"]["status"] == "internal_error"
15121511

15131512

15141513
@pytest.mark.asyncio

0 commit comments

Comments
 (0)