|
1 | 1 | import sys |
2 | | -from contextlib import contextmanager |
3 | 2 | from typing import TYPE_CHECKING, cast |
4 | 3 |
|
5 | 4 | from sentry_sdk.consts import OP, SPANDATA |
| 5 | +from sentry_sdk.integrations import DidNotEnable |
6 | 6 | from sentry_sdk.integrations.boto3._services.base import _ServiceExtension |
7 | 7 | from sentry_sdk.traces import SpanStatus, StreamedSpan |
8 | 8 | 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") |
10 | 19 |
|
11 | 20 | if TYPE_CHECKING: |
12 | 21 | from typing import Any, Iterator, Optional, Union |
13 | 22 |
|
14 | | - from botocore.eventstream import EventStream |
15 | 23 | from mypy_boto3_bedrock_runtime.type_defs import ( |
16 | 24 | ConverseResponseTypeDef, |
17 | | - ConverseStreamOutputTypeDef, |
18 | 25 | ConverseStreamResponseTypeDef, |
19 | 26 | ) |
20 | 27 |
|
|
23 | 30 | from sentry_sdk.tracing import Span |
24 | 31 |
|
25 | 32 |
|
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: |
28 | 39 | """ |
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. |
30 | 42 | """ |
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() |
49 | 57 | 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] |
51 | 140 |
|
52 | 141 |
|
53 | 142 | class _BedrockExtension(_ServiceExtension): |
@@ -115,43 +204,13 @@ def on_success_response( |
115 | 204 | span.finish() |
116 | 205 | return |
117 | 206 |
|
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] |
119 | 210 | return |
120 | 211 |
|
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 |
156 | 215 |
|
157 | | - yield event |
| 216 | + span.finish() |
0 commit comments