@@ -292,14 +292,14 @@ def _uninstrument(self, **kwargs):
292292 self .patched_handlers = []
293293
294294
295- def patch_handler_class (self , cls , request_hook = None ):
295+ def patch_handler_class (instrumentation , cls , request_hook = None ):
296296 if getattr (cls , _OTEL_PATCHED_KEY , False ):
297297 return False
298298
299299 setattr (cls , _OTEL_PATCHED_KEY , True )
300- _wrap (cls , "prepare" , partial (_prepare , self , request_hook ))
301- _wrap (cls , "on_finish" , partial (_on_finish , self ))
302- _wrap (cls , "log_exception" , partial (_log_exception , self ))
300+ _wrap (cls , "prepare" , partial (_prepare , instrumentation , request_hook ))
301+ _wrap (cls , "on_finish" , partial (_on_finish , instrumentation ))
302+ _wrap (cls , "log_exception" , partial (_log_exception , instrumentation ))
303303 return True
304304
305305
@@ -319,28 +319,36 @@ def _wrap(cls, method_name, wrapper):
319319 wrapt .apply_patch (cls , method_name , wrapper )
320320
321321
322- def _prepare (self , request_hook , func , handler , args , kwargs ):
322+ def _prepare (instrumentation , request_hook , func , handler , args , kwargs ):
323+ instrumentation .start_time = default_timer ()
323324 request = handler .request
324325 if _excluded_urls .url_disabled (request .uri ):
325326 return func (* args , ** kwargs )
326- ctx = _start_span (self , handler )
327+
328+ _record_prepare_metrics (instrumentation , handler )
329+
330+ ctx = _start_span (instrumentation .tracer , handler )
327331 if request_hook :
328332 request_hook (ctx .span , handler )
329333 return func (* args , ** kwargs )
330334
331335
332- def _on_finish (self , func , handler , args , kwargs ):
336+ def _on_finish (instrumentation , func , handler , args , kwargs ):
333337 response = func (* args , ** kwargs )
334- _finish_span (self , handler )
338+
339+ _record_on_finish_metrics (instrumentation , handler )
340+
341+ _finish_span (instrumentation .tracer , handler )
342+
335343 return response
336344
337345
338- def _log_exception (self , func , handler , args , kwargs ):
346+ def _log_exception (instrumentation , func , handler , args , kwargs ):
339347 error = None
340348 if len (args ) == 3 :
341349 error = args [1 ]
342350
343- _finish_span (self , handler , error )
351+ _finish_span (instrumentation , handler , error )
344352 return func (* args , ** kwargs )
345353
346354
@@ -406,11 +414,9 @@ def _get_full_handler_name(handler):
406414 return f"{ klass .__module__ } .{ klass .__qualname__ } "
407415
408416
409- def _start_span (self , handler ) -> _TraceContext :
410- start_time = default_timer ()
411-
417+ def _start_span (tracer , handler ) -> _TraceContext :
412418 span , token = _start_internal_or_server_span (
413- tracer = self . tracer ,
419+ tracer = tracer ,
414420 span_name = _get_operation_name (handler , handler .request ),
415421 start_time = time_ns (),
416422 context_carrier = handler .request .headers ,
@@ -429,14 +435,6 @@ def _start_span(self, handler) -> _TraceContext:
429435 if len (custom_attributes ) > 0 :
430436 span .set_attributes (custom_attributes )
431437
432- metric_attributes = _create_metric_attributes (handler )
433- request_size = len (handler .request .body )
434-
435- self .request_size_histogram .record (
436- request_size , attributes = metric_attributes
437- )
438- self .active_requests_histogram .add (1 , attributes = metric_attributes )
439-
440438 activation = trace .use_span (span , end_on_exit = True )
441439 activation .__enter__ () # pylint: disable=E1101
442440 ctx = _TraceContext (activation , span , token )
@@ -449,15 +447,10 @@ def _start_span(self, handler) -> _TraceContext:
449447 if propagator :
450448 propagator .inject (handler , setter = response_propagation_setter )
451449
452- elapsed_time = round ((default_timer () - start_time ) * 1000 )
453-
454- self .duration_histogram .record (elapsed_time , attributes = metric_attributes )
455- self .active_requests_histogram .add (- 1 , attributes = metric_attributes )
456-
457450 return ctx
458451
459452
460- def _finish_span (self , handler , error = None ):
453+ def _finish_span (tracer , handler , error = None ):
461454 status_code = handler .get_status ()
462455 reason = getattr (handler , "_reason" )
463456 finish_args = (None , None , None )
@@ -467,7 +460,7 @@ def _finish_span(self, handler, error=None):
467460 if isinstance (error , tornado .web .HTTPError ):
468461 status_code = error .status_code
469462 if not ctx and status_code == 404 :
470- ctx = _start_span (self , handler )
463+ ctx = _start_span (tracer , handler )
471464 else :
472465 status_code = 500
473466 reason = None
@@ -508,13 +501,51 @@ def _finish_span(self, handler, error=None):
508501 delattr (handler , _HANDLER_CONTEXT_KEY )
509502
510503
511- def _create_metric_attributes (handler ):
504+ def _record_prepare_metrics (instrumentation , handler ):
505+ request_size = len (handler .request .body )
506+ metric_attributes = _create_metric_attributes (handler )
507+
508+ instrumentation .request_size_histogram .record (
509+ request_size , attributes = metric_attributes
510+ )
511+
512+ active_requests_attributes = _create_active_requests_attributes (
513+ handler .request
514+ )
515+ instrumentation .active_requests_histogram .add (
516+ 1 , attributes = active_requests_attributes
517+ )
518+
519+
520+ def _record_on_finish_metrics (instrumentation , handler ):
521+ elapsed_time = round ((default_timer () - instrumentation .start_time ) * 1000 )
522+
523+ metric_attributes = _create_metric_attributes (handler )
524+ instrumentation .duration_histogram .record (
525+ elapsed_time , attributes = metric_attributes
526+ )
527+
528+ active_requests_attributes = _create_active_requests_attributes (
529+ handler .request
530+ )
531+ instrumentation .active_requests_histogram .add (
532+ - 1 , attributes = active_requests_attributes
533+ )
534+
535+
536+ def _create_active_requests_attributes (request ):
512537 metric_attributes = {
513- SpanAttributes .HTTP_METHOD : handler .request .method ,
514- SpanAttributes .HTTP_SCHEME : handler .request .protocol ,
515- SpanAttributes .HTTP_STATUS_CODE : handler .get_status (),
516- SpanAttributes .HTTP_FLAVOR : handler .request .version ,
517- SpanAttributes .HTTP_HOST : handler .request .host ,
538+ SpanAttributes .HTTP_METHOD : request .method ,
539+ SpanAttributes .HTTP_SCHEME : request .protocol ,
540+ SpanAttributes .HTTP_FLAVOR : request .version ,
541+ SpanAttributes .HTTP_HOST : request .host ,
518542 }
519543
520544 return metric_attributes
545+
546+
547+ def _create_metric_attributes (handler ):
548+ metric_attributes = _create_active_requests_attributes (handler .request )
549+ metric_attributes [SpanAttributes .HTTP_STATUS_CODE ] = handler .get_status ()
550+
551+ return metric_attributes
0 commit comments