Skip to content

Commit 05af184

Browse files
committed
Fixed an issue #2352 where http_server_request_duration metrics was being recorded for excluded urls.
1 parent 4ae8662 commit 05af184

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
## Unreleased
1313

1414
### Fixed
15+
- `opentelemetry-instrumentation-flask` Fixed an issue where http_server_request_duration metrics was being recorded for excluded urls.
1516

1617
### Added
1718
- `opentelemetry-instrumentation`: botocore: Add support for AWS Secrets Manager semantic convention attribute

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -395,31 +395,35 @@ def _start_response(status, response_headers, *args, **kwargs):
395395
return start_response(status, response_headers, *args, **kwargs)
396396

397397
result = wsgi_app(wrapped_app_environ, _start_response)
398-
duration_s = default_timer() - start
399-
if duration_histogram_old:
400-
duration_attrs_old = otel_wsgi._parse_duration_attrs(
401-
attributes, _StabilityMode.DEFAULT
402-
)
398+
if flask.request and (
399+
excluded_urls is None
400+
or not excluded_urls.url_disabled(flask.request.url)
401+
):
402+
duration_s = default_timer() - start
403+
if duration_histogram_old:
404+
duration_attrs_old = otel_wsgi._parse_duration_attrs(
405+
attributes, _StabilityMode.DEFAULT
406+
)
403407

404-
if request_route:
405-
# http.target to be included in old semantic conventions
406-
duration_attrs_old[HTTP_TARGET] = str(request_route)
408+
if request_route:
409+
# http.target to be included in old semantic conventions
410+
duration_attrs_old[HTTP_TARGET] = str(request_route)
407411

408-
duration_histogram_old.record(
409-
max(round(duration_s * 1000), 0), duration_attrs_old
410-
)
411-
if duration_histogram_new:
412-
duration_attrs_new = otel_wsgi._parse_duration_attrs(
413-
attributes, _StabilityMode.HTTP
414-
)
412+
duration_histogram_old.record(
413+
max(round(duration_s * 1000), 0), duration_attrs_old
414+
)
415+
if duration_histogram_new:
416+
duration_attrs_new = otel_wsgi._parse_duration_attrs(
417+
attributes, _StabilityMode.HTTP
418+
)
415419

416-
if request_route:
417-
duration_attrs_new[HTTP_ROUTE] = str(request_route)
420+
if request_route:
421+
duration_attrs_new[HTTP_ROUTE] = str(request_route)
418422

419-
duration_histogram_new.record(
420-
max(duration_s, 0), duration_attrs_new
421-
)
422-
active_requests_counter.add(-1, active_requests_count_attrs)
423+
duration_histogram_new.record(
424+
max(duration_s, 0), duration_attrs_new
425+
)
426+
active_requests_counter.add(-1, active_requests_count_attrs)
423427
return result
424428

425429
return _wrapped_app

0 commit comments

Comments
 (0)