Skip to content

Commit da37f0a

Browse files
feat(bedrock): Add extension skeleton (#8226)
Add a basic Bedrock extension that creates chat spans with the `gen_ai.operation.name`, `gen_ai.provider.name` and `gen_ai.response.streaming` attributes. Clean up the extension system by removing the indirection from `registry.py`. Remove `capture_internal_exceptions()` around invocations of extension functions. Extensions should wrap relevant lines themselves. Add a `skip_if_no_active_span` boolean so that Bedrock spans are created even when there is no active span, matching the behavior of other chat integrations.
1 parent 4cf1ee5 commit da37f0a

7 files changed

Lines changed: 367 additions & 169 deletions

File tree

‎sentry_sdk/integrations/boto3/_client.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
_set_span_attributes,
1515
_start_client_span,
1616
)
17-
from sentry_sdk.integrations.boto3._services.registry import (
18-
_resolve_service,
19-
)
17+
from sentry_sdk.integrations.boto3._services.bedrock import _BedrockExtension
2018
from sentry_sdk.integrations.boto3.consts import IDENTIFIER
2119
from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan
2220
from sentry_sdk.utils import capture_internal_exceptions
@@ -104,7 +102,11 @@ def sentry_patched_make_api_call(
104102
with capture_internal_exceptions():
105103
# add optional metadata to the context, e.g. service-name, region-name, etc.
106104
ctx.add_metadata(self)
107-
service_ext = _resolve_service(ctx.service_name)
105+
106+
service_ext = None
107+
if ctx.service_name == "bedrock-runtime":
108+
service_ext = _BedrockExtension()
109+
108110
span = _start_client_span(ctx, service_ext)
109111

110112
if span is None:

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,30 +173,28 @@ def _start_client_span(
173173
# https://opentelemetry.io/docs/specs/semconv/cloud-providers/aws-sdk/#aws-sdk-spans
174174
SPANDATA.SENTRY_KIND: "client",
175175
}
176-
with capture_internal_exceptions():
177-
attributes.update(_get_client_attributes(ctx))
176+
attributes.update(_get_client_attributes(ctx))
178177

179178
# `sentry.span_op` and `sentry.span_origin` are set to generic defaults;
180179
# a service extension can override them with `get_span_op()` and `get_span_origin()`.
181180
span_op = OP.HTTP_CLIENT
182181
span_origin = ORIGIN
183182

184183
if service_ext is not None:
185-
with capture_internal_exceptions():
186-
service_op = service_ext.get_span_op(ctx)
187-
if service_op is not None:
188-
span_op = service_op
184+
service_op = service_ext.get_span_op(ctx)
185+
if service_op is not None:
186+
span_op = service_op
189187

190-
with capture_internal_exceptions():
191-
service_origin = service_ext.get_span_origin(ctx)
192-
if service_origin is not None:
193-
span_origin = service_origin
188+
service_origin = service_ext.get_span_origin(ctx)
189+
if service_origin is not None:
190+
span_origin = service_origin
194191

195-
with capture_internal_exceptions():
196-
attributes.update(service_ext.get_request_attributes(ctx))
192+
attributes.update(service_ext.get_request_attributes(ctx))
197193

198194
if has_span_streaming_enabled(client.options):
199-
if sentry_sdk.traces.get_current_span() is None:
195+
if (
196+
service_ext is None or service_ext.skip_if_no_active_span
197+
) and sentry_sdk.traces.get_current_span() is None:
200198
return None
201199

202200
# `start_span()` evaluates `ignore_spans` against the initial attributes.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class _ServiceExtension:
1616
"""
1717

1818
__slots__ = ()
19+
skip_if_no_active_span = True
1920

2021
def get_span_op(self, ctx: "AwsCallContext") -> "Optional[str]":
2122
"""Return an optional `sentry.op` override, or `None` to keep the default."""
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import TYPE_CHECKING
2+
3+
from sentry_sdk.consts import OP, SPANDATA
4+
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
5+
6+
if TYPE_CHECKING:
7+
from typing import Any, Optional
8+
9+
from sentry_sdk._types import Attributes
10+
from sentry_sdk.integrations.boto3._context import AwsCallContext
11+
12+
13+
class _BedrockExtension(_ServiceExtension):
14+
__slots__ = ()
15+
skip_if_no_active_span = False
16+
17+
def get_span_op(self, ctx: "AwsCallContext") -> "Optional[str]":
18+
if ctx.operation_name != "Converse" and ctx.operation_name != "ConverseStream":
19+
return None
20+
return OP.GEN_AI_CHAT
21+
22+
def get_span_origin(self, ctx: "AwsCallContext") -> "Optional[str]":
23+
if ctx.operation_name != "Converse" and ctx.operation_name != "ConverseStream":
24+
return None
25+
return "auto.ai.boto3"
26+
27+
def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
28+
if ctx.operation_name != "Converse" and ctx.operation_name != "ConverseStream":
29+
return {}
30+
31+
return {
32+
SPANDATA.GEN_AI_PROVIDER_NAME: "aws.bedrock",
33+
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
34+
SPANDATA.GEN_AI_RESPONSE_STREAMING: ctx.operation_name == "ConverseStream",
35+
}
36+
37+
def get_response_attributes(
38+
self,
39+
ctx: "AwsCallContext",
40+
response: "Any",
41+
) -> "Attributes":
42+
return {}

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)