Skip to content

Commit 76934fa

Browse files
fix
1 parent 10b49de commit 76934fa

File tree

2 files changed

+48
-21
lines changed
  • instrumentation/opentelemetry-instrumentation-flask

2 files changed

+48
-21
lines changed

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

+42-19
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ def _wrapped_app(wrapped_app_environ, start_response):
305305
wrapped_app_environ[_ENVIRON_STARTTIME_KEY] = time_ns()
306306
start = default_timer()
307307
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)
310310
)
311311
duration_attrs = otel_wsgi._parse_duration_attrs(attributes)
312312
active_requests_counter.add(1, active_requests_count_attrs)
@@ -326,15 +326,20 @@ def _start_response(status, response_headers, *args, **kwargs):
326326
)
327327

328328
if span:
329-
otel_wsgi.add_response_attributes(span, status, response_headers)
329+
otel_wsgi.add_response_attributes(
330+
span, status, response_headers
331+
)
330332
status_code = otel_wsgi._parse_status_code(status)
331333
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
338343
)
339344
if len(custom_attributes) > 0:
340345
span.set_attributes(custom_attributes)
@@ -382,16 +387,22 @@ def _before_request():
382387
request_hook(span, flask_request_environ)
383388

384389
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+
)
386393
if flask.request.url_rule:
387394
# For 404 that result from no route found, etc, we
388395
# 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
390399
for key, value in attributes.items():
391400
span.set_attribute(key, value)
392401
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+
)
395406
)
396407
if len(custom_attributes) > 0:
397408
span.set_attributes(custom_attributes)
@@ -411,7 +422,10 @@ def _before_request():
411422
if flask and flask.request:
412423
if commenter_options.get("framework", True):
413424
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+
):
415429
flask_info["controller"] = flask.request.endpoint
416430
if (
417431
commenter_options.get("route", True)
@@ -437,7 +451,9 @@ def _teardown_request(exc):
437451

438452
activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
439453

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+
)
441457
current_reqctx_ref = _request_ctx_ref()
442458
if not activation or original_reqctx_ref != current_reqctx_ref:
443459
# This request didn't start a span, maybe because it was created in
@@ -454,7 +470,9 @@ def _teardown_request(exc):
454470
if exc is None:
455471
activation.__exit__(None, None, None)
456472
else:
457-
activation.__exit__(type(exc), exc, getattr(exc, "__traceback__", None))
473+
activation.__exit__(
474+
type(exc), exc, getattr(exc, "__traceback__", None)
475+
)
458476

459477
if flask.request.environ.get(_ENVIRON_TOKEN, None):
460478
context.detach(flask.request.environ.get(_ENVIRON_TOKEN))
@@ -477,7 +495,9 @@ def __init__(self, *args, **kwargs):
477495
self._original_wsgi_app = self.wsgi_app
478496
self._is_instrumented_by_opentelemetry = True
479497

480-
meter = get_meter(__name__, __version__, _InstrumentedFlask._meter_provider)
498+
meter = get_meter(
499+
__name__, __version__, _InstrumentedFlask._meter_provider
500+
)
481501
duration_histogram = meter.create_histogram(
482502
name=MetricInstruments.HTTP_SERVER_DURATION,
483503
unit="ms",
@@ -603,7 +623,9 @@ def instrument_app(
603623
tracer,
604624
excluded_urls=excluded_urls,
605625
enable_commenter=enable_commenter,
606-
commenter_options=commenter_options if commenter_options else {},
626+
commenter_options=commenter_options
627+
if commenter_options
628+
else {},
607629
)
608630
app._before_request = _before_request
609631
app.before_request(_before_request)
@@ -631,5 +653,6 @@ def uninstrument_app(app):
631653
app._is_instrumented_by_opentelemetry = False
632654
else:
633655
_logger.warning(
634-
"Attempting to uninstrument Flask " "app while already uninstrumented"
656+
"Attempting to uninstrument Flask "
657+
"app while already uninstrumented"
635658
)

instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def _custom_response_headers():
7676
resp = flask.Response("test response")
7777
resp.headers["content-type"] = "text/plain; charset=utf-8"
7878
resp.headers["content-length"] = "13"
79-
resp.headers["my-custom-header"] = "my-custom-value-1,my-custom-header-2"
79+
resp.headers[
80+
"my-custom-header"
81+
] = "my-custom-value-1,my-custom-header-2"
8082
resp.headers[
8183
"my-custom-regex-header-1"
8284
] = "my-custom-regex-value-1,my-custom-regex-value-2"
@@ -101,7 +103,9 @@ def excluded2_endpoint():
101103
self.app.route("/excluded/<int:helloid>")(self._hello_endpoint)
102104
self.app.route("/excluded")(excluded_endpoint)
103105
self.app.route("/excluded2")(excluded2_endpoint)
104-
self.app.route("/test_custom_response_headers")(self._custom_response_headers)
106+
self.app.route("/test_custom_response_headers")(
107+
self._custom_response_headers
108+
)
105109

106110
# pylint: disable=attribute-defined-outside-init
107111
self.client = Client(self.app, Response)

0 commit comments

Comments
 (0)