Skip to content

Commit 88cea40

Browse files
merge master
2 parents 51c9190 + 843958d commit 88cea40

20 files changed

Lines changed: 3417 additions & 2617 deletions

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typing = [
8282
"openai-agents>=0.22.0",
8383
"mistralai>=2.10.1",
8484
"typesafe-sdk>=0.7.5",
85+
"mypy-boto3-bedrock-runtime>=1.43.62",
8586
]
8687
test = [
8788
"dataclasses ; python_full_version < '3.7'",

‎sentry_sdk/integrations/boto3/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
22
from sentry_sdk.integrations.boto3._client import _patch_botocore_client
3+
from sentry_sdk.integrations.boto3._services.bedrock import _patch_event_stream
34
from sentry_sdk.integrations.boto3.consts import IDENTIFIER, ORIGIN
45
from sentry_sdk.utils import parse_version
56

@@ -19,3 +20,4 @@ def setup_once() -> None:
1920
_check_minimum_version(Boto3Integration, version, "botocore")
2021

2122
_patch_botocore_client()
23+
_patch_event_stream()

‎sentry_sdk/integrations/boto3/_client.py‎

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import TYPE_CHECKING
33

44
import sentry_sdk
5-
from sentry_sdk.consts import SPANDATA
65
from sentry_sdk.integrations import DidNotEnable
76
from sentry_sdk.integrations.boto3._context import AwsCallContext
87
from sentry_sdk.integrations.boto3._instrumentation import (
@@ -14,21 +13,31 @@
1413
_sentry_request_created,
1514
_start_client_span,
1615
)
17-
from sentry_sdk.integrations.boto3._services.registry import (
18-
_resolve_service,
16+
from sentry_sdk.integrations.boto3._services.bedrock import _BedrockExtension
17+
from sentry_sdk.integrations.boto3._services.dynamodb import _DynamoDBExtension
18+
from sentry_sdk.integrations.boto3._services.kinesis import _KinesisExtension
19+
from sentry_sdk.integrations.boto3._services.lambda_ import _LambdaExtension
20+
from sentry_sdk.integrations.boto3._services.s3 import _S3Extension
21+
from sentry_sdk.integrations.boto3._services.secretsmanager import (
22+
_SecretsManagerExtension,
23+
)
24+
from sentry_sdk.integrations.boto3._services.sns import _SNSExtension
25+
from sentry_sdk.integrations.boto3._services.sqs import _SQSExtension
26+
from sentry_sdk.integrations.boto3._services.stepfunctions import (
27+
_StepFunctionsExtension,
1928
)
2029
from sentry_sdk.integrations.boto3.consts import IDENTIFIER
21-
from sentry_sdk.traces import NoOpSpan, Span, SpanStatus
30+
from sentry_sdk.traces import NoOpSpan, Span
2231
from sentry_sdk.utils import capture_internal_exceptions
2332

2433
if TYPE_CHECKING:
2534
from typing import Any, Dict, Iterator, Optional
2635

2736
from sentry_sdk._types import Attributes
37+
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
2838

2939
try:
3040
from botocore.client import BaseClient
31-
from botocore.exceptions import ClientError
3241
except ImportError:
3342
raise DidNotEnable("botocore not installed")
3443

@@ -94,7 +103,27 @@ def sentry_patched_make_api_call(
94103
with capture_internal_exceptions():
95104
# add optional metadata to the context, e.g. service-name, region-name, etc.
96105
ctx.add_metadata(self)
97-
service_ext = _resolve_service(ctx.service_name)
106+
107+
service_ext: "Optional[_ServiceExtension]" = None
108+
if ctx.service_name == "bedrock-runtime":
109+
service_ext = _BedrockExtension()
110+
elif ctx.service_name == "dynamodb":
111+
service_ext = _DynamoDBExtension()
112+
elif ctx.service_name == "kinesis":
113+
service_ext = _KinesisExtension()
114+
elif ctx.service_name == "lambda":
115+
service_ext = _LambdaExtension()
116+
elif ctx.service_name == "s3":
117+
service_ext = _S3Extension()
118+
elif ctx.service_name == "secretsmanager":
119+
service_ext = _SecretsManagerExtension()
120+
elif ctx.service_name == "sns":
121+
service_ext = _SNSExtension()
122+
elif ctx.service_name == "sqs":
123+
service_ext = _SQSExtension()
124+
elif ctx.service_name == "stepfunctions":
125+
service_ext = _StepFunctionsExtension()
126+
98127
span = _start_client_span(ctx, service_ext)
99128

100129
if span is None:
@@ -107,40 +136,30 @@ def sentry_patched_make_api_call(
107136
try:
108137
parsed = orig_make_api_call(self, operation_name, api_params)
109138
except BaseException as error:
110-
if service_ext is not None and isinstance(error, ClientError):
111-
with capture_internal_exceptions():
112-
attributes.update(
113-
service_ext.get_response_attributes(ctx, error.response)
114-
)
115139
with capture_internal_exceptions():
116140
attributes.update(_get_error_attributes(error))
117141
with capture_internal_exceptions():
118142
span.set_attributes(attributes)
119143
raise
120-
if service_ext is not None:
121-
with capture_internal_exceptions():
122-
attributes.update(
123-
service_ext.get_response_attributes(ctx, parsed)
124-
)
125144
with capture_internal_exceptions():
126145
attributes.update(_get_response_attributes(parsed))
127146
with capture_internal_exceptions():
128147
span.set_attributes(attributes)
129-
# a service extension reports a failure that didn't raise, i.e. one
130-
# in a successful response, with `error.type`.
131-
if SPANDATA.ERROR_TYPE in attributes:
132-
span.status = SpanStatus.ERROR
133148
except BaseException as error:
134149
_finish_span(span, error)
135150
raise
136151

152+
if service_ext is not None:
153+
service_ext.on_success_response(ctx, parsed, span)
154+
return parsed
155+
137156
streaming_body_instrumented = False
138157
with capture_internal_exceptions():
139158
streaming_body_instrumented = _instrument_streaming_body(span, parsed)
140159

141160
# `StreamingBody`s finish their span when consumed or closed.
142161
if not streaming_body_instrumented:
143-
_finish_span(span)
162+
span.end()
144163
return parsed
145164

146165
BaseClient.__init__ = sentry_patched_init # type: ignore

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def _start_client_span(
150150
ctx: "AwsCallContext",
151151
service_ext: "Optional[_ServiceExtension]" = None,
152152
) -> "Optional[Span]":
153-
if sentry_sdk.get_current_span() is None:
153+
if (
154+
service_ext is None or service_ext.skip_if_no_active_span
155+
) and sentry_sdk.get_current_span() is None:
154156
return None
155157

156158
# https://opentelemetry.io/docs/specs/semconv/cloud-providers/aws-sdk/#aws-sdk-spans
@@ -164,21 +166,18 @@ def _start_client_span(
164166
# https://opentelemetry.io/docs/specs/semconv/cloud-providers/aws-sdk/#aws-sdk-spans
165167
SPANDATA.SENTRY_KIND: "client",
166168
}
167-
with capture_internal_exceptions():
168-
attributes.update(_get_client_attributes(ctx))
169+
attributes.update(_get_client_attributes(ctx))
169170

170171
# `sentry.op` is set to a generic default, which a service extension can override
171172
# with `get_span_op()`.
172173
span_op = OP.RPC
173174

174175
if service_ext is not None:
175-
with capture_internal_exceptions():
176-
service_op = service_ext.get_span_op(ctx)
177-
if service_op is not None:
178-
span_op = service_op
176+
service_op = service_ext.get_span_op(ctx)
177+
if service_op is not None:
178+
span_op = service_op
179179

180-
with capture_internal_exceptions():
181-
attributes.update(service_ext.get_request_attributes(ctx))
180+
attributes.update(service_ext.get_request_attributes(ctx))
182181

183182
# `start_span()` evaluates `ignore_spans` against the initial attributes.
184183
# https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#rpc-client-span

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from sentry_sdk._types import Attributes
77
from sentry_sdk.integrations.boto3._context import AwsCallContext
8+
from sentry_sdk.traces import Span
89

910

1011
class _ServiceExtension:
@@ -16,6 +17,7 @@ class _ServiceExtension:
1617
"""
1718

1819
__slots__ = ()
20+
skip_if_no_active_span = True
1921

2022
def get_span_op(self, ctx: "AwsCallContext") -> "Optional[str]":
2123
"""Return an optional `sentry.op` override, or `None` to keep the default."""
@@ -25,8 +27,11 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
2527
"""Return request attributes to add before the AWS request is made."""
2628
return {}
2729

28-
def get_response_attributes(
29-
self, ctx: "AwsCallContext", response: "Any"
30-
) -> "Attributes":
31-
"""Return response attributes to add after the AWS request is made."""
32-
return {}
30+
def on_success_response(
31+
self,
32+
ctx: "AwsCallContext",
33+
response: "Any",
34+
span: "Span",
35+
) -> None:
36+
"""Hook that fires after a successful response. Responsible for closing the span."""
37+
return None

0 commit comments

Comments
 (0)