1+ import sys
2+ from contextlib import contextmanager
13from typing import TYPE_CHECKING , cast
24
35from sentry_sdk .consts import OP , SPANDATA
46from sentry_sdk .integrations .boto3 ._services .base import _ServiceExtension
7+ from sentry_sdk .traces import SpanStatus , StreamedSpan
8+ from sentry_sdk .tracing import SPANSTATUS
9+ from sentry_sdk .utils import capture_internal_exceptions , reraise
510
611if TYPE_CHECKING :
7- from typing import Any , Optional
12+ from typing import Any , Iterator , Optional , Union
813
9- from mypy_boto3_bedrock_runtime .type_defs import ConverseResponseTypeDef
14+ from botocore .eventstream import EventStream
15+ from mypy_boto3_bedrock_runtime .type_defs import (
16+ ConverseResponseTypeDef ,
17+ ConverseStreamOutputTypeDef ,
18+ ConverseStreamResponseTypeDef ,
19+ )
1020
1121 from sentry_sdk ._types import Attributes
1222 from sentry_sdk .integrations .boto3 ._context import AwsCallContext
23+ from sentry_sdk .tracing import Span
24+
25+
26+ @contextmanager
27+ def _stream_span_context (span : "Union[Span, StreamedSpan]" ):
28+ """
29+ Closes the provided span and set the span's status on exit.
30+ """
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 )
49+ else :
50+ end_span ()
1351
1452
1553class _BedrockExtension (_ServiceExtension ):
@@ -36,29 +74,79 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
3674 SPANDATA .GEN_AI_RESPONSE_STREAMING : ctx .operation_name == "ConverseStream" ,
3775 }
3876
39- def get_response_attributes (
77+ def on_success_response (
4078 self ,
4179 ctx : "AwsCallContext" ,
4280 response : "Any" ,
43- ) -> "Attributes" :
44- if ctx .operation_name != "Converse" or "Error" in response :
45- return {}
81+ span : "Union[Span, StreamedSpan]" ,
82+ ) -> None :
83+ set_on_span = (
84+ span .set_attribute if isinstance (span , StreamedSpan ) else span .set_data
85+ )
4686
47- usage = cast ("ConverseResponseTypeDef" , response )["usage" ]
48- attributes : "Attributes" = {
49- SPANDATA .GEN_AI_USAGE_INPUT_TOKENS : usage ["inputTokens" ],
50- SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS : usage ["outputTokens" ],
51- SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS : usage ["totalTokens" ],
52- }
87+ if ctx .operation_name == "Converse" :
88+ usage = cast ("ConverseResponseTypeDef" , response )["usage" ]
89+
90+ set_on_span (SPANDATA .GEN_AI_USAGE_INPUT_TOKENS , usage ["inputTokens" ])
91+ set_on_span (SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , usage ["outputTokens" ])
92+ set_on_span (SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage ["totalTokens" ])
93+
94+ cache_read_tokens = usage .get ("cacheReadInputTokens" )
95+ if cache_read_tokens is not None :
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+ set_on_span (
103+ SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE , cache_write_tokens
104+ )
105+
106+ if isinstance (span , StreamedSpan ):
107+ span .end ()
108+ return
109+
110+ span .finish ()
111+ return
112+
113+ if ctx .operation_name != "ConverseStream" :
114+ return
115+
116+ stream = cast ("ConverseStreamResponseTypeDef" , response )["stream" ]
117+ response ["stream" ] = _wrap_converse_stream (stream , span )
118+
119+
120+ def _wrap_converse_stream (
121+ stream : "EventStream[ConverseStreamOutputTypeDef]" ,
122+ span : "Union[Span, StreamedSpan]" ,
123+ ) -> "Iterator[ConverseStreamOutputTypeDef]" :
124+ set_on_span = (
125+ span .set_attribute if isinstance (span , StreamedSpan ) else span .set_data
126+ )
127+
128+ with _stream_span_context (span ):
129+ for event in stream :
130+ if "metadata" not in event :
131+ yield event
132+ continue
133+
134+ usage = event ["metadata" ]["usage" ]
135+
136+ set_on_span (SPANDATA .GEN_AI_USAGE_INPUT_TOKENS , usage ["inputTokens" ])
137+ set_on_span (SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , usage ["outputTokens" ])
138+ set_on_span (SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage ["totalTokens" ])
53139
54- cache_read_tokens = usage .get ("cacheReadInputTokens" )
55- if cache_read_tokens is not None :
56- attributes [SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHED ] = cache_read_tokens
140+ cache_read_tokens = usage .get ("cacheReadInputTokens" )
141+ if cache_read_tokens is not None :
142+ set_on_span (
143+ SPANDATA .GEN_AI_USAGE_INPUT_TOKENS_CACHED , cache_read_tokens
144+ )
57145
58- cache_write_tokens = usage .get ("cacheWriteInputTokens" )
59- if cache_write_tokens is not None :
60- attributes [ SPANDATA . GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE ] = (
61- cache_write_tokens
62- )
146+ cache_write_tokens = usage .get ("cacheWriteInputTokens" )
147+ if cache_write_tokens is not None :
148+ set_on_span (
149+ SPANDATA . GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE , cache_write_tokens
150+ )
63151
64- return attributes
152+ yield event
0 commit comments