@@ -292,14 +292,14 @@ def _uninstrument(self, **kwargs):
292
292
self .patched_handlers = []
293
293
294
294
295
- def patch_handler_class (self , cls , request_hook = None ):
295
+ def patch_handler_class (instrumentation , cls , request_hook = None ):
296
296
if getattr (cls , _OTEL_PATCHED_KEY , False ):
297
297
return False
298
298
299
299
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 ))
303
303
return True
304
304
305
305
@@ -319,28 +319,36 @@ def _wrap(cls, method_name, wrapper):
319
319
wrapt .apply_patch (cls , method_name , wrapper )
320
320
321
321
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 ()
323
324
request = handler .request
324
325
if _excluded_urls .url_disabled (request .uri ):
325
326
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 )
327
331
if request_hook :
328
332
request_hook (ctx .span , handler )
329
333
return func (* args , ** kwargs )
330
334
331
335
332
- def _on_finish (self , func , handler , args , kwargs ):
336
+ def _on_finish (instrumentation , func , handler , args , kwargs ):
333
337
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
+
335
343
return response
336
344
337
345
338
- def _log_exception (self , func , handler , args , kwargs ):
346
+ def _log_exception (instrumentation , func , handler , args , kwargs ):
339
347
error = None
340
348
if len (args ) == 3 :
341
349
error = args [1 ]
342
350
343
- _finish_span (self , handler , error )
351
+ _finish_span (instrumentation , handler , error )
344
352
return func (* args , ** kwargs )
345
353
346
354
@@ -406,11 +414,9 @@ def _get_full_handler_name(handler):
406
414
return f"{ klass .__module__ } .{ klass .__qualname__ } "
407
415
408
416
409
- def _start_span (self , handler ) -> _TraceContext :
410
- start_time = default_timer ()
411
-
417
+ def _start_span (tracer , handler ) -> _TraceContext :
412
418
span , token = _start_internal_or_server_span (
413
- tracer = self . tracer ,
419
+ tracer = tracer ,
414
420
span_name = _get_operation_name (handler , handler .request ),
415
421
start_time = time_ns (),
416
422
context_carrier = handler .request .headers ,
@@ -429,14 +435,6 @@ def _start_span(self, handler) -> _TraceContext:
429
435
if len (custom_attributes ) > 0 :
430
436
span .set_attributes (custom_attributes )
431
437
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
-
440
438
activation = trace .use_span (span , end_on_exit = True )
441
439
activation .__enter__ () # pylint: disable=E1101
442
440
ctx = _TraceContext (activation , span , token )
@@ -449,15 +447,10 @@ def _start_span(self, handler) -> _TraceContext:
449
447
if propagator :
450
448
propagator .inject (handler , setter = response_propagation_setter )
451
449
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
-
457
450
return ctx
458
451
459
452
460
- def _finish_span (self , handler , error = None ):
453
+ def _finish_span (tracer , handler , error = None ):
461
454
status_code = handler .get_status ()
462
455
reason = getattr (handler , "_reason" )
463
456
finish_args = (None , None , None )
@@ -467,7 +460,7 @@ def _finish_span(self, handler, error=None):
467
460
if isinstance (error , tornado .web .HTTPError ):
468
461
status_code = error .status_code
469
462
if not ctx and status_code == 404 :
470
- ctx = _start_span (self , handler )
463
+ ctx = _start_span (tracer , handler )
471
464
else :
472
465
status_code = 500
473
466
reason = None
@@ -508,13 +501,51 @@ def _finish_span(self, handler, error=None):
508
501
delattr (handler , _HANDLER_CONTEXT_KEY )
509
502
510
503
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 ):
512
537
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 ,
518
542
}
519
543
520
544
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