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