@@ -305,8 +305,8 @@ def _wrapped_app(wrapped_app_environ, start_response):
305
305
wrapped_app_environ [_ENVIRON_STARTTIME_KEY ] = time_ns ()
306
306
start = default_timer ()
307
307
attributes = otel_wsgi .collect_request_attributes (wrapped_app_environ )
308
- active_requests_count_attrs = otel_wsgi . _parse_active_request_count_attrs (
309
- attributes
308
+ active_requests_count_attrs = (
309
+ otel_wsgi . _parse_active_request_count_attrs ( attributes )
310
310
)
311
311
duration_attrs = otel_wsgi ._parse_duration_attrs (attributes )
312
312
active_requests_counter .add (1 , active_requests_count_attrs )
@@ -326,15 +326,20 @@ def _start_response(status, response_headers, *args, **kwargs):
326
326
)
327
327
328
328
if span :
329
- otel_wsgi .add_response_attributes (span , status , response_headers )
329
+ otel_wsgi .add_response_attributes (
330
+ span , status , response_headers
331
+ )
330
332
status_code = otel_wsgi ._parse_status_code (status )
331
333
if status_code is not None :
332
- duration_attrs [SpanAttributes .HTTP_STATUS_CODE ] = status_code
333
- if span .is_recording () and span .kind == trace .SpanKind .SERVER :
334
- custom_attributes = (
335
- otel_wsgi .collect_custom_response_headers_attributes (
336
- response_headers
337
- )
334
+ duration_attrs [
335
+ SpanAttributes .HTTP_STATUS_CODE
336
+ ] = status_code
337
+ if (
338
+ span .is_recording ()
339
+ and span .kind == trace .SpanKind .SERVER
340
+ ):
341
+ custom_attributes = otel_wsgi .collect_custom_response_headers_attributes (
342
+ response_headers
338
343
)
339
344
if len (custom_attributes ) > 0 :
340
345
span .set_attributes (custom_attributes )
@@ -382,16 +387,22 @@ def _before_request():
382
387
request_hook (span , flask_request_environ )
383
388
384
389
if span .is_recording ():
385
- attributes = otel_wsgi .collect_request_attributes (flask_request_environ )
390
+ attributes = otel_wsgi .collect_request_attributes (
391
+ flask_request_environ
392
+ )
386
393
if flask .request .url_rule :
387
394
# For 404 that result from no route found, etc, we
388
395
# don't have a url_rule.
389
- attributes [SpanAttributes .HTTP_ROUTE ] = flask .request .url_rule .rule
396
+ attributes [
397
+ SpanAttributes .HTTP_ROUTE
398
+ ] = flask .request .url_rule .rule
390
399
for key , value in attributes .items ():
391
400
span .set_attribute (key , value )
392
401
if span .is_recording () and span .kind == trace .SpanKind .SERVER :
393
- custom_attributes = otel_wsgi .collect_custom_request_headers_attributes (
394
- flask_request_environ
402
+ custom_attributes = (
403
+ otel_wsgi .collect_custom_request_headers_attributes (
404
+ flask_request_environ
405
+ )
395
406
)
396
407
if len (custom_attributes ) > 0 :
397
408
span .set_attributes (custom_attributes )
@@ -411,7 +422,10 @@ def _before_request():
411
422
if flask and flask .request :
412
423
if commenter_options .get ("framework" , True ):
413
424
flask_info ["framework" ] = f"flask:{ flask .__version__ } "
414
- if commenter_options .get ("controller" , True ) and flask .request .endpoint :
425
+ if (
426
+ commenter_options .get ("controller" , True )
427
+ and flask .request .endpoint
428
+ ):
415
429
flask_info ["controller" ] = flask .request .endpoint
416
430
if (
417
431
commenter_options .get ("route" , True )
@@ -437,7 +451,9 @@ def _teardown_request(exc):
437
451
438
452
activation = flask .request .environ .get (_ENVIRON_ACTIVATION_KEY )
439
453
440
- original_reqctx_ref = flask .request .environ .get (_ENVIRON_REQCTX_REF_KEY )
454
+ original_reqctx_ref = flask .request .environ .get (
455
+ _ENVIRON_REQCTX_REF_KEY
456
+ )
441
457
current_reqctx_ref = _request_ctx_ref ()
442
458
if not activation or original_reqctx_ref != current_reqctx_ref :
443
459
# This request didn't start a span, maybe because it was created in
@@ -454,7 +470,9 @@ def _teardown_request(exc):
454
470
if exc is None :
455
471
activation .__exit__ (None , None , None )
456
472
else :
457
- activation .__exit__ (type (exc ), exc , getattr (exc , "__traceback__" , None ))
473
+ activation .__exit__ (
474
+ type (exc ), exc , getattr (exc , "__traceback__" , None )
475
+ )
458
476
459
477
if flask .request .environ .get (_ENVIRON_TOKEN , None ):
460
478
context .detach (flask .request .environ .get (_ENVIRON_TOKEN ))
@@ -477,7 +495,9 @@ def __init__(self, *args, **kwargs):
477
495
self ._original_wsgi_app = self .wsgi_app
478
496
self ._is_instrumented_by_opentelemetry = True
479
497
480
- meter = get_meter (__name__ , __version__ , _InstrumentedFlask ._meter_provider )
498
+ meter = get_meter (
499
+ __name__ , __version__ , _InstrumentedFlask ._meter_provider
500
+ )
481
501
duration_histogram = meter .create_histogram (
482
502
name = MetricInstruments .HTTP_SERVER_DURATION ,
483
503
unit = "ms" ,
@@ -603,7 +623,9 @@ def instrument_app(
603
623
tracer ,
604
624
excluded_urls = excluded_urls ,
605
625
enable_commenter = enable_commenter ,
606
- commenter_options = commenter_options if commenter_options else {},
626
+ commenter_options = commenter_options
627
+ if commenter_options
628
+ else {},
607
629
)
608
630
app ._before_request = _before_request
609
631
app .before_request (_before_request )
@@ -631,5 +653,6 @@ def uninstrument_app(app):
631
653
app ._is_instrumented_by_opentelemetry = False
632
654
else :
633
655
_logger .warning (
634
- "Attempting to uninstrument Flask " "app while already uninstrumented"
656
+ "Attempting to uninstrument Flask "
657
+ "app while already uninstrumented"
635
658
)
0 commit comments