|
1 | | -from functools import partial |
| 1 | +from contextlib import contextmanager |
2 | 2 | from typing import TYPE_CHECKING |
3 | 3 |
|
4 | | -from sentry_sdk.integrations import DidNotEnable, _check_minimum_version |
| 4 | +import sentry_sdk |
| 5 | +from sentry_sdk.integrations import DidNotEnable |
| 6 | +from sentry_sdk.integrations.boto3 import Boto3Integration |
| 7 | +from sentry_sdk.integrations.boto3._context import AwsCallContext |
5 | 8 | from sentry_sdk.integrations.boto3._instrumentation import ( |
6 | | - _sentry_after_call, |
7 | | - _sentry_after_call_error, |
| 9 | + _finish_span, |
| 10 | + _instrument_streaming_body, |
8 | 11 | _sentry_before_sign, |
9 | 12 | _sentry_request_created, |
| 13 | + _start_client_span, |
10 | 14 | ) |
11 | | -from sentry_sdk.utils import parse_version |
| 15 | +from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan |
| 16 | +from sentry_sdk.utils import capture_internal_exceptions |
12 | 17 |
|
13 | 18 | if TYPE_CHECKING: |
14 | | - from typing import Any |
| 19 | + from typing import Any, Iterator, Optional, Union |
| 20 | + |
| 21 | + from sentry_sdk.tracing import Span |
15 | 22 |
|
16 | 23 | try: |
17 | | - from botocore import __version__ as BOTOCORE_VERSION |
18 | 24 | from botocore.client import BaseClient |
19 | 25 | except ImportError: |
20 | | - raise DidNotEnable("botocore is not installed") |
| 26 | + raise DidNotEnable("botocore not installed") |
21 | 27 |
|
22 | 28 |
|
23 | | -def _patch_botocore_client() -> None: |
24 | | - from sentry_sdk.integrations.boto3 import Boto3Integration |
| 29 | +@contextmanager |
| 30 | +def _activate_client_span(span: "StreamedSpan") -> "Iterator[StreamedSpan]": |
| 31 | + """Temporarily activate an inactive boto span without ending it.""" |
| 32 | + if isinstance(span, NoOpStreamedSpan): |
| 33 | + yield span |
| 34 | + return |
25 | 35 |
|
26 | | - version = parse_version(BOTOCORE_VERSION) |
27 | | - _check_minimum_version(Boto3Integration, version, "botocore") |
| 36 | + scope = sentry_sdk.get_current_scope() |
| 37 | + previous_span = scope.streamed_span |
| 38 | + scope.streamed_span = span |
| 39 | + try: |
| 40 | + yield span |
| 41 | + finally: |
| 42 | + scope.streamed_span = previous_span |
28 | 43 |
|
| 44 | + |
| 45 | +def _patch_botocore_client() -> None: |
29 | 46 | orig_init = BaseClient.__init__ |
| 47 | + orig_make_api_call = BaseClient._make_api_call # type: ignore |
30 | 48 |
|
31 | 49 | def sentry_patched_init(self: "BaseClient", *args: "Any", **kwargs: "Any") -> None: |
32 | 50 | orig_init(self, *args, **kwargs) |
33 | 51 | meta = self.meta |
34 | | - service_id = meta.service_model.service_id |
35 | | - meta.events.register( |
36 | | - "request-created", |
37 | | - partial(_sentry_request_created, service_id=service_id), |
38 | | - ) |
39 | | - # run after other `before-sign` handlers, allowing it to see and preserve existing baggage. |
| 52 | + meta.events.register("request-created", _sentry_request_created) |
| 53 | + # run after other `before-sign` handlers so existing baggage is preserved. |
40 | 54 | meta.events.register_last("before-sign", _sentry_before_sign) |
41 | | - meta.events.register("after-call", _sentry_after_call) |
42 | | - meta.events.register("after-call-error", _sentry_after_call_error) |
| 55 | + |
| 56 | + def sentry_patched_make_api_call( |
| 57 | + self: "BaseClient", operation_name: str, api_params: "Any" |
| 58 | + ) -> "Any": |
| 59 | + """ |
| 60 | + Track a single API call, including retries, serialization, and endpoint |
| 61 | + resolution. For streaming responses, keep the span open until the |
| 62 | + response body is consumed or closed. |
| 63 | + https://github.com/boto/botocore/blob/develop/botocore/client.py |
| 64 | + https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#rpc-client-span |
| 65 | + """ |
| 66 | + client = sentry_sdk.get_client() |
| 67 | + if client.get_integration(Boto3Integration) is None: |
| 68 | + return orig_make_api_call(self, operation_name, api_params) |
| 69 | + |
| 70 | + ctx = AwsCallContext(operation_name) |
| 71 | + |
| 72 | + # add optional metadata to context. |
| 73 | + with capture_internal_exceptions(): |
| 74 | + ctx.add_metadata(self) |
| 75 | + |
| 76 | + span: "Optional[Union[Span, StreamedSpan]]" = None |
| 77 | + with capture_internal_exceptions(): |
| 78 | + span = _start_client_span(ctx) |
| 79 | + |
| 80 | + if span is None: |
| 81 | + return orig_make_api_call(self, operation_name, api_params) |
| 82 | + |
| 83 | + # activate without finishing; a streaming response may outlive the call. |
| 84 | + span_ctx = ( |
| 85 | + _activate_client_span(span) if isinstance(span, StreamedSpan) else span |
| 86 | + ) |
| 87 | + |
| 88 | + try: |
| 89 | + with span_ctx: |
| 90 | + parsed = orig_make_api_call(self, operation_name, api_params) |
| 91 | + except BaseException as error: |
| 92 | + # finish `StreamedSpan` explicitly; static spans are finished by |
| 93 | + # their context manager. |
| 94 | + if isinstance(span, StreamedSpan): |
| 95 | + _finish_span(span, error) |
| 96 | + raise |
| 97 | + |
| 98 | + streaming_body_instrumented = False |
| 99 | + with capture_internal_exceptions(): |
| 100 | + streaming_body_instrumented = _instrument_streaming_body(span, parsed) |
| 101 | + |
| 102 | + # `StreamingBody`s finish their span when consumed or closed. |
| 103 | + if isinstance(span, StreamedSpan) and not streaming_body_instrumented: |
| 104 | + _finish_span(span) |
| 105 | + return parsed |
43 | 106 |
|
44 | 107 | BaseClient.__init__ = sentry_patched_init # type: ignore |
| 108 | + BaseClient._make_api_call = sentry_patched_make_api_call # type: ignore |
0 commit comments