@@ -120,6 +120,12 @@ def handle_exception(self, *args, **kwargs):
120120 custom = {"app" : self .app },
121121 handled = False ,
122122 )
123+ # End the transaction here, as `request_finished` won't be called when an
124+ # unhandled exception occurs.
125+ #
126+ # Unfortunately, that also means that we can't capture any response data,
127+ # as the response isn't ready at this point in time.
128+ self .client .end_transaction (result = "HTTP 5xx" )
123129
124130 def init_app (self , app , ** defaults ):
125131 self .app = app
@@ -182,11 +188,6 @@ def request_started(self, app):
182188 else :
183189 trace_parent = None
184190 self .client .begin_transaction ("request" , trace_parent = trace_parent )
185-
186- def request_finished (self , app , response ):
187- if not self .app .debug or self .client .config .debug :
188- rule = request .url_rule .rule if request .url_rule is not None else ""
189- rule = build_name_with_http_method_prefix (rule , request )
190191 elasticapm .set_context (
191192 lambda : get_data_from_request (
192193 request ,
@@ -195,14 +196,19 @@ def request_finished(self, app, response):
195196 ),
196197 "request" ,
197198 )
199+ rule = request .url_rule .rule if request .url_rule is not None else ""
200+ rule = build_name_with_http_method_prefix (rule , request )
201+ elasticapm .set_transaction_name (rule , override = False )
202+
203+ def request_finished (self , app , response ):
204+ if not self .app .debug or self .client .config .debug :
198205 elasticapm .set_context (
199206 lambda : get_data_from_response (response , capture_headers = self .client .config .capture_headers ), "response"
200207 )
201208 if response .status_code :
202209 result = "HTTP {}xx" .format (response .status_code // 100 )
203210 else :
204211 result = response .status
205- elasticapm .set_transaction_name (rule , override = False )
206212 elasticapm .set_transaction_result (result , override = False )
207213 # Instead of calling end_transaction here, we defer the call until the response is closed.
208214 # This ensures that we capture things that happen until the WSGI server closes the response.
0 commit comments