-
Notifications
You must be signed in to change notification settings - Fork 673
feat(boto3): Add common OTel AWS client attributes #7481
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
Open
pabloDeputter
wants to merge
4
commits into
pablo/trace-boto3-client-call-lifecycle
from
pablo/improve-boto3-call-lifecycle
+1,072
−166
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,108 @@ | ||
| from functools import partial | ||
| from contextlib import contextmanager | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry_sdk.integrations import DidNotEnable, _check_minimum_version | ||
| import sentry_sdk | ||
| from sentry_sdk.integrations import DidNotEnable | ||
| from sentry_sdk.integrations.boto3 import Boto3Integration | ||
| from sentry_sdk.integrations.boto3._context import AwsCallContext | ||
| from sentry_sdk.integrations.boto3._instrumentation import ( | ||
| _sentry_after_call, | ||
| _sentry_after_call_error, | ||
| _finish_span, | ||
| _instrument_streaming_body, | ||
| _sentry_before_sign, | ||
| _sentry_request_created, | ||
| _start_client_span, | ||
| ) | ||
| from sentry_sdk.utils import parse_version | ||
| from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan | ||
| from sentry_sdk.utils import capture_internal_exceptions | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any | ||
| from typing import Any, Iterator, Optional, Union | ||
|
|
||
| from sentry_sdk.tracing import Span | ||
|
|
||
| try: | ||
| from botocore import __version__ as BOTOCORE_VERSION | ||
| from botocore.client import BaseClient | ||
| except ImportError: | ||
| raise DidNotEnable("botocore is not installed") | ||
| raise DidNotEnable("botocore not installed") | ||
|
|
||
|
|
||
| def _patch_botocore_client() -> None: | ||
| from sentry_sdk.integrations.boto3 import Boto3Integration | ||
| @contextmanager | ||
| def _activate_client_span(span: "StreamedSpan") -> "Iterator[StreamedSpan]": | ||
| """Temporarily activate an inactive boto span without ending it.""" | ||
| if isinstance(span, NoOpStreamedSpan): | ||
| yield span | ||
| return | ||
|
|
||
| version = parse_version(BOTOCORE_VERSION) | ||
| _check_minimum_version(Boto3Integration, version, "botocore") | ||
| scope = sentry_sdk.get_current_scope() | ||
| previous_span = scope.streamed_span | ||
| scope.streamed_span = span | ||
| try: | ||
| yield span | ||
| finally: | ||
| scope.streamed_span = previous_span | ||
|
|
||
|
|
||
| def _patch_botocore_client() -> None: | ||
| orig_init = BaseClient.__init__ | ||
| orig_make_api_call = BaseClient._make_api_call # type: ignore | ||
|
|
||
| def sentry_patched_init(self: "BaseClient", *args: "Any", **kwargs: "Any") -> None: | ||
| orig_init(self, *args, **kwargs) | ||
| meta = self.meta | ||
| service_id = meta.service_model.service_id | ||
| meta.events.register( | ||
| "request-created", | ||
| partial(_sentry_request_created, service_id=service_id), | ||
| ) | ||
| # run after other `before-sign` handlers, allowing it to see and preserve existing baggage. | ||
| meta.events.register("request-created", _sentry_request_created) | ||
| # run after other `before-sign` handlers so existing baggage is preserved. | ||
| meta.events.register_last("before-sign", _sentry_before_sign) | ||
| meta.events.register("after-call", _sentry_after_call) | ||
| meta.events.register("after-call-error", _sentry_after_call_error) | ||
|
|
||
| def sentry_patched_make_api_call( | ||
| self: "BaseClient", operation_name: str, api_params: "Any" | ||
| ) -> "Any": | ||
| """ | ||
| Track a single API call, including retries, serialization, and endpoint | ||
| resolution. For streaming responses, keep the span open until the | ||
| response body is consumed or closed. | ||
| https://github.com/boto/botocore/blob/develop/botocore/client.py | ||
| https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#rpc-client-span | ||
| """ | ||
| client = sentry_sdk.get_client() | ||
| if client.get_integration(Boto3Integration) is None: | ||
| return orig_make_api_call(self, operation_name, api_params) | ||
|
|
||
| ctx = AwsCallContext(operation_name, api_params) | ||
|
|
||
| # add optional metadata to context. | ||
| with capture_internal_exceptions(): | ||
| ctx.add_metadata(self) | ||
|
|
||
| span: "Optional[Union[Span, StreamedSpan]]" = None | ||
| with capture_internal_exceptions(): | ||
| span = _start_client_span(ctx) | ||
|
|
||
| if span is None: | ||
| return orig_make_api_call(self, operation_name, api_params) | ||
|
|
||
| # activate without finishing; a streaming response may outlive the call. | ||
| span_ctx = ( | ||
| _activate_client_span(span) if isinstance(span, StreamedSpan) else span | ||
| ) | ||
|
|
||
| try: | ||
| with span_ctx: | ||
| parsed = orig_make_api_call(self, operation_name, api_params) | ||
| except BaseException as error: | ||
| # finish `StreamedSpan` explicitly; static spans are finished by | ||
| # their context manager. | ||
| if isinstance(span, StreamedSpan): | ||
| _finish_span(span, error) | ||
| raise | ||
|
|
||
| streaming_body_instrumented = False | ||
| with capture_internal_exceptions(): | ||
| streaming_body_instrumented = _instrument_streaming_body(span, parsed) | ||
|
|
||
| # `StreamingBody`s finish their span when consumed or closed. | ||
| if isinstance(span, StreamedSpan) and not streaming_body_instrumented: | ||
| _finish_span(span) | ||
| return parsed | ||
|
|
||
| BaseClient.__init__ = sentry_patched_init # type: ignore | ||
| BaseClient._make_api_call = sentry_patched_make_api_call # type: ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry_sdk.integrations import DidNotEnable | ||
| from sentry_sdk.utils import capture_internal_exceptions | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| try: | ||
| from botocore.client import BaseClient | ||
| except ImportError: | ||
| raise DidNotEnable("botocore not installed") | ||
|
|
||
|
|
||
| class AwsCallContext: | ||
| __slots__ = ( | ||
| "service_name", | ||
| "service_id", | ||
| "service_id_hyphenized", | ||
| "operation_name", | ||
| "region_name", | ||
| "endpoint_url", | ||
| "params", | ||
| ) | ||
|
|
||
| def __init__( | ||
| self, | ||
| operation_name: str, | ||
| params: "Any", | ||
| ) -> None: | ||
| self.operation_name: str = operation_name | ||
| self.params: "Dict[str, Any]" = {} | ||
| self.service_name: "Optional[str]" = None | ||
| self.service_id: "Optional[str]" = None | ||
| self.service_id_hyphenized: "Optional[str]" = None | ||
| self.region_name: "Optional[str]" = None | ||
| self.endpoint_url: "Optional[str]" = None | ||
|
|
||
| if isinstance(params, dict): | ||
| with capture_internal_exceptions(): | ||
| self.params = dict(params) | ||
|
|
||
| def add_metadata(self, client: "BaseClient") -> None: | ||
| def _get_attr(obj: "Any", name: str) -> "Any": | ||
| if obj is None: | ||
| return None | ||
|
|
||
| with capture_internal_exceptions(): | ||
| return getattr(obj, name) | ||
|
|
||
| client_meta = _get_attr(client, "meta") | ||
| service_model = _get_attr(client_meta, "service_model") | ||
|
|
||
| # botocore's internal identifier, e.g. `apigateway`. | ||
| self.service_name = _get_attr(service_model, "service_name") | ||
|
|
||
| # modeled AWS service identity used in span names, e.g. `API Gateway`. | ||
| service_id = _get_attr(service_model, "service_id") | ||
| if service_id is not None: | ||
| with capture_internal_exceptions(): | ||
| self.service_id = str(service_id) | ||
| with capture_internal_exceptions(): | ||
| self.service_id_hyphenized = service_id.hyphenize() | ||
|
|
||
| self.region_name = _get_attr(client_meta, "region_name") | ||
| self.endpoint_url = _get_attr(client_meta, "endpoint_url") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.