Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 660
feat(boto3): Support span streaming#6193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c24ed843d0f22f94241254cadf25839ad7c7eea15df5bdf4813739ca11b122b3386cc3a5ef80a90f8e97c3a55c67e39d5810c67de88928ad8f613f98121605c777a8317b42e6e0796940c635e77458755File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,9 +4,10 @@ | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable | ||
| from sentry_sdk.tracing import Span | ||
| from sentry_sdk.traces import StreamedSpan | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
| from sentry_sdk.utils import ( | ||
| capture_internal_exceptions, | ||
| ensure_integration_enabled, | ||
| parse_url, | ||
| parse_version, | ||
| ) | ||
| @@ -18,6 +19,9 @@ | ||
| from typing import Dict | ||
| from typing import Optional | ||
| from typing import Type | ||
| from typing import Union | ||
| from botocore.model import ServiceId | ||
| try: | ||
| from botocore import __version__ as BOTOCORE_VERSION | ||
| @@ -44,7 +48,7 @@ def sentry_patched_init( | ||
| ) -> None: | ||
| orig_init(self, *args, **kwargs) | ||
| meta = self.meta | ||
| service_id = meta.service_model.service_id.hyphenize() | ||
| service_id = meta.service_model.service_id | ||
| meta.events.register( | ||
| "request-created", | ||
| partial(_sentry_request_created, service_id=service_id), | ||
| @@ -55,27 +59,52 @@ def sentry_patched_init( | ||
| BaseClient.__init__ = sentry_patched_init # type: ignore | ||
| @ensure_integration_enabled(Boto3Integration) | ||
| def _sentry_request_created( | ||
| service_id: str, request: "AWSRequest", operation_name: str, **kwargs: "Any" | ||
| service_id: "ServiceId", request: "AWSRequest", operation_name: str, **kwargs: "Any" | ||
| ) -> None: | ||
| description = "aws.%s.%s" % (service_id, operation_name) | ||
| span = sentry_sdk.start_span( | ||
| op=OP.HTTP_CLIENT, | ||
| name=description, | ||
| origin=Boto3Integration.origin, | ||
| ) | ||
| if request.url is not None: | ||
| with capture_internal_exceptions(): | ||
| parsed_url = parse_url(request.url, sanitize=False) | ||
| span.set_data("aws.request.url", parsed_url.url) | ||
| span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) | ||
| span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) | ||
| span.set_tag("aws.service_id", service_id) | ||
| span.set_tag("aws.operation_name", operation_name) | ||
| span.set_data(SPANDATA.HTTP_METHOD, request.method) | ||
| description = "aws.%s.%s" % (service_id.hyphenize(), operation_name) | ||
| client = sentry_sdk.get_client() | ||
| if client.get_integration(Boto3Integration) is None: | ||
| return | ||
| is_span_streaming_enabled = has_span_streaming_enabled(client.options) | ||
| span: "Union[Span, StreamedSpan]" | ||
| if is_span_streaming_enabled: | ||
| span = sentry_sdk.traces.start_span( | ||
| name=description, | ||
| attributes={ | ||
| "sentry.op": OP.HTTP_CLIENT, | ||
| "sentry.origin": Boto3Integration.origin, | ||
| SPANDATA.RPC_METHOD: f"{service_id}/{operation_name}", | ||
| }, | ||
| ) | ||
| if request.url is not None: | ||
| with capture_internal_exceptions(): | ||
| parsed_url = parse_url(request.url, sanitize=False) | ||
| span.set_attribute(SPANDATA.URL_FULL, parsed_url.url) | ||
| span.set_attribute(SPANDATA.URL_QUERY, parsed_url.query) | ||
| span.set_attribute(SPANDATA.URL_FRAGMENT, parsed_url.fragment) | ||
| if request.method is not None: | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| span.set_attribute(SPANDATA.HTTP_REQUEST_METHOD, request.method) | ||
| else: | ||
| span = sentry_sdk.start_span( | ||
| op=OP.HTTP_CLIENT, | ||
| name=description, | ||
| origin=Boto3Integration.origin, | ||
| ) | ||
| if request.url is not None: | ||
| with capture_internal_exceptions(): | ||
| parsed_url = parse_url(request.url, sanitize=False) | ||
| span.set_data("aws.request.url", parsed_url.url) | ||
| span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) | ||
| span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) | ||
| span.set_tag("aws.service_id", service_id.hyphenize()) | ||
| span.set_tag("aws.operation_name", operation_name) | ||
| span.set_data(SPANDATA.HTTP_METHOD, request.method) | ||
| # We do it in order for subsequent http calls/retries be | ||
| # attached to this span. | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @@ -89,7 +118,7 @@ def _sentry_request_created( | ||
| def _sentry_after_call( | ||
| context: "Dict[str, Any]", parsed: "Dict[str, Any]", **kwargs: "Any" | ||
| ) -> None: | ||
| span: "Optional[Span]" = context.pop("_sentrysdk_span", None) | ||
| span: "Optional[Union[Span, StreamedSpan]]" = context.pop("_sentrysdk_span", None) | ||
sentry-warden[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # Span could be absent if the integration is disabled. | ||
| if span is None: | ||
| @@ -100,29 +129,51 @@ def _sentry_after_call( | ||
| if not isinstance(body, StreamingBody): | ||
| return | ||
| streaming_span = span.start_child( | ||
| op=OP.HTTP_CLIENT_STREAM, | ||
| name=span.description, | ||
| origin=Boto3Integration.origin, | ||
| ) | ||
| streaming_span: "Union[Span, StreamedSpan]" | ||
| if isinstance(span, StreamedSpan): | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| streaming_span = sentry_sdk.traces.start_span( | ||
| name=span.name, | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| parent_span=span, | ||
| attributes={ | ||
| "sentry.op": OP.HTTP_CLIENT_STREAM, | ||
| "sentry.origin": Boto3Integration.origin, | ||
| }, | ||
| ) | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. sentry[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| else: | ||
| streaming_span = span.start_child( | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| op=OP.HTTP_CLIENT_STREAM, | ||
| name=span.description, | ||
| origin=Boto3Integration.origin, | ||
| ) | ||
alexander-alderman-webb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| orig_read = body.read | ||
| orig_close = body.close | ||
| def sentry_streaming_body_read(*args: "Any", **kwargs: "Any") -> bytes: | ||
| try: | ||
| ret = orig_read(*args, **kwargs) | ||
| if not ret: | ||
| if ret: | ||
| return ret | ||
| if isinstance(streaming_span, StreamedSpan): | ||
| streaming_span.end() | ||
| else: | ||
| streaming_span.finish() | ||
| return ret | ||
| except Exception: | ||
| streaming_span.finish() | ||
| if isinstance(streaming_span, StreamedSpan): | ||
| streaming_span.end() | ||
| else: | ||
| streaming_span.finish() | ||
| raise | ||
| body.read = sentry_streaming_body_read # type: ignore | ||
| def sentry_streaming_body_close(*args: "Any", **kwargs: "Any") -> None: | ||
| streaming_span.finish() | ||
| if isinstance(streaming_span, StreamedSpan): | ||
| streaming_span.end() | ||
| else: | ||
| streaming_span.finish() | ||
| orig_close(*args, **kwargs) | ||
| body.close = sentry_streaming_body_close # type: ignore | ||
| @@ -131,7 +182,7 @@ def sentry_streaming_body_close(*args: "Any", **kwargs: "Any") -> None: | ||
| def _sentry_after_call_error( | ||
| context: "Dict[str, Any]", exception: "Type[BaseException]", **kwargs: "Any" | ||
| ) -> None: | ||
| span: "Optional[Span]" = context.pop("_sentrysdk_span", None) | ||
| span: "Optional[Union[Span, StreamedSpan]]" = context.pop("_sentrysdk_span", None) | ||
| # Span could be absent if the integration is disabled. | ||
| if span is None: | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.