-
Notifications
You must be signed in to change notification settings - Fork 602
feat(asgi): Migrate away from event processor in span first #5920
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
Draft
sentrivana
wants to merge
18
commits into
master
Choose a base branch
from
ivana/migrate-asgi-event-processor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
282ba35
feat(asgi): Migrate away from event processor in span first
sentrivana e2484bd
fixes
sentrivana 51af434
Merge branch 'master' into ivana/migrate-asgi-event-processor
sentrivana 5c3174f
.
sentrivana b270e5a
.
sentrivana e93fd1b
.
sentrivana 7cde973
capture_items
sentrivana ef9e640
Merge branch 'master' into ivana/migrate-asgi-event-processor
sentrivana df3f3af
.
sentrivana babb3bd
.
sentrivana 75429dc
no annotatedvalues
sentrivana d9d7674
.
sentrivana 32e4191
.
sentrivana 5cecf97
more tests
sentrivana a39337b
Merge branch 'master' into ivana/migrate-asgi-event-processor
sentrivana 7fb4863
.
sentrivana 7490fe3
.
sentrivana c11e8f1
.
sentrivana 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
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
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| from sentry_sdk.consts import OP | ||
| from sentry_sdk.integrations._asgi_common import ( | ||
| _get_headers, | ||
| _get_request_attributes, | ||
| _get_request_data, | ||
| _get_url, | ||
| ) | ||
|
|
@@ -23,7 +24,11 @@ | |
| nullcontext, | ||
| ) | ||
| from sentry_sdk.sessions import track_session | ||
| from sentry_sdk.traces import StreamedSpan | ||
| from sentry_sdk.traces import ( | ||
| StreamedSpan, | ||
| SegmentSource, | ||
| SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE, | ||
| ) | ||
| from sentry_sdk.tracing import ( | ||
| SOURCE_FOR_STYLE, | ||
| Transaction, | ||
|
|
@@ -40,6 +45,7 @@ | |
| _get_installed_modules, | ||
| reraise, | ||
| capture_internal_exceptions, | ||
| qualname_from_function, | ||
| ) | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
@@ -235,7 +241,7 @@ async def _run_app( | |
| transaction_source, "value", transaction_source | ||
| ), | ||
| "sentry.origin": self.span_origin, | ||
| "asgi.type": ty, | ||
| "network.protocol.name": ty, | ||
sentry-warden[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if ty in ("http", "websocket"): | ||
|
|
@@ -301,6 +307,9 @@ async def _run_app( | |
| else nullcontext() | ||
| ) | ||
|
|
||
| for attribute, value in _get_request_attributes(scope).items(): | ||
| sentry_scope.set_attribute(attribute, value) | ||
|
|
||
| with span_ctx as span: | ||
| try: | ||
|
|
||
|
|
@@ -336,6 +345,7 @@ async def _sentry_wrapped_send( | |
| return await self.app( | ||
| scope, receive, _sentry_wrapped_send | ||
| ) | ||
|
|
||
| except Exception as exc: | ||
| suppress_chained_exceptions = ( | ||
| sentry_sdk.get_client() | ||
|
|
@@ -350,6 +360,26 @@ async def _sentry_wrapped_send( | |
| with capture_internal_exceptions(): | ||
| self._capture_request_exception(exc) | ||
| reraise(*exc_info) | ||
|
|
||
| finally: | ||
| if isinstance(span, StreamedSpan): | ||
| already_set = ( | ||
| span is not None | ||
| and span.name != _DEFAULT_TRANSACTION_NAME | ||
| and span.get_attributes().get("sentry.span.source") | ||
| in [ | ||
| SegmentSource.COMPONENT.value, | ||
| SegmentSource.ROUTE.value, | ||
| SegmentSource.CUSTOM.value, | ||
| ] | ||
| ) | ||
| with capture_internal_exceptions(): | ||
| if not already_set: | ||
| name, source = self._get_segment_name_and_source( | ||
| self.transaction_style, scope | ||
| ) | ||
| span.name = name | ||
| span.set_attribute("sentry.span.source", source) | ||
| finally: | ||
| _asgi_middleware_applied.set(False) | ||
|
|
||
|
|
@@ -424,3 +454,40 @@ def _get_transaction_name_and_source( | |
| return name, source | ||
|
|
||
| return name, source | ||
|
|
||
| def _get_segment_name_and_source( | ||
|
Contributor
Author
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. This is a copy of |
||
| self: "SentryAsgiMiddleware", segment_style: str, asgi_scope: "Any" | ||
| ) -> "Tuple[str, str]": | ||
| name = None | ||
| source = SEGMENT_SOURCE_FOR_STYLE[segment_style].value | ||
| ty = asgi_scope.get("type") | ||
|
|
||
| if segment_style == "endpoint": | ||
| endpoint = asgi_scope.get("endpoint") | ||
| # Webframeworks like Starlette mutate the ASGI env once routing is | ||
| # done, which is sometime after the request has started. If we have | ||
| # an endpoint, overwrite our generic transaction name. | ||
| if endpoint: | ||
| name = qualname_from_function(endpoint) or "" | ||
| else: | ||
| name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None) | ||
| source = SegmentSource.URL.value | ||
|
|
||
| elif segment_style == "url": | ||
| # FastAPI includes the route object in the scope to let Sentry extract the | ||
| # path from it for the transaction name | ||
| route = asgi_scope.get("route") | ||
| if route: | ||
| path = getattr(route, "path", None) | ||
| if path is not None: | ||
| name = path | ||
| else: | ||
| name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None) | ||
| source = SegmentSource.URL.value | ||
|
|
||
| if name is None: | ||
| name = _DEFAULT_TRANSACTION_NAME | ||
| source = SegmentSource.ROUTE.value | ||
| return name, source | ||
|
|
||
| return name, source | ||
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
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.
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.
This is an attributes based copy of
_get_request_datajust above