Skip to content

Commit ce4225d

Browse files
patch __iter__ and close()
1 parent d0233d7 commit ce4225d

3 files changed

Lines changed: 198 additions & 93 deletions

File tree

‎sentry_sdk/integrations/boto3/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
22
from sentry_sdk.integrations.boto3._client import _patch_botocore_client
3+
from sentry_sdk.integrations.boto3._services.bedrock import _patch_event_stream
34
from sentry_sdk.integrations.boto3.consts import IDENTIFIER, ORIGIN
45
from sentry_sdk.utils import parse_version
56

@@ -19,3 +20,4 @@ def setup_once() -> None:
1920
_check_minimum_version(Boto3Integration, version, "botocore")
2021

2122
_patch_botocore_client()
23+
_patch_event_stream()
Lines changed: 122 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import sys
2-
from contextlib import contextmanager
32
from typing import TYPE_CHECKING, cast
43

54
from sentry_sdk.consts import OP, SPANDATA
5+
from sentry_sdk.integrations import DidNotEnable
66
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
77
from sentry_sdk.traces import SpanStatus, StreamedSpan
88
from sentry_sdk.tracing import SPANSTATUS
9-
from sentry_sdk.utils import capture_internal_exceptions, reraise
9+
from sentry_sdk.utils import (
10+
capture_internal_exceptions,
11+
reraise,
12+
should_be_treated_as_error,
13+
)
14+
15+
try:
16+
from botocore.eventstream import EventStream
17+
except ImportError:
18+
raise DidNotEnable("botocore is not installed")
1019

1120
if TYPE_CHECKING:
1221
from typing import Any, Iterator, Optional, Union
1322

14-
from botocore.eventstream import EventStream
1523
from mypy_boto3_bedrock_runtime.type_defs import (
1624
ConverseResponseTypeDef,
17-
ConverseStreamOutputTypeDef,
1825
ConverseStreamResponseTypeDef,
1926
)
2027

@@ -23,31 +30,113 @@
2330
from sentry_sdk.tracing import Span
2431

2532

26-
@contextmanager
27-
def _stream_span_context(span: "Union[Span, StreamedSpan]"):
33+
def _finish_span(
34+
stream: "EventStream[Any]",
35+
exc_type: "Optional[type[BaseException]]",
36+
exc_val: "Optional[BaseException]",
37+
exc_tb: "Optional[Any]",
38+
) -> None:
2839
"""
29-
Closes the provided span and set the span's status on exit.
40+
Finishes the span attached to the stream.
41+
Is a no-op if the stream has no span set, i.e., when the span has already been finished.
3042
"""
31-
end_span = span.end if isinstance(span, StreamedSpan) else span.finish
32-
33-
try:
34-
yield
35-
except GeneratorExit: # Raised when close() is called on the stream.
36-
exc_info = sys.exc_info()
37-
with capture_internal_exceptions():
38-
end_span()
39-
reraise(*exc_info)
40-
except BaseException:
41-
exc_info = sys.exc_info()
42-
with capture_internal_exceptions():
43-
if isinstance(span, StreamedSpan):
44-
span.status = SpanStatus.ERROR
45-
else:
46-
span.status = SPANSTATUS.INTERNAL_ERROR
47-
end_span()
48-
reraise(*exc_info)
43+
if not hasattr(stream, "_span"):
44+
return
45+
46+
span = stream._span
47+
del stream._span
48+
49+
if should_be_treated_as_error(exc_type, exc_val):
50+
if isinstance(span, StreamedSpan):
51+
span.status = SpanStatus.ERROR
52+
else:
53+
span.status = SPANSTATUS.INTERNAL_ERROR
54+
55+
if isinstance(span, StreamedSpan):
56+
span.end()
4957
else:
50-
end_span()
58+
span.finish()
59+
60+
61+
def _patch_event_stream() -> None:
62+
original_iter = EventStream.__iter__
63+
original_close = EventStream.close
64+
65+
def sentry_patched_iter(
66+
self: "EventStream[Any]",
67+
) -> "Iterator[Any]":
68+
"""
69+
Sets information received while iterating the response stream on the AI Client Span.
70+
Responsible for closing the AI Client Span unless the span has already been closed in the close() patch.
71+
"""
72+
span = getattr(self, "_span", None)
73+
if span is None:
74+
yield from original_iter(self)
75+
return
76+
77+
set_on_span = (
78+
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
79+
)
80+
81+
try:
82+
for event in original_iter(self):
83+
if "metadata" not in event:
84+
yield event
85+
continue
86+
87+
usage = event["metadata"]["usage"]
88+
89+
input_tokens = usage["inputTokens"]
90+
set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage["outputTokens"])
91+
set_on_span(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage["totalTokens"])
92+
93+
cache_read_tokens = usage.get("cacheReadInputTokens")
94+
if cache_read_tokens is not None:
95+
input_tokens += cache_read_tokens
96+
set_on_span(
97+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, cache_read_tokens
98+
)
99+
100+
cache_write_tokens = usage.get("cacheWriteInputTokens")
101+
if cache_write_tokens is not None:
102+
input_tokens += cache_write_tokens
103+
set_on_span(
104+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE,
105+
cache_write_tokens,
106+
)
107+
108+
set_on_span(
109+
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS,
110+
input_tokens,
111+
)
112+
113+
yield event
114+
except GeneratorExit:
115+
raise # Leave span open since underlying stream is not necessarily closed.
116+
except BaseException:
117+
exc_info = sys.exc_info()
118+
with capture_internal_exceptions():
119+
_finish_span(self, *exc_info)
120+
reraise(*exc_info)
121+
122+
_finish_span(self, None, None, None)
123+
124+
def sentry_patched_close(self: "EventStream[Any]") -> None:
125+
"""
126+
Closes the AI Client Span unless `sentry_patched_iter()` closes the span first.
127+
"""
128+
try:
129+
original_close(self)
130+
except BaseException:
131+
exc_info = sys.exc_info()
132+
with capture_internal_exceptions():
133+
_finish_span(self, *exc_info)
134+
reraise(*exc_info)
135+
136+
_finish_span(self, None, None, None)
137+
138+
EventStream.__iter__ = sentry_patched_iter # type: ignore[method-assign]
139+
EventStream.close = sentry_patched_close # type: ignore[method-assign]
51140

52141

53142
class _BedrockExtension(_ServiceExtension):
@@ -115,43 +204,13 @@ def on_success_response(
115204
span.finish()
116205
return
117206

118-
if ctx.operation_name != "ConverseStream":
207+
if ctx.operation_name == "ConverseStream":
208+
stream = cast("ConverseStreamResponseTypeDef", response)["stream"]
209+
stream._span = span # type: ignore[attr-defined]
119210
return
120211

121-
stream = cast("ConverseStreamResponseTypeDef", response)["stream"]
122-
response["stream"] = _wrap_converse_stream(stream, span)
123-
124-
125-
def _wrap_converse_stream(
126-
stream: "EventStream[ConverseStreamOutputTypeDef]",
127-
span: "Union[Span, StreamedSpan]",
128-
) -> "Iterator[ConverseStreamOutputTypeDef]":
129-
set_on_span = (
130-
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
131-
)
132-
133-
with _stream_span_context(span):
134-
for event in stream:
135-
if "metadata" not in event:
136-
yield event
137-
continue
138-
139-
usage = event["metadata"]["usage"]
140-
141-
set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage["inputTokens"])
142-
set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage["outputTokens"])
143-
set_on_span(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage["totalTokens"])
144-
145-
cache_read_tokens = usage.get("cacheReadInputTokens")
146-
if cache_read_tokens is not None:
147-
set_on_span(
148-
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, cache_read_tokens
149-
)
150-
151-
cache_write_tokens = usage.get("cacheWriteInputTokens")
152-
if cache_write_tokens is not None:
153-
set_on_span(
154-
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE, cache_write_tokens
155-
)
212+
if isinstance(span, StreamedSpan):
213+
span.end()
214+
return
156215

157-
yield event
216+
span.finish()

0 commit comments

Comments
 (0)