@@ -69,13 +69,35 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
69
69
return ret
70
70
71
71
72
- class TornadoHandleExceptionInstrumentation (AbstractInstrumentedModule ):
73
- name = "tornado_handle_exception "
72
+ class TornadoHandleRequestExceptionInstrumentation (AbstractInstrumentedModule ):
73
+ name = "tornado_handle_request_exception "
74
74
75
- instrument_list = [("tornado.web.RequestHandler" , "_handle_exception" )]
75
+ instrument_list = [("tornado.web.RequestHandler" , "_handle_request_exception" )]
76
+
77
+ def call (self , module , method , wrapped , instance , args , kwargs ):
78
+
79
+ # Late import to avoid ImportErrors
80
+ from tornado .web import Finish , HTTPError
81
+
82
+ e = args [0 ]
83
+ if isinstance (e , Finish ):
84
+ # Not an error; Finish is an exception that ends a request without an error response
85
+ return wrapped (* args , ** kwargs )
86
+
87
+ client = instance .application .elasticapm_client
88
+ request = instance .request
89
+ client .capture_exception (
90
+ context = {
91
+ "request" : get_data_from_request (request , capture_body = client .config .capture_body in ("all" , "errors" ))
92
+ }
93
+ )
94
+ if isinstance (e , HTTPError ):
95
+ elasticapm .set_transaction_result ("HTTP {}xx" .format (int (e .status_code / 100 )), override = False )
96
+ elasticapm .set_context ({"status_code" : e .status_code }, "response" )
97
+ else :
98
+ elasticapm .set_transaction_result ("HTTP 5xx" , override = False )
99
+ elasticapm .set_context ({"status_code" : 500 }, "response" )
76
100
77
- async def call (self , module , method , wrapped , instance , args , kwargs ):
78
- # FIXME
79
101
return wrapped (* args , ** kwargs )
80
102
81
103
0 commit comments