Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 103871b

Browse files
committedMay 24, 2023
format
1 parent 72136d6 commit 103871b

File tree

2 files changed

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

2 files changed

+21
-48
lines changed
 

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

+19-42
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 = (
309-
otel_wsgi._parse_active_request_count_attrs(attributes)
308+
active_requests_count_attrs = otel_wsgi._parse_active_request_count_attrs(
309+
attributes
310310
)
311311
duration_attrs = otel_wsgi._parse_duration_attrs(attributes)
312312
active_requests_counter.add(1, active_requests_count_attrs)
@@ -326,20 +326,15 @@ def _start_response(status, response_headers, *args, **kwargs):
326326
)
327327

328328
if span:
329-
otel_wsgi.add_response_attributes(
330-
span, status, response_headers
331-
)
329+
otel_wsgi.add_response_attributes(span, status, response_headers)
332330
status_code = otel_wsgi._parse_status_code(status)
333331
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+
)
343338
)
344339
if len(custom_attributes) > 0:
345340
span.set_attributes(custom_attributes)
@@ -387,22 +382,16 @@ def _before_request():
387382
request_hook(span, flask_request_environ)
388383

389384
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)
393386
if flask.request.url_rule:
394387
# For 404 that result from no route found, etc, we
395388
# 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
399390
for key, value in attributes.items():
400391
span.set_attribute(key, value)
401392
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
406395
)
407396
if len(custom_attributes) > 0:
408397
span.set_attributes(custom_attributes)
@@ -422,10 +411,7 @@ def _before_request():
422411
if flask and flask.request:
423412
if commenter_options.get("framework", True):
424413
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:
429415
flask_info["controller"] = flask.request.endpoint
430416
if (
431417
commenter_options.get("route", True)
@@ -451,9 +437,7 @@ def _teardown_request(exc):
451437

452438
activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
453439

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

477459
if flask.request.environ.get(_ENVIRON_TOKEN, None):
478460
context.detach(flask.request.environ.get(_ENVIRON_TOKEN))
@@ -495,9 +477,7 @@ def __init__(self, *args, **kwargs):
495477
self._original_wsgi_app = self.wsgi_app
496478
self._is_instrumented_by_opentelemetry = True
497479

498-
meter = get_meter(
499-
__name__, __version__, _InstrumentedFlask._meter_provider
500-
)
480+
meter = get_meter(__name__, __version__, _InstrumentedFlask._meter_provider)
501481
duration_histogram = meter.create_histogram(
502482
name=MetricInstruments.HTTP_SERVER_DURATION,
503483
unit="ms",
@@ -623,9 +603,7 @@ def instrument_app(
623603
tracer,
624604
excluded_urls=excluded_urls,
625605
enable_commenter=enable_commenter,
626-
commenter_options=commenter_options
627-
if commenter_options
628-
else {},
606+
commenter_options=commenter_options if commenter_options else {},
629607
)
630608
app._before_request = _before_request
631609
app.before_request(_before_request)
@@ -653,6 +631,5 @@ def uninstrument_app(app):
653631
app._is_instrumented_by_opentelemetry = False
654632
else:
655633
_logger.warning(
656-
"Attempting to uninstrument Flask "
657-
"app while already uninstrumented"
634+
"Attempting to uninstrument Flask " "app while already uninstrumented"
658635
)

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ def _custom_response_headers():
7777
resp = flask.Response("test response")
7878
resp.headers["content-type"] = "text/plain; charset=utf-8"
7979
resp.headers["content-length"] = "13"
80-
resp.headers[
81-
"my-custom-header"
82-
] = "my-custom-value-1,my-custom-header-2"
80+
resp.headers["my-custom-header"] = "my-custom-value-1,my-custom-header-2"
8381
resp.headers[
8482
"my-custom-regex-header-1"
8583
] = "my-custom-regex-value-1,my-custom-regex-value-2"
@@ -104,9 +102,7 @@ def excluded2_endpoint():
104102
self.app.route("/excluded/<int:helloid>")(self._hello_endpoint)
105103
self.app.route("/excluded")(excluded_endpoint)
106104
self.app.route("/excluded2")(excluded2_endpoint)
107-
self.app.route("/test_custom_response_headers")(
108-
self._custom_response_headers
109-
)
105+
self.app.route("/test_custom_response_headers")(self._custom_response_headers)
110106

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

0 commit comments

Comments
 (0)
Please sign in to comment.