|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from sentry_sdk import Hub |
| 4 | +from sentry_sdk.integrations import Integration, DidNotEnable |
| 5 | +from sentry_sdk.tracing import Span |
| 6 | + |
| 7 | +from sentry_sdk._functools import partial |
| 8 | +from sentry_sdk._types import MYPY |
| 9 | + |
| 10 | +if MYPY: |
| 11 | + from typing import Any |
| 12 | + from typing import Dict |
| 13 | + from typing import Optional |
| 14 | + from typing import Type |
| 15 | + |
| 16 | +try: |
| 17 | + from botocore.client import BaseClient # type: ignore |
| 18 | + from botocore.response import StreamingBody # type: ignore |
| 19 | + from botocore.awsrequest import AWSRequest # type: ignore |
| 20 | +except ImportError: |
| 21 | + raise DidNotEnable("botocore is not installed") |
| 22 | + |
| 23 | + |
| 24 | +class Boto3Integration(Integration): |
| 25 | + identifier = "boto3" |
| 26 | + |
| 27 | + @staticmethod |
| 28 | + def setup_once(): |
| 29 | + # type: () -> None |
| 30 | + orig_init = BaseClient.__init__ |
| 31 | + |
| 32 | + def sentry_patched_init(self, *args, **kwargs): |
| 33 | + # type: (Type[BaseClient], *Any, **Any) -> None |
| 34 | + orig_init(self, *args, **kwargs) |
| 35 | + meta = self.meta |
| 36 | + service_id = meta.service_model.service_id.hyphenize() |
| 37 | + meta.events.register( |
| 38 | + "request-created", |
| 39 | + partial(_sentry_request_created, service_id=service_id), |
| 40 | + ) |
| 41 | + meta.events.register("after-call", _sentry_after_call) |
| 42 | + meta.events.register("after-call-error", _sentry_after_call_error) |
| 43 | + |
| 44 | + BaseClient.__init__ = sentry_patched_init |
| 45 | + |
| 46 | + |
| 47 | +def _sentry_request_created(service_id, request, operation_name, **kwargs): |
| 48 | + # type: (str, AWSRequest, str, **Any) -> None |
| 49 | + hub = Hub.current |
| 50 | + if hub.get_integration(Boto3Integration) is None: |
| 51 | + return |
| 52 | + |
| 53 | + description = "aws.%s.%s" % (service_id, operation_name) |
| 54 | + span = hub.start_span( |
| 55 | + hub=hub, |
| 56 | + op="aws.request", |
| 57 | + description=description, |
| 58 | + ) |
| 59 | + span.set_tag("aws.service_id", service_id) |
| 60 | + span.set_tag("aws.operation_name", operation_name) |
| 61 | + span.set_data("aws.request.url", request.url) |
| 62 | + |
| 63 | + # We do it in order for subsequent http calls/retries be |
| 64 | + # attached to this span. |
| 65 | + span.__enter__() |
| 66 | + |
| 67 | + # request.context is an open-ended data-structure |
| 68 | + # where we can add anything useful in request life cycle. |
| 69 | + request.context["_sentrysdk_span"] = span |
| 70 | + |
| 71 | + |
| 72 | +def _sentry_after_call(context, parsed, **kwargs): |
| 73 | + # type: (Dict[str, Any], Dict[str, Any], **Any) -> None |
| 74 | + span = context.pop("_sentrysdk_span", None) # type: Optional[Span] |
| 75 | + |
| 76 | + # Span could be absent if the integration is disabled. |
| 77 | + if span is None: |
| 78 | + return |
| 79 | + span.__exit__(None, None, None) |
| 80 | + |
| 81 | + body = parsed.get("Body") |
| 82 | + if not isinstance(body, StreamingBody): |
| 83 | + return |
| 84 | + |
| 85 | + streaming_span = span.start_child( |
| 86 | + op="aws.request.stream", |
| 87 | + description=span.description, |
| 88 | + ) |
| 89 | + |
| 90 | + orig_read = body.read |
| 91 | + orig_close = body.close |
| 92 | + |
| 93 | + def sentry_streaming_body_read(*args, **kwargs): |
| 94 | + # type: (*Any, **Any) -> bytes |
| 95 | + try: |
| 96 | + ret = orig_read(*args, **kwargs) |
| 97 | + if not ret: |
| 98 | + streaming_span.finish() |
| 99 | + return ret |
| 100 | + except Exception: |
| 101 | + streaming_span.finish() |
| 102 | + raise |
| 103 | + |
| 104 | + body.read = sentry_streaming_body_read |
| 105 | + |
| 106 | + def sentry_streaming_body_close(*args, **kwargs): |
| 107 | + # type: (*Any, **Any) -> None |
| 108 | + streaming_span.finish() |
| 109 | + orig_close(*args, **kwargs) |
| 110 | + |
| 111 | + body.close = sentry_streaming_body_close |
| 112 | + |
| 113 | + |
| 114 | +def _sentry_after_call_error(context, exception, **kwargs): |
| 115 | + # type: (Dict[str, Any], Type[BaseException], **Any) -> None |
| 116 | + span = context.pop("_sentrysdk_span", None) # type: Optional[Span] |
| 117 | + |
| 118 | + # Span could be absent if the integration is disabled. |
| 119 | + if span is None: |
| 120 | + return |
| 121 | + span.__exit__(type(exception), exception, None) |
0 commit comments