From 05af184107ab2bfbb87aa6d4ae6b383300458223 Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Mon, 29 Sep 2025 14:22:48 -0700 Subject: [PATCH 1/5] Fixed an issue #2352 where http_server_request_duration metrics was being recorded for excluded urls. --- CHANGELOG.md | 1 + .../instrumentation/flask/__init__.py | 46 ++++++++++--------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8349f4b942..c292bcbac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ 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. ### Added - `opentelemetry-instrumentation`: botocore: Add support for AWS Secrets Manager semantic convention attribute 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..b390ed0247 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -395,31 +395,35 @@ 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 - ) - active_requests_counter.add(-1, active_requests_count_attrs) + duration_histogram_new.record( + max(duration_s, 0), duration_attrs_new + ) + active_requests_counter.add(-1, active_requests_count_attrs) return result return _wrapped_app From 19ec8c7559c5bf40d395635ba8718be31b774281 Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Mon, 29 Sep 2025 14:27:53 -0700 Subject: [PATCH 2/5] Updated CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c292bcbac1..69fb8eac4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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)) ### Added - `opentelemetry-instrumentation`: botocore: Add support for AWS Secrets Manager semantic convention attribute From 6cba6616251e068d12a63c3ae6a61c70bca21814 Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Mon, 29 Sep 2025 14:56:11 -0700 Subject: [PATCH 3/5] Fix indentation --- .../src/opentelemetry/instrumentation/flask/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 b390ed0247..310d11b322 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -396,9 +396,9 @@ def _start_response(status, response_headers, *args, **kwargs): result = wsgi_app(wrapped_app_environ, _start_response) if flask.request and ( - excluded_urls is None - or not excluded_urls.url_disabled(flask.request.url) - ): + 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( From 23d51d8be6ca3f32f02b070f68cfbdae38263e6b Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Mon, 29 Sep 2025 16:18:33 -0700 Subject: [PATCH 4/5] Fix tests --- .../src/opentelemetry/instrumentation/flask/__init__.py | 2 +- .../tests/test_programmatic.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 310d11b322..2dfe135ff2 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -423,7 +423,7 @@ def _start_response(status, response_headers, *args, **kwargs): duration_histogram_new.record( max(duration_s, 0), duration_attrs_new ) - active_requests_counter.add(-1, active_requests_count_attrs) + active_requests_counter.add(-1, active_requests_count_attrs) return result return _wrapped_app diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index 60f08c7cd2..92cdff1a73 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -523,7 +523,7 @@ def test_flask_metrics(self): attr, _recommended_metrics_attrs_old[metric.name], ) - self.assertTrue(number_data_point_seen and histogram_data_point_seen) + self.assertFalse(number_data_point_seen and histogram_data_point_seen) def test_flask_metrics_new_semconv(self): start = default_timer() @@ -561,7 +561,7 @@ 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.assertFalse(number_data_point_seen and histogram_data_point_seen) def test_flask_metric_values(self): start = default_timer() From b3b53b97b5704c8a6e83f1eb7b3cb1b5885339e2 Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Mon, 29 Sep 2025 16:29:36 -0700 Subject: [PATCH 5/5] Fix assert condition --- .../tests/test_programmatic.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index 92cdff1a73..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.assertFalse(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.assertFalse(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()