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