11import weakref
2+ import contextlib
23from inspect import iscoroutinefunction
34
45from sentry_sdk .hub import Hub , _should_send_default_pii
6+ from sentry_sdk .tracing import Transaction
57from sentry_sdk .utils import (
68 HAS_REAL_CONTEXTVARS ,
79 CONTEXTVARS_ERROR_MESSAGE ,
3234 from typing import Optional
3335 from typing import Dict
3436 from typing import Callable
37+ from typing import Generator
3538
3639 from sentry_sdk ._types import EventProcessor
3740
@@ -63,38 +66,16 @@ def setup_once():
6366 # Starting Tornado 6 RequestHandler._execute method is a standard Python coroutine (async/await)
6467 # In that case our method should be a coroutine function too
6568 async def sentry_execute_request_handler (self , * args , ** kwargs ):
66- # type: (Any, *Any, **Any) -> Any
67- hub = Hub .current
68- integration = hub .get_integration (TornadoIntegration )
69- if integration is None :
70- return await old_execute (self , * args , ** kwargs )
71-
72- weak_handler = weakref .ref (self )
73-
74- with Hub (hub ) as hub :
75- with hub .configure_scope () as scope :
76- scope .clear_breadcrumbs ()
77- processor = _make_event_processor (weak_handler ) # type: ignore
78- scope .add_event_processor (processor )
69+ # type: (RequestHandler, *Any, **Any) -> Any
70+ with _handle_request_impl (self ):
7971 return await old_execute (self , * args , ** kwargs )
8072
8173 else :
8274
8375 @coroutine # type: ignore
8476 def sentry_execute_request_handler (self , * args , ** kwargs ):
8577 # type: (RequestHandler, *Any, **Any) -> Any
86- hub = Hub .current
87- integration = hub .get_integration (TornadoIntegration )
88- if integration is None :
89- return old_execute (self , * args , ** kwargs )
90-
91- weak_handler = weakref .ref (self )
92-
93- with Hub (hub ) as hub :
94- with hub .configure_scope () as scope :
95- scope .clear_breadcrumbs ()
96- processor = _make_event_processor (weak_handler ) # type: ignore
97- scope .add_event_processor (processor )
78+ with _handle_request_impl (self ):
9879 result = yield from old_execute (self , * args , ** kwargs )
9980 return result
10081
@@ -110,6 +91,39 @@ def sentry_log_exception(self, ty, value, tb, *args, **kwargs):
11091 RequestHandler .log_exception = sentry_log_exception # type: ignore
11192
11293
94+ @contextlib .contextmanager
95+ def _handle_request_impl (self ):
96+ # type: (RequestHandler) -> Generator[None, None, None]
97+ hub = Hub .current
98+ integration = hub .get_integration (TornadoIntegration )
99+
100+ if integration is None :
101+ yield
102+
103+ weak_handler = weakref .ref (self )
104+
105+ with Hub (hub ) as hub :
106+ with hub .configure_scope () as scope :
107+ scope .clear_breadcrumbs ()
108+ processor = _make_event_processor (weak_handler ) # type: ignore
109+ scope .add_event_processor (processor )
110+
111+ transaction = Transaction .continue_from_headers (
112+ self .request .headers ,
113+ op = "http.server" ,
114+ # Like with all other integrations, this is our
115+ # fallback transaction in case there is no route.
116+ # sentry_urldispatcher_resolve is responsible for
117+ # setting a transaction name later.
118+ name = "generic Tornado request" ,
119+ )
120+
121+ with hub .start_transaction (
122+ transaction , custom_sampling_context = {"tornado_request" : self .request }
123+ ):
124+ yield
125+
126+
113127def _capture_exception (ty , value , tb ):
114128 # type: (type, BaseException, Any) -> None
115129 hub = Hub .current
0 commit comments