Skip to content

Commit 1197d69

Browse files
feat(boto3): Truncate Bedrock input messages like the other gen_ai integrations
When gen_ai spans are sent in transactions (`should_truncate_gen_ai_input()`), keep only the last input message with `truncate_and_annotate_messages()`, like the Anthropic and OpenAI integrations, so long histories don't exceed the transaction size limit. The helper needs the started span, so request data is now added through a new `_ServiceExtension.get_span_start_attributes()` hook. Refs #5470 Co-Authored-By: Sergiy Dybskiy <3760543+sergical@users.noreply.github.com>
1 parent 2bd07e4 commit 1197d69

4 files changed

Lines changed: 97 additions & 14 deletions

File tree

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,19 @@ def _start_client_span(
213213
SPANDATA.SENTRY_ORIGIN: span_origin,
214214
}
215215
)
216-
return sentry_sdk.traces.start_span(
216+
streamed_span = sentry_sdk.traces.start_span(
217217
name=span_name,
218218
attributes=attributes,
219219
# `StreamingBody` responses outlive `_make_api_call()`. `_activate_client_span()`
220220
# activates this span only while the call itself runs.
221221
active=False,
222222
)
223+
if service_ext is not None:
224+
with capture_internal_exceptions():
225+
streamed_span.set_attributes(
226+
service_ext.get_span_start_attributes(ctx, streamed_span)
227+
)
228+
return streamed_span
223229

224230
span = sentry_sdk.start_span(
225231
name=span_name,
@@ -228,6 +234,9 @@ def _start_client_span(
228234
)
229235
with capture_internal_exceptions():
230236
_set_span_attributes(span, attributes)
237+
if service_ext is not None:
238+
with capture_internal_exceptions():
239+
_set_span_attributes(span, service_ext.get_span_start_attributes(ctx, span))
231240
with capture_internal_exceptions():
232241
if ctx.service_id_hyphenized is not None:
233242
span.set_tag("aws.service_id", ctx.service_id_hyphenized)

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

Lines changed: 13 additions & 1 deletion
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:
@@ -33,6 +35,16 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
3335
"""Return request attributes to add before the AWS request is made."""
3436
return {}
3537

38+
def get_span_start_attributes(
39+
self, ctx: "AwsCallContext", span: "Union[Span, StreamedSpan]"
40+
) -> "Attributes":
41+
"""
42+
Return request attributes that need the started span, e.g. truncated
43+
`gen_ai` messages. Unlike `get_request_attributes()`, they're added after
44+
the span starts, so `ignore_spans` doesn't see them.
45+
"""
46+
return {}
47+
3648
def get_response_attributes(
3749
self, ctx: "AwsCallContext", response: "Any"
3850
) -> "Attributes":

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from typing import TYPE_CHECKING, cast
44

55
import sentry_sdk
6+
from sentry_sdk.ai.utils import truncate_and_annotate_messages
67
from sentry_sdk.consts import OP, SPANDATA
78
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
89
from sentry_sdk.integrations.boto3.consts import AI_ORIGIN, IDENTIFIER
910
from sentry_sdk.scope import should_send_default_pii
11+
from sentry_sdk.tracing_utils import should_truncate_gen_ai_input
1012
from sentry_sdk.utils import capture_internal_exceptions, has_data_collection_enabled
1113

1214
if TYPE_CHECKING:
@@ -24,6 +26,8 @@
2426
)
2527
from sentry_sdk.integrations.boto3 import Boto3Integration
2628
from sentry_sdk.integrations.boto3._context import AwsCallContext
29+
from sentry_sdk.traces import StreamedSpan
30+
from sentry_sdk.tracing import Span
2731

2832
ContentPart = Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart]
2933

@@ -82,11 +86,16 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
8286
if model_id:
8387
attributes[SPANDATA.GEN_AI_REQUEST_MODEL] = model_id
8488

85-
with capture_internal_exceptions():
86-
attributes.update(_get_converse_request_attributes(ctx.params))
87-
8889
return attributes
8990

91+
def get_span_start_attributes(
92+
self, ctx: "AwsCallContext", span: "Union[Span, StreamedSpan]"
93+
) -> "Attributes":
94+
if ctx.operation_name not in GEN_AI_OPERATIONS:
95+
return {}
96+
97+
return _get_converse_request_attributes(span, ctx.params)
98+
9099
def get_response_attributes(
91100
self, ctx: "AwsCallContext", response: "Any"
92101
) -> "Attributes":
@@ -114,7 +123,9 @@ def _should_record_gen_ai(kind: 'Literal["inputs", "outputs"]') -> bool:
114123
)
115124

116125

117-
def _get_converse_request_attributes(params: "Mapping[str, Any]") -> "Attributes":
126+
def _get_converse_request_attributes(
127+
span: "Union[Span, StreamedSpan]", params: "Mapping[str, Any]"
128+
) -> "Attributes":
118129
"""
119130
https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_RequestSyntax
120131
"""
@@ -148,9 +159,11 @@ def _get_converse_request_attributes(params: "Mapping[str, Any]") -> "Attributes
148159
)
149160

150161
with capture_internal_exceptions():
151-
input_messages = _transform_input_messages(params.get("messages"))
152-
if input_messages:
153-
attributes[SPANDATA.GEN_AI_INPUT_MESSAGES] = json.dumps(input_messages)
162+
attributes.update(
163+
_get_input_messages_attributes(
164+
span, _transform_input_messages(params.get("messages"))
165+
)
166+
)
154167

155168
with capture_internal_exceptions():
156169
tool_config = params.get("toolConfig")
@@ -164,6 +177,28 @@ def _get_converse_request_attributes(params: "Mapping[str, Any]") -> "Attributes
164177
return attributes
165178

166179

180+
def _get_input_messages_attributes(
181+
span: "Union[Span, StreamedSpan]", input_messages: "list[InputMessage]"
182+
) -> "Attributes":
183+
"""
184+
Like the other `gen_ai` integrations, only the last message is kept when
185+
`gen_ai` spans are sent in transactions, so that long histories don't exceed
186+
the transaction size limit.
187+
"""
188+
messages: "Optional[list[dict[str, Any]]]" = cast(
189+
"list[dict[str, Any]]", input_messages
190+
)
191+
if should_truncate_gen_ai_input(sentry_sdk.get_client().options):
192+
messages = truncate_and_annotate_messages(
193+
messages, span, sentry_sdk.get_current_scope()
194+
)
195+
196+
if not messages:
197+
return {}
198+
199+
return {SPANDATA.GEN_AI_INPUT_MESSAGES: json.dumps(messages)}
200+
201+
167202
def _get_converse_response_attributes(response: "Any") -> "Attributes":
168203
"""
169204
https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_ResponseSyntax

‎tests/integrations/boto3/test_bedrock_runtime.py‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ def _capture_spans(capture_items, trace_mode, call):
154154
return spans
155155

156156

157+
def _expected_input_messages(trace_mode, input_messages):
158+
"""
159+
Like the other `gen_ai` integrations, only the last input message is kept when
160+
`gen_ai` spans are sent in transactions.
161+
"""
162+
if trace_mode.get("stream_gen_ai_spans") is False:
163+
return input_messages[-1:]
164+
return input_messages
165+
166+
157167
def _get_bedrock_span(spans, origin=AI_ORIGIN):
158168
(span,) = (
159169
span
@@ -181,7 +191,11 @@ def test_converse(
181191
params = {
182192
"modelId": MODEL_ID,
183193
"system": [{"text": "You are a helpful assistant."}],
184-
"messages": [{"role": "user", "content": [{"text": "Hello!"}]}],
194+
"messages": [
195+
{"role": "user", "content": [{"text": "Hi!"}]},
196+
{"role": "assistant", "content": [{"text": "Hi! What do you need?"}]},
197+
{"role": "user", "content": [{"text": "Hello!"}]},
198+
],
185199
"inferenceConfig": {
186200
"maxTokens": 100,
187201
"temperature": 0.5,
@@ -243,9 +257,19 @@ def test_converse(
243257
assert json.loads(attributes[SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) == [
244258
{"type": "text", "content": "You are a helpful assistant."}
245259
]
246-
assert json.loads(attributes[SPANDATA.GEN_AI_INPUT_MESSAGES]) == [
247-
{"role": "user", "parts": [{"type": "text", "content": "Hello!"}]}
248-
]
260+
assert json.loads(
261+
attributes[SPANDATA.GEN_AI_INPUT_MESSAGES]
262+
) == _expected_input_messages(
263+
trace_mode,
264+
[
265+
{"role": "user", "parts": [{"type": "text", "content": "Hi!"}]},
266+
{
267+
"role": "assistant",
268+
"parts": [{"type": "text", "content": "Hi! What do you need?"}],
269+
},
270+
{"role": "user", "parts": [{"type": "text", "content": "Hello!"}]},
271+
],
272+
)
249273
assert json.loads(attributes[SPANDATA.GEN_AI_TOOL_DEFINITIONS]) == [
250274
{
251275
"type": "function",
@@ -343,7 +367,7 @@ def test_converse_tool_use_and_reasoning(init_bedrock, capture_items, trace_mode
343367
)
344368

345369
attributes = _get_bedrock_span(spans)["attributes"]
346-
assert json.loads(attributes[SPANDATA.GEN_AI_INPUT_MESSAGES]) == [
370+
expected_input_messages = [
347371
{"role": "user", "parts": [{"type": "text", "content": "Weather in Paris?"}]},
348372
{
349373
"role": "assistant",
@@ -370,6 +394,9 @@ def test_converse_tool_use_and_reasoning(init_bedrock, capture_items, trace_mode
370394
# media content isn't recorded.
371395
{"role": "user", "parts": [{"type": "text", "content": "And in London?"}]},
372396
]
397+
assert json.loads(
398+
attributes[SPANDATA.GEN_AI_INPUT_MESSAGES]
399+
) == _expected_input_messages(trace_mode, expected_input_messages)
373400
assert json.loads(attributes[SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [
374401
{
375402
"role": "assistant",

0 commit comments

Comments
 (0)