-
Notifications
You must be signed in to change notification settings - Fork 603
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
282ba35
e2484bd
51af434
5c3174f
b270e5a
e93fd1b
7cde973
ef9e640
df3f3af
babb3bd
75429dc
d9d7674
32e4191
5cecf97
a39337b
7fb4863
7490fe3
c11e8f1
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| from sentry_sdk.consts import OP | ||
| from sentry_sdk.integrations._asgi_common import ( | ||
| _get_headers, | ||
| _get_request_attributes, | ||
|
Check failure on line 18 in sentry_sdk/integrations/asgi.py
|
||
| _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 @@ | |
| 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 @@ | |
| else nullcontext() | ||
| ) | ||
|
|
||
| for attribute, value in _get_request_attributes(scope): | ||
| sentry_scope.set_attribute(attribute, value) | ||
|
Check failure on line 311 in sentry_sdk/integrations/asgi.py
|
||
sentrivana marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| with span_ctx as span: | ||
| try: | ||
|
|
||
|
|
@@ -329,13 +338,24 @@ | |
| return await send(event) | ||
|
|
||
| if asgi_version == 2: | ||
| return await self.app(scope)( | ||
| result = await self.app(scope)( | ||
| receive, _sentry_wrapped_send | ||
| ) | ||
| else: | ||
| return await self.app( | ||
| result = await self.app( | ||
| scope, receive, _sentry_wrapped_send | ||
| ) | ||
|
|
||
| with capture_internal_exceptions(): | ||
| name, source = self._get_segment_name_and_source( | ||
| self.transaction_style, scope | ||
| ) | ||
| if isinstance(span, StreamedSpan): | ||
| span.name = name | ||
| span.set_attribute("sentry.span.source", source) | ||
|
|
||
| return result | ||
sentry-warden[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| except Exception as exc: | ||
| suppress_chained_exceptions = ( | ||
| sentry_sdk.get_client() | ||
|
|
@@ -424,3 +444,40 @@ | |
| 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] | ||
|
Check warning on line 452 in sentry_sdk/integrations/asgi.py
|
||
sentrivana marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
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