diff --git a/CHANGELOG.md b/CHANGELOG.md index 95d22a4600..b26122d09f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Fixed +- `opentelemetry-instrumentation-flask` Fixed an issue where http_server_request_duration metrics was being recorded for excluded urls. + ([#3794](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3794)) - `opentelemetry-instrumentation-dbapi`: fix crash retrieving libpq version when enabling commenter with psycopg ([#3796](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3796)) diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 9a22383e1b..2dfe135ff2 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -395,30 +395,34 @@ def _start_response(status, response_headers, *args, **kwargs): return start_response(status, response_headers, *args, **kwargs) result = wsgi_app(wrapped_app_environ, _start_response) - duration_s = default_timer() - start - if duration_histogram_old: - duration_attrs_old = otel_wsgi._parse_duration_attrs( - attributes, _StabilityMode.DEFAULT - ) + if flask.request and ( + excluded_urls is None + or not excluded_urls.url_disabled(flask.request.url) + ): + duration_s = default_timer() - start + if duration_histogram_old: + duration_attrs_old = otel_wsgi._parse_duration_attrs( + attributes, _StabilityMode.DEFAULT + ) - if request_route: - # http.target to be included in old semantic conventions - duration_attrs_old[HTTP_TARGET] = str(request_route) + if request_route: + # http.target to be included in old semantic conventions + duration_attrs_old[HTTP_TARGET] = str(request_route) - duration_histogram_old.record( - max(round(duration_s * 1000), 0), duration_attrs_old - ) - if duration_histogram_new: - duration_attrs_new = otel_wsgi._parse_duration_attrs( - attributes, _StabilityMode.HTTP - ) + duration_histogram_old.record( + max(round(duration_s * 1000), 0), duration_attrs_old + ) + if duration_histogram_new: + duration_attrs_new = otel_wsgi._parse_duration_attrs( + attributes, _StabilityMode.HTTP + ) - if request_route: - duration_attrs_new[HTTP_ROUTE] = str(request_route) + if request_route: + duration_attrs_new[HTTP_ROUTE] = str(request_route) - duration_histogram_new.record( - max(duration_s, 0), duration_attrs_new - ) + duration_histogram_new.record( + max(duration_s, 0), duration_attrs_new + ) active_requests_counter.add(-1, active_requests_count_attrs) return result diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index 60f08c7cd2..3ff2345297 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -523,7 +523,8 @@ def test_flask_metrics(self): attr, _recommended_metrics_attrs_old[metric.name], ) - self.assertTrue(number_data_point_seen and histogram_data_point_seen) + self.assertTrue(number_data_point_seen) + self.assertFalse(histogram_data_point_seen) def test_flask_metrics_new_semconv(self): start = default_timer() @@ -561,7 +562,8 @@ def test_flask_metrics_new_semconv(self): attr, _recommended_metrics_attrs_new[metric.name], ) - self.assertTrue(number_data_point_seen and histogram_data_point_seen) + self.assertTrue(number_data_point_seen) + self.assertFalse(histogram_data_point_seen) def test_flask_metric_values(self): start = default_timer()