From 1c64ff787e39268454c3a5ff766ab6d899a1f3d5 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 1 Oct 2024 14:35:23 +0200 Subject: [PATCH] Configure HTTP methods to capture in WSGI middleware and frameworks (#3531) - Do not capture transactions for OPTIONS and HEAD HTTP methods by default. - Make it possible with an `http_methods_to_capture` config option for Django, Flask, Starlette, and FastAPI to specify what HTTP methods to capture. --- sentry_sdk/integrations/_wsgi_common.py | 21 ++++ sentry_sdk/integrations/asgi.py | 100 ++++++++++-------- sentry_sdk/integrations/django/__init__.py | 27 +++-- sentry_sdk/integrations/flask.py | 21 +++- sentry_sdk/integrations/starlette.py | 8 ++ sentry_sdk/integrations/wsgi.py | 55 +++++++--- tests/integrations/django/myapp/urls.py | 1 + tests/integrations/django/myapp/views.py | 5 + tests/integrations/django/test_basic.py | 63 ++++++++++- tests/integrations/fastapi/test_fastapi.py | 96 ++++++++++++++++- tests/integrations/flask/test_flask.py | 72 +++++++++++++ .../integrations/starlette/test_starlette.py | 80 ++++++++++++++ 12 files changed, 477 insertions(+), 72 deletions(-) diff --git a/sentry_sdk/integrations/_wsgi_common.py b/sentry_sdk/integrations/_wsgi_common.py index 5052b6fa5c..7266a91f56 100644 --- a/sentry_sdk/integrations/_wsgi_common.py +++ b/sentry_sdk/integrations/_wsgi_common.py @@ -1,3 +1,4 @@ +from contextlib import contextmanager import json from copy import deepcopy @@ -15,6 +16,7 @@ if TYPE_CHECKING: from typing import Any from typing import Dict + from typing import Iterator from typing import Mapping from typing import MutableMapping from typing import Optional @@ -37,6 +39,25 @@ x[len("HTTP_") :] for x in SENSITIVE_ENV_KEYS if x.startswith("HTTP_") ) +DEFAULT_HTTP_METHODS_TO_CAPTURE = ( + "CONNECT", + "DELETE", + "GET", + # "HEAD", # do not capture HEAD requests by default + # "OPTIONS", # do not capture OPTIONS requests by default + "PATCH", + "POST", + "PUT", + "TRACE", +) + + +# 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 + def request_body_within_bounds(client, content_length): # type: (Optional[sentry_sdk.client.BaseClient], int) -> bool diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 33fe18bd82..1b256c8eee 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -18,6 +18,10 @@ _get_request_data, _get_url, ) +from sentry_sdk.integrations._wsgi_common import ( + DEFAULT_HTTP_METHODS_TO_CAPTURE, + nullcontext, +) from sentry_sdk.sessions import track_session from sentry_sdk.tracing import ( SOURCE_FOR_STYLE, @@ -89,17 +93,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 +140,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 +192,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"]) return await send(event) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 7d33aad29c..c9f20dd49b 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -25,7 +25,10 @@ from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations.logging import ignore_logger from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware -from sentry_sdk.integrations._wsgi_common import RequestExtractor +from sentry_sdk.integrations._wsgi_common import ( + DEFAULT_HTTP_METHODS_TO_CAPTURE, + RequestExtractor, +) try: from django import VERSION as DJANGO_VERSION @@ -125,13 +128,14 @@ class DjangoIntegration(Integration): def __init__( self, - transaction_style="url", - middleware_spans=True, - signals_spans=True, - cache_spans=False, - signals_denylist=None, + transaction_style="url", # type: str + middleware_spans=True, # type: bool + signals_spans=True, # type: bool + cache_spans=False, # type: bool + signals_denylist=None, # type: Optional[list[signals.Signal]] + http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: tuple[str, ...] ): - # type: (str, bool, bool, bool, Optional[list[signals.Signal]]) -> None + # type: (...) -> None if transaction_style not in TRANSACTION_STYLE_VALUES: raise ValueError( "Invalid value for transaction_style: %s (must be in %s)" @@ -145,6 +149,8 @@ def __init__( self.cache_spans = cache_spans + self.http_methods_to_capture = tuple(map(str.upper, http_methods_to_capture)) + @staticmethod def setup_once(): # type: () -> None @@ -172,10 +178,17 @@ def sentry_patched_wsgi_handler(self, environ, start_response): use_x_forwarded_for = settings.USE_X_FORWARDED_HOST + integration = sentry_sdk.get_client().get_integration(DjangoIntegration) + middleware = SentryWsgiMiddleware( bound_old_app, use_x_forwarded_for, span_origin=DjangoIntegration.origin, + http_methods_to_capture=( + integration.http_methods_to_capture + if integration + else DEFAULT_HTTP_METHODS_TO_CAPTURE + ), ) return middleware(environ, start_response) diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index b504376264..128301ddb4 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -1,6 +1,9 @@ import sentry_sdk from sentry_sdk.integrations import DidNotEnable, Integration -from sentry_sdk.integrations._wsgi_common import RequestExtractor +from sentry_sdk.integrations._wsgi_common import ( + DEFAULT_HTTP_METHODS_TO_CAPTURE, + RequestExtractor, +) from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware from sentry_sdk.scope import should_send_default_pii from sentry_sdk.tracing import SOURCE_FOR_STYLE @@ -52,14 +55,19 @@ class FlaskIntegration(Integration): transaction_style = "" - def __init__(self, transaction_style="endpoint"): - # type: (str) -> None + def __init__( + self, + transaction_style="endpoint", # type: str + http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: tuple[str, ...] + ): + # type: (...) -> None if transaction_style not in TRANSACTION_STYLE_VALUES: raise ValueError( "Invalid value for transaction_style: %s (must be in %s)" % (transaction_style, TRANSACTION_STYLE_VALUES) ) self.transaction_style = transaction_style + self.http_methods_to_capture = tuple(map(str.upper, http_methods_to_capture)) @staticmethod def setup_once(): @@ -83,9 +91,16 @@ def sentry_patched_wsgi_app(self, environ, start_response): if sentry_sdk.get_client().get_integration(FlaskIntegration) is None: return old_app(self, environ, start_response) + integration = sentry_sdk.get_client().get_integration(FlaskIntegration) + middleware = SentryWsgiMiddleware( lambda *a, **kw: old_app(self, *a, **kw), span_origin=FlaskIntegration.origin, + http_methods_to_capture=( + integration.http_methods_to_capture + if integration + else DEFAULT_HTTP_METHODS_TO_CAPTURE + ), ) return middleware(environ, start_response) diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 61c5f3e4ff..03584fdad7 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -12,6 +12,7 @@ _DEFAULT_FAILED_REQUEST_STATUS_CODES, ) from sentry_sdk.integrations._wsgi_common import ( + DEFAULT_HTTP_METHODS_TO_CAPTURE, HttpCodeRangeContainer, _is_json_content_type, request_body_within_bounds, @@ -85,6 +86,7 @@ def __init__( transaction_style="url", # type: str failed_request_status_codes=_DEFAULT_FAILED_REQUEST_STATUS_CODES, # type: Union[Set[int], list[HttpStatusCodeRange], None] middleware_spans=True, # type: bool + http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: tuple[str, ...] ): # type: (...) -> None if transaction_style not in TRANSACTION_STYLE_VALUES: @@ -94,6 +96,7 @@ def __init__( ) self.transaction_style = transaction_style self.middleware_spans = middleware_spans + self.http_methods_to_capture = tuple(map(str.upper, http_methods_to_capture)) if isinstance(failed_request_status_codes, Set): self.failed_request_status_codes = ( @@ -390,6 +393,11 @@ async def _sentry_patched_asgi_app(self, scope, receive, send): mechanism_type=StarletteIntegration.identifier, transaction_style=integration.transaction_style, span_origin=StarletteIntegration.origin, + http_methods_to_capture=( + integration.http_methods_to_capture + if integration + else DEFAULT_HTTP_METHODS_TO_CAPTURE + ), ) middleware.__call__ = middleware._run_asgi3 diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 00aad30854..50deae10c5 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -6,7 +6,11 @@ from sentry_sdk.api import continue_trace from sentry_sdk.consts import OP from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.integrations._wsgi_common import _filter_headers +from sentry_sdk.integrations._wsgi_common import ( + DEFAULT_HTTP_METHODS_TO_CAPTURE, + _filter_headers, + nullcontext, +) from sentry_sdk.sessions import track_session from sentry_sdk.scope import use_isolation_scope from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE @@ -66,13 +70,25 @@ def get_request_url(environ, use_x_forwarded_for=False): class SentryWsgiMiddleware: - __slots__ = ("app", "use_x_forwarded_for", "span_origin") + __slots__ = ( + "app", + "use_x_forwarded_for", + "span_origin", + "http_methods_to_capture", + ) - def __init__(self, app, use_x_forwarded_for=False, span_origin="manual"): - # type: (Callable[[Dict[str, str], Callable[..., Any]], Any], bool, str) -> None + def __init__( + self, + app, # type: Callable[[Dict[str, str], Callable[..., Any]], Any] + use_x_forwarded_for=False, # type: bool + span_origin="manual", # type: str + http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: Tuple[str, ...] + ): + # type: (...) -> None self.app = app self.use_x_forwarded_for = use_x_forwarded_for self.span_origin = span_origin + self.http_methods_to_capture = http_methods_to_capture def __call__(self, environ, start_response): # type: (Dict[str, str], Callable[..., Any]) -> _ScopedResponse @@ -92,16 +108,24 @@ def __call__(self, environ, start_response): ) ) - transaction = continue_trace( - environ, - op=OP.HTTP_SERVER, - name="generic WSGI request", - source=TRANSACTION_SOURCE_ROUTE, - origin=self.span_origin, - ) + method = environ.get("REQUEST_METHOD", "").upper() + transaction = None + if method in self.http_methods_to_capture: + transaction = continue_trace( + environ, + op=OP.HTTP_SERVER, + name="generic WSGI request", + source=TRANSACTION_SOURCE_ROUTE, + origin=self.span_origin, + ) - with sentry_sdk.start_transaction( - transaction, custom_sampling_context={"wsgi_environ": environ} + with ( + sentry_sdk.start_transaction( + transaction, + custom_sampling_context={"wsgi_environ": environ}, + ) + if transaction is not None + else nullcontext() ): try: response = self.app( @@ -120,7 +144,7 @@ def __call__(self, environ, start_response): def _sentry_start_response( # type: ignore old_start_response, # type: StartResponse - transaction, # type: Transaction + transaction, # type: Optional[Transaction] status, # type: str response_headers, # type: WsgiResponseHeaders exc_info=None, # type: Optional[WsgiExcInfo] @@ -128,7 +152,8 @@ def _sentry_start_response( # type: ignore # type: (...) -> WsgiResponseIter with capture_internal_exceptions(): status_int = int(status.split(" ", 1)[0]) - transaction.set_http_status(status_int) + if transaction is not None: + transaction.set_http_status(status_int) if exc_info is None: # The Django Rest Framework WSGI test client, and likely other diff --git a/tests/integrations/django/myapp/urls.py b/tests/integrations/django/myapp/urls.py index b9e821afa8..79dd4edd52 100644 --- a/tests/integrations/django/myapp/urls.py +++ b/tests/integrations/django/myapp/urls.py @@ -43,6 +43,7 @@ def path(path, *args, **kwargs): ), path("middleware-exc", views.message, name="middleware_exc"), path("message", views.message, name="message"), + path("nomessage", views.nomessage, name="nomessage"), path("view-with-signal", views.view_with_signal, name="view_with_signal"), path("mylogin", views.mylogin, name="mylogin"), path("classbased", views.ClassBasedView.as_view(), name="classbased"), diff --git a/tests/integrations/django/myapp/views.py b/tests/integrations/django/myapp/views.py index c1950059fe..5e8cc39053 100644 --- a/tests/integrations/django/myapp/views.py +++ b/tests/integrations/django/myapp/views.py @@ -115,6 +115,11 @@ def message(request): return HttpResponse("ok") +@csrf_exempt +def nomessage(request): + return HttpResponse("ok") + + @csrf_exempt def view_with_signal(request): custom_signal = Signal() diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index f02f8ee217..2089f1e936 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -145,7 +145,11 @@ def test_transaction_with_class_view(sentry_init, client, capture_events): def test_has_trace_if_performance_enabled(sentry_init, client, capture_events): sentry_init( - integrations=[DjangoIntegration()], + integrations=[ + DjangoIntegration( + http_methods_to_capture=("HEAD",), + ) + ], traces_sample_rate=1.0, ) events = capture_events() @@ -192,7 +196,11 @@ def test_has_trace_if_performance_disabled(sentry_init, client, capture_events): def test_trace_from_headers_if_performance_enabled(sentry_init, client, capture_events): sentry_init( - integrations=[DjangoIntegration()], + integrations=[ + DjangoIntegration( + http_methods_to_capture=("HEAD",), + ) + ], traces_sample_rate=1.0, ) @@ -225,7 +233,11 @@ def test_trace_from_headers_if_performance_disabled( sentry_init, client, capture_events ): sentry_init( - integrations=[DjangoIntegration()], + integrations=[ + DjangoIntegration( + http_methods_to_capture=("HEAD",), + ) + ], ) events = capture_events() @@ -1183,3 +1195,48 @@ def test_span_origin(sentry_init, client, capture_events): signal_span_found = True assert signal_span_found + + +def test_transaction_http_method_default(sentry_init, client, capture_events): + """ + By default OPTIONS and HEAD requests do not create a transaction. + """ + sentry_init( + integrations=[DjangoIntegration()], + traces_sample_rate=1.0, + ) + events = capture_events() + + client.get("/nomessage") + client.options("/nomessage") + client.head("/nomessage") + + (event,) = events + + assert len(events) == 1 + assert event["request"]["method"] == "GET" + + +def test_transaction_http_method_custom(sentry_init, client, capture_events): + sentry_init( + integrations=[ + DjangoIntegration( + http_methods_to_capture=( + "OPTIONS", + "head", + ), # capitalization does not matter + ) + ], + traces_sample_rate=1.0, + ) + events = capture_events() + + client.get("/nomessage") + client.options("/nomessage") + client.head("/nomessage") + + assert len(events) == 2 + + (event1, event2) = events + assert event1["request"]["method"] == "OPTIONS" + assert event2["request"]["method"] == "HEAD" diff --git a/tests/integrations/fastapi/test_fastapi.py b/tests/integrations/fastapi/test_fastapi.py index 0603455186..93d048c029 100644 --- a/tests/integrations/fastapi/test_fastapi.py +++ b/tests/integrations/fastapi/test_fastapi.py @@ -1,10 +1,11 @@ import json import logging +import pytest import threading import warnings from unittest import mock -import pytest +import fastapi from fastapi import FastAPI, HTTPException, Request from fastapi.testclient import TestClient from fastapi.middleware.trustedhost import TrustedHostMiddleware @@ -13,6 +14,10 @@ from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.integrations.fastapi import FastApiIntegration from sentry_sdk.integrations.starlette import StarletteIntegration +from sentry_sdk.utils import parse_version + + +FASTAPI_VERSION = parse_version(fastapi.__version__) from tests.integrations.starlette import test_starlette @@ -31,6 +36,17 @@ async def _message(): capture_message("Hi") return {"message": "Hi"} + @app.delete("/nomessage") + @app.get("/nomessage") + @app.head("/nomessage") + @app.options("/nomessage") + @app.patch("/nomessage") + @app.post("/nomessage") + @app.put("/nomessage") + @app.trace("/nomessage") + async def _nomessage(): + return {"message": "nothing here..."} + @app.get("/message/{message_id}") async def _message_with_id(message_id): capture_message("Hi") @@ -548,6 +564,84 @@ async def _error(): assert not events +@pytest.mark.skipif( + FASTAPI_VERSION < (0, 80), + reason="Requires FastAPI >= 0.80, because earlier versions do not support HTTP 'HEAD' requests", +) +def test_transaction_http_method_default(sentry_init, capture_events): + """ + By default OPTIONS and HEAD requests do not create a transaction. + """ + # FastAPI is heavily based on Starlette so we also need + # to enable StarletteIntegration. + # In the future this will be auto enabled. + sentry_init( + traces_sample_rate=1.0, + integrations=[ + StarletteIntegration(), + FastApiIntegration(), + ], + ) + + app = fastapi_app_factory() + + events = capture_events() + + client = TestClient(app) + client.get("/nomessage") + client.options("/nomessage") + client.head("/nomessage") + + assert len(events) == 1 + + (event,) = events + + assert event["request"]["method"] == "GET" + + +@pytest.mark.skipif( + FASTAPI_VERSION < (0, 80), + reason="Requires FastAPI >= 0.80, because earlier versions do not support HTTP 'HEAD' requests", +) +def test_transaction_http_method_custom(sentry_init, capture_events): + # FastAPI is heavily based on Starlette so we also need + # to enable StarletteIntegration. + # In the future this will be auto enabled. + sentry_init( + traces_sample_rate=1.0, + integrations=[ + StarletteIntegration( + http_methods_to_capture=( + "OPTIONS", + "head", + ), # capitalization does not matter + ), + FastApiIntegration( + http_methods_to_capture=( + "OPTIONS", + "head", + ), # capitalization does not matter + ), + ], + ) + + app = fastapi_app_factory() + + events = capture_events() + + client = TestClient(app) + client.get("/nomessage") + client.options("/nomessage") + client.head("/nomessage") + + assert len(events) == 2 + + (event1, event2) = events + + assert event1["request"]["method"] == "OPTIONS" + assert event2["request"]["method"] == "HEAD" + + @test_starlette.parametrize_test_configurable_status_codes def test_configurable_status_codes( sentry_init, diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index 03a3b0b9d0..6febb12b8b 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -47,6 +47,10 @@ def hi(): capture_message("hi") return "ok" + @app.route("/nomessage") + def nohi(): + return "ok" + @app.route("/message/") def hi_with_id(message_id): capture_message("hi again") @@ -962,3 +966,71 @@ def test_span_origin(sentry_init, app, capture_events): (_, event) = events assert event["contexts"]["trace"]["origin"] == "auto.http.flask" + + +def test_transaction_http_method_default( + sentry_init, + app, + capture_events, +): + """ + By default OPTIONS and HEAD requests do not create a transaction. + """ + sentry_init( + traces_sample_rate=1.0, + integrations=[flask_sentry.FlaskIntegration()], + ) + events = capture_events() + + client = app.test_client() + response = client.get("/nomessage") + assert response.status_code == 200 + + response = client.options("/nomessage") + assert response.status_code == 200 + + response = client.head("/nomessage") + assert response.status_code == 200 + + (event,) = events + + assert len(events) == 1 + assert event["request"]["method"] == "GET" + + +def test_transaction_http_method_custom( + sentry_init, + app, + capture_events, +): + """ + Configure FlaskIntegration to ONLY capture OPTIONS and HEAD requests. + """ + sentry_init( + traces_sample_rate=1.0, + integrations=[ + flask_sentry.FlaskIntegration( + http_methods_to_capture=( + "OPTIONS", + "head", + ) # capitalization does not matter + ) # case does not matter + ], + ) + events = capture_events() + + client = app.test_client() + response = client.get("/nomessage") + assert response.status_code == 200 + + response = client.options("/nomessage") + assert response.status_code == 200 + + response = client.head("/nomessage") + assert response.status_code == 200 + + assert len(events) == 2 + + (event1, event2) = events + assert event1["request"]["method"] == "OPTIONS" + assert event2["request"]["method"] == "HEAD" diff --git a/tests/integrations/starlette/test_starlette.py b/tests/integrations/starlette/test_starlette.py index 097ecbdcf7..1ba9eb7589 100644 --- a/tests/integrations/starlette/test_starlette.py +++ b/tests/integrations/starlette/test_starlette.py @@ -113,6 +113,9 @@ async def _message(request): capture_message("hi") return starlette.responses.JSONResponse({"status": "ok"}) + async def _nomessage(request): + return starlette.responses.JSONResponse({"status": "ok"}) + async def _message_with_id(request): capture_message("hi") return starlette.responses.JSONResponse({"status": "ok"}) @@ -142,12 +145,25 @@ async def _render_template(request): } return templates.TemplateResponse("trace_meta.html", template_context) + all_methods = [ + "CONNECT", + "DELETE", + "GET", + "HEAD", + "OPTIONS", + "PATCH", + "POST", + "PUT", + "TRACE", + ] + app = starlette.applications.Starlette( debug=debug, routes=[ starlette.routing.Route("/some_url", _homepage), starlette.routing.Route("/custom_error", _custom_error), starlette.routing.Route("/message", _message), + starlette.routing.Route("/nomessage", _nomessage, methods=all_methods), starlette.routing.Route("/message/{message_id}", _message_with_id), starlette.routing.Route("/sync/thread_ids", _thread_ids_sync), starlette.routing.Route("/async/thread_ids", _thread_ids_async), @@ -1210,6 +1226,70 @@ async def _error(request): assert not events +@pytest.mark.skipif( + STARLETTE_VERSION < (0, 21), + reason="Requires Starlette >= 0.21, because earlier versions do not support HTTP 'HEAD' requests", +) +def test_transaction_http_method_default(sentry_init, capture_events): + """ + By default OPTIONS and HEAD requests do not create a transaction. + """ + sentry_init( + traces_sample_rate=1.0, + integrations=[ + StarletteIntegration(), + ], + ) + events = capture_events() + + starlette_app = starlette_app_factory() + + client = TestClient(starlette_app) + client.get("/nomessage") + client.options("/nomessage") + client.head("/nomessage") + + assert len(events) == 1 + + (event,) = events + + assert event["request"]["method"] == "GET" + + +@pytest.mark.skipif( + STARLETTE_VERSION < (0, 21), + reason="Requires Starlette >= 0.21, because earlier versions do not support HTTP 'HEAD' requests", +) +def test_transaction_http_method_custom(sentry_init, capture_events): + sentry_init( + traces_sample_rate=1.0, + integrations=[ + StarletteIntegration( + http_methods_to_capture=( + "OPTIONS", + "head", + ), # capitalization does not matter + ), + ], + debug=True, + ) + events = capture_events() + + starlette_app = starlette_app_factory() + + client = TestClient(starlette_app) + client.get("/nomessage") + client.options("/nomessage") + client.head("/nomessage") + + assert len(events) == 2 + + (event1, event2) = events + + assert event1["request"]["method"] == "OPTIONS" + assert event2["request"]["method"] == "HEAD" + + parametrize_test_configurable_status_codes = pytest.mark.parametrize( ("failed_request_status_codes", "status_code", "expected_error"), (