Skip to content

Commit 2212084

Browse files
feat(boto3): Instrument Bedrock Runtime Converse calls as gen_ai spans
Record Bedrock Runtime `Converse` calls as `gen_ai.chat` spans with the `auto.ai.boto3` origin, the request model and inference parameters, token usage, and finish reasons. Input and output messages, system instructions, and tool definitions are recorded when `data_collection` allows gen_ai inputs/outputs, or with `send_default_pii` when `data_collection` isn't set. Adds a `get_span_name()` hook to `_ServiceExtension`. Refs #5470 Co-Authored-By: David Cramer <23610+dcramer@users.noreply.github.com>
1 parent dbc50bd commit 2212084

8 files changed

Lines changed: 825 additions & 5 deletions

File tree

‎sentry_sdk/_types.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,16 +479,22 @@ class ReasoningPart(TypedDict):
479479

480480
class ToolCallPart(TypedDict):
481481
type: Literal["tool_call"]
482+
id: NotRequired[str]
482483
name: NotRequired[str]
483484
arguments: NotRequired[Any]
484485

486+
class ToolCallResponsePart(TypedDict):
487+
type: Literal["tool_call_response"]
488+
id: NotRequired[str]
489+
result: NotRequired[Any]
490+
485491
class InputMessage(TypedDict):
486492
role: Literal["user", "assistant", "tool"]
487-
parts: list[Union[TextPart, ReasoningPart, ToolCallPart]]
493+
parts: list[Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart]]
488494

489495
class OutputMessage(TypedDict):
490496
role: Literal["assistant"]
491-
parts: list[Union[TextPart, ReasoningPart]]
497+
parts: list[Union[TextPart, ReasoningPart, ToolCallPart]]
492498

493499
class ToolDefinition(TypedDict):
494500
type: str

‎sentry_sdk/consts.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,12 @@ class SPANDATA:
733733
Example: "1234567890"
734734
"""
735735

736+
GEN_AI_REQUEST_STOP_SEQUENCES = "gen_ai.request.stop_sequences"
737+
"""
738+
List of sequences that the model will use to stop generating further tokens.
739+
Example: ["forest", "lived"]
740+
"""
741+
736742
GEN_AI_REQUEST_TEMPERATURE = "gen_ai.request.temperature"
737743
"""
738744
The temperature parameter used to control randomness in the output.

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,18 @@ def _start_client_span(
176176
with capture_internal_exceptions():
177177
attributes.update(_get_client_attributes(ctx))
178178

179-
# `sentry.span_op` and `sentry.span_origin` are set to generic defaults;
180-
# a service extension can override them with `get_span_op()` and `get_span_origin()`.
179+
# the span name, `sentry.span_op`, and `sentry.span_origin` are set to generic defaults;
180+
# a service extension can override them with `get_span_name()`, `get_span_op()`, and
181+
# `get_span_origin()`.
181182
span_op = OP.HTTP_CLIENT
182183
span_origin = ORIGIN
183184

184185
if service_ext is not None:
186+
with capture_internal_exceptions():
187+
service_span_name = service_ext.get_span_name(ctx)
188+
if service_span_name is not None:
189+
span_name = service_span_name
190+
185191
with capture_internal_exceptions():
186192
service_op = service_ext.get_span_op(ctx)
187193
if service_op is not None:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class _ServiceExtension:
1717

1818
__slots__ = ()
1919

20+
def get_span_name(self, ctx: "AwsCallContext") -> "Optional[str]":
21+
"""Return an optional span name override, or `None` to keep the default."""
22+
return None
23+
2024
def get_span_op(self, ctx: "AwsCallContext") -> "Optional[str]":
2125
"""Return an optional `sentry.op` override, or `None` to keep the default."""
2226
return None

0 commit comments

Comments
 (0)