Skip to content

Commit 74d8ae4

Browse files
feat(bedrock): Set token attributes for streamed responses
1 parent dd9644c commit 74d8ae4

4 files changed

Lines changed: 278 additions & 44 deletions

File tree

‎sentry_sdk/integrations/boto3/_client.py‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
try:
2929
from botocore.client import BaseClient
30-
from botocore.exceptions import ClientError
3130
except ImportError:
3231
raise DidNotEnable("botocore not installed")
3332

@@ -121,21 +120,11 @@ def sentry_patched_make_api_call(
121120
try:
122121
parsed = orig_make_api_call(self, operation_name, api_params)
123122
except BaseException as error:
124-
if service_ext is not None and isinstance(error, ClientError):
125-
with capture_internal_exceptions():
126-
attributes.update(
127-
service_ext.get_response_attributes(ctx, error.response)
128-
)
129123
with capture_internal_exceptions():
130124
attributes.update(_get_error_attributes(error))
131125
with capture_internal_exceptions():
132126
_set_span_attributes(span, attributes)
133127
raise
134-
if service_ext is not None:
135-
with capture_internal_exceptions():
136-
attributes.update(
137-
service_ext.get_response_attributes(ctx, parsed)
138-
)
139128
with capture_internal_exceptions():
140129
attributes.update(_get_response_attributes(parsed))
141130
with capture_internal_exceptions():
@@ -144,6 +133,10 @@ def sentry_patched_make_api_call(
144133
_finish_span(span, error)
145134
raise
146135

136+
if service_ext is not None:
137+
service_ext.on_success_response(ctx, parsed, span)
138+
return parsed
139+
147140
streaming_body_instrumented = False
148141
with capture_internal_exceptions():
149142
streaming_body_instrumented = _instrument_streaming_body(span, parsed)

‎sentry_sdk/integrations/boto3/_services/base.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import TYPE_CHECKING
22

33
if TYPE_CHECKING:
4-
from typing import Any, Optional
4+
from typing import Any, Optional, Union
55

66
from sentry_sdk._types import Attributes
77
from sentry_sdk.integrations.boto3._context import AwsCallContext
8+
from sentry_sdk.traces import StreamedSpan
9+
from sentry_sdk.tracing import Span
810

911

1012
class _ServiceExtension:
@@ -30,8 +32,11 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
3032
"""Return request attributes to add before the AWS request is made."""
3133
return {}
3234

33-
def get_response_attributes(
34-
self, ctx: "AwsCallContext", response: "Any"
35-
) -> "Attributes":
36-
"""Return response attributes to add after the AWS request is made."""
37-
return {}
35+
def on_success_response(
36+
self,
37+
ctx: "AwsCallContext",
38+
response: "Any",
39+
span: "Union[Span, StreamedSpan]",
40+
) -> None:
41+
"""Hook that fires after a successful response. Responsible for closing the span."""
42+
return None
Lines changed: 109 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
1+
import sys
2+
from contextlib import contextmanager
13
from typing import TYPE_CHECKING, cast
24

35
from sentry_sdk.consts import OP, SPANDATA
46
from 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

611
if 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

1553
class _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

Comments
 (0)