-
Notifications
You must be signed in to change notification settings - Fork 610
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
base: master
Are you sure you want to change the base?
Changes from all commits
c24ed84
3d0f22f
9424125
4cadf25
839ad7c
7eea15d
f5bdf48
13739ca
11b122b
3386cc3
a5ef80a
90f8e97
c3a55c6
7e39d58
10c67de
88928ad
8f613f9
8121605
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| 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, | ||
|
|
@@ -18,6 +20,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 +49,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), | ||
|
|
@@ -57,25 +62,48 @@ def sentry_patched_init( | |
|
|
||
| @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() | ||
| span_streaming = has_span_streaming_enabled(client.options) | ||
| span: "Union[Span, StreamedSpan]" | ||
| if span_streaming: | ||
| span = sentry_sdk.traces.start_span( | ||
| name=description, | ||
| attributes={ | ||
| "sentry.op": OP.HTTP_CLIENT, | ||
| "sentry.origin": Boto3Integration.origin, | ||
| }, | ||
| ) | ||
| 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) | ||
|
|
||
| span.set_attribute(SPANDATA.RPC_METHOD, f"{service_id}/{operation_name}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is set outside of any conditionals within this block, you could move this up into the |
||
| if request.method is not None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that the legacy approach doesn't have this guard, and I assume it's because This makes me wonder - are we currently sending |
||
| 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. | ||
|
|
@@ -89,7 +117,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.
|
||
|
|
||
| # Span could be absent if the integration is disabled. | ||
| if span is None: | ||
|
|
@@ -100,29 +128,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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is currently written, If you only care about |
||
| streaming_span = sentry_sdk.traces.start_span( | ||
| name=span.name, | ||
|
alexander-alderman-webb marked this conversation as resolved.
|
||
| parent_span=span, | ||
| attributes={ | ||
| "sentry.op": OP.HTTP_CLIENT_STREAM, | ||
| "sentry.origin": Boto3Integration.origin, | ||
| }, | ||
| ) | ||
| else: | ||
| streaming_span = span.start_child( | ||
|
alexander-alderman-webb marked this conversation as resolved.
|
||
| op=OP.HTTP_CLIENT_STREAM, | ||
| name=span.description, | ||
| origin=Boto3Integration.origin, | ||
| ) | ||
|
alexander-alderman-webb marked this conversation as resolved.
|
||
|
|
||
| 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 +181,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: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For variable names capturing states (like this one), naming it like a noun/a thing makes it harder to read in isolation in other areas of the code.
Something that leverages a verb/predicate, along the lines of
is_span_streaming_enabled, is generally better for values/variables like this.