-
Notifications
You must be signed in to change notification settings - Fork 551
Update integrations with new continue_trace callback usage #3486
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
Changes from all commits
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 |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
from functools import partial | ||
|
||
import sentry_sdk | ||
from sentry_sdk.api import continue_trace | ||
from sentry_sdk.consts import OP | ||
|
||
from sentry_sdk.integrations._asgi_common import ( | ||
|
@@ -34,7 +33,6 @@ | |
transaction_from_function, | ||
_get_installed_modules, | ||
) | ||
from sentry_sdk.tracing import Transaction | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
|
@@ -185,66 +183,47 @@ async def _run_app(self, scope, receive, send, asgi_version): | |
scope, | ||
) | ||
|
||
if ty in ("http", "websocket"): | ||
transaction = continue_trace( | ||
_get_headers(scope), | ||
op="{}.server".format(ty), | ||
with sentry_sdk.continue_trace(_get_headers(scope)): | ||
with sentry_sdk.start_transaction( | ||
op=( | ||
OP.WEBSOCKET_SERVER | ||
if ty == "websocket" | ||
else OP.HTTP_SERVER | ||
), | ||
name=transaction_name, | ||
source=transaction_source, | ||
origin=self.span_origin, | ||
) | ||
logger.debug( | ||
"[ASGI] Created transaction (continuing trace): %s", | ||
transaction, | ||
) | ||
else: | ||
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. We're removing this 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. Ok this is why: 44c43a8 Apparently fetching headers might be problematic for other scope types. So we should probably keep this 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. thanks for digging, made the accessor safe to avoid the if |
||
transaction = Transaction( | ||
op=OP.HTTP_SERVER, | ||
name=transaction_name, | ||
source=transaction_source, | ||
origin=self.span_origin, | ||
) | ||
logger.debug( | ||
"[ASGI] Created transaction (new): %s", transaction | ||
) | ||
|
||
transaction.set_tag("asgi.type", ty) | ||
logger.debug( | ||
"[ASGI] Set transaction name and source on transaction: '%s' / '%s'", | ||
transaction.name, | ||
transaction.source, | ||
) | ||
|
||
with sentry_sdk.start_transaction( | ||
transaction, | ||
custom_sampling_context={"asgi_scope": scope}, | ||
): | ||
logger.debug("[ASGI] Started transaction: %s", transaction) | ||
try: | ||
|
||
async def _sentry_wrapped_send(event): | ||
# type: (Dict[str, Any]) -> Any | ||
is_http_response = ( | ||
event.get("type") == "http.response.start" | ||
and transaction is not None | ||
and "status" in event | ||
) | ||
if is_http_response: | ||
transaction.set_http_status(event["status"]) | ||
|
||
return await send(event) | ||
|
||
if asgi_version == 2: | ||
return await self.app(scope)( | ||
receive, _sentry_wrapped_send | ||
) | ||
else: | ||
return await self.app( | ||
scope, receive, _sentry_wrapped_send | ||
custom_sampling_context={"asgi_scope": scope}, | ||
) as transaction: | ||
logger.debug("[ASGI] Started transaction: %s", transaction) | ||
transaction.set_tag("asgi.type", ty) | ||
try: | ||
|
||
async def _sentry_wrapped_send(event): | ||
# type: (Dict[str, Any]) -> Any | ||
is_http_response = ( | ||
event.get("type") == "http.response.start" | ||
and transaction is not None | ||
and "status" in event | ||
) | ||
if is_http_response: | ||
transaction.set_http_status(event["status"]) | ||
|
||
return await send(event) | ||
|
||
if asgi_version == 2: | ||
return await self.app(scope)( | ||
receive, _sentry_wrapped_send | ||
) | ||
else: | ||
return await self.app( | ||
scope, receive, _sentry_wrapped_send | ||
) | ||
except Exception as exc: | ||
_capture_exception( | ||
exc, mechanism_type=self.mechanism_type | ||
) | ||
except Exception as exc: | ||
_capture_exception(exc, mechanism_type=self.mechanism_type) | ||
raise exc from None | ||
raise exc from None | ||
finally: | ||
_asgi_middleware_applied.set(False) | ||
|
||
|
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.
I wonder whether it might make sense to introduce a new convenience function that would continue the trace and start a span in one call, so that these two
with
context managers could be combined into a singlewith continue_trace_start_span
(perhaps with better naming) statement.