-
Notifications
You must be signed in to change notification settings - Fork 505
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
Configure HTTP methods to capture in ASGI middleware and frameworks #3533
Changes from all commits
1518f5d
583c09b
7cc7bb5
b86e57a
b45a349
2136871
c39f042
8e63cc7
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
""" | ||
|
||
import asyncio | ||
from contextlib import contextmanager | ||
import inspect | ||
from copy import deepcopy | ||
from functools import partial | ||
|
@@ -14,6 +15,7 @@ | |
from sentry_sdk.consts import OP | ||
|
||
from sentry_sdk.integrations._asgi_common import ( | ||
DEFAULT_HTTP_METHODS_TO_CAPTURE, | ||
_get_headers, | ||
_get_request_data, | ||
_get_url, | ||
|
@@ -42,6 +44,7 @@ | |
from typing import Any | ||
from typing import Callable | ||
from typing import Dict | ||
from typing import Iterator | ||
from typing import Optional | ||
from typing import Tuple | ||
|
||
|
@@ -55,6 +58,13 @@ | |
TRANSACTION_STYLE_VALUES = ("endpoint", "url") | ||
|
||
|
||
# This noop context manager can be replaced with "from contextlib import nullcontext" when we drop Python 3.6 support | ||
@contextmanager | ||
def nullcontext(): | ||
# type: () -> Iterator[None] | ||
yield | ||
Comment on lines
+61
to
+65
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. Maybe move this to |
||
|
||
|
||
def _capture_exception(exc, mechanism_type="asgi"): | ||
# type: (Any, str) -> None | ||
|
||
|
@@ -89,17 +99,19 @@ class SentryAsgiMiddleware: | |
"transaction_style", | ||
"mechanism_type", | ||
"span_origin", | ||
"http_methods_to_capture", | ||
) | ||
|
||
def __init__( | ||
self, | ||
app, | ||
unsafe_context_data=False, | ||
transaction_style="endpoint", | ||
mechanism_type="asgi", | ||
span_origin="manual", | ||
app, # type: Any | ||
unsafe_context_data=False, # type: bool | ||
transaction_style="endpoint", # type: str | ||
mechanism_type="asgi", # type: str | ||
span_origin="manual", # type: str | ||
http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: Tuple[str, ...] | ||
): | ||
# type: (Any, bool, str, str, str) -> None | ||
# type: (...) -> None | ||
""" | ||
Instrument an ASGI application with Sentry. Provides HTTP/websocket | ||
data to sent events and basic handling for exceptions bubbling up | ||
|
@@ -134,6 +146,7 @@ def __init__( | |
self.mechanism_type = mechanism_type | ||
self.span_origin = span_origin | ||
self.app = app | ||
self.http_methods_to_capture = http_methods_to_capture | ||
|
||
if _looks_like_asgi3(app): | ||
self.__call__ = self._run_asgi3 # type: Callable[..., Any] | ||
|
@@ -185,52 +198,59 @@ 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), | ||
name=transaction_name, | ||
source=transaction_source, | ||
origin=self.span_origin, | ||
) | ||
logger.debug( | ||
"[ASGI] Created transaction (continuing trace): %s", | ||
transaction, | ||
) | ||
else: | ||
transaction = Transaction( | ||
op=OP.HTTP_SERVER, | ||
name=transaction_name, | ||
source=transaction_source, | ||
origin=self.span_origin, | ||
) | ||
method = scope.get("method", "").upper() | ||
transaction = None | ||
if method in self.http_methods_to_capture: | ||
if ty in ("http", "websocket"): | ||
transaction = continue_trace( | ||
_get_headers(scope), | ||
op="{}.server".format(ty), | ||
name=transaction_name, | ||
source=transaction_source, | ||
origin=self.span_origin, | ||
) | ||
logger.debug( | ||
"[ASGI] Created transaction (continuing trace): %s", | ||
transaction, | ||
) | ||
else: | ||
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] Created transaction (new): %s", transaction | ||
"[ASGI] Set transaction name and source on transaction: '%s' / '%s'", | ||
transaction.name, | ||
transaction.source, | ||
) | ||
|
||
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}, | ||
with ( | ||
sentry_sdk.start_transaction( | ||
transaction, | ||
custom_sampling_context={"asgi_scope": scope}, | ||
) | ||
if transaction is not None | ||
else nullcontext() | ||
): | ||
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"]) | ||
if transaction is not None: | ||
is_http_response = ( | ||
event.get("type") == "http.response.start" | ||
and "status" in event | ||
) | ||
if is_http_response: | ||
transaction.set_http_status(event["status"]) | ||
Comment on lines
+247
to
+253
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 did it this way to make mypy happy 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. mypy also made me do things I'm not proud of. We need mypy anonymous |
||
|
||
return await send(event) | ||
|
||
|
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.
Should we maybe just import this from
_wsgi_common
here? Or directly use the_wsgi_common
constant in the integrations?