fix(flask): align http.server.active_requests metric with semconv helper (#4094)

* fix(flask): correct HTTP metrics handling with semconv opt-in

* fix(flask): avoid hardcoded legacy http.server.duration metric name

* changelog: note flask http server metrics consistency fix

* fix(flask): use semconv constant for legacy http.server.duration metric

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

* Update CHANGELOG.md

* fix(flask): explicitly split active requests metric by semconv mode

* fix(flask): align http.server.active_requests metric with semconv helper

---------

Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com>
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
This commit is contained in:
Ritesh Tripathi
2026-03-02 16:54:07 +05:30
committed by GitHub
parent b865493be8
commit 4880e33217
2 changed files with 24 additions and 11 deletions

View File

@@ -63,6 +63,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- `opentelemetry-instrumentation-flask`: Align `http.server.active_requests` initialization with semantic convention helpers to ensure consistent names, units, and descriptions.
([#4094](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4094))
- `opentelemetry-instrumentation-asyncio`: Fix environment variables not appearing in Read the Docs documentation
([#4256](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4256))
- `opentelemetry-instrumentation-mysql`: Refactor MySQL integration test mocks to use concrete DBAPI connection attributes, reducing noisy attribute type warnings.

View File

@@ -287,6 +287,9 @@ from opentelemetry.semconv._incubating.attributes.http_attributes import (
HTTP_ROUTE,
HTTP_TARGET,
)
from opentelemetry.semconv._incubating.metrics.http_metrics import (
create_http_server_active_requests,
)
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_SERVER_REQUEST_DURATION,
@@ -299,7 +302,6 @@ from opentelemetry.util.http import (
)
_logger = getLogger(__name__)
# Global constants for Flask 3.1+ streaming context cleanup
_IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(
flask.__version__
@@ -692,11 +694,15 @@ class _InstrumentedFlask(flask.Flask):
description="Duration of HTTP server requests.",
explicit_bucket_boundaries_advisory=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
)
active_requests_counter = meter.create_up_down_counter(
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
unit="requests",
description="measures the number of concurrent HTTP requests that are currently in-flight",
)
if _report_new(_InstrumentedFlask._sem_conv_opt_in_mode):
active_requests_counter = create_http_server_active_requests(meter)
else:
active_requests_counter = meter.create_up_down_counter(
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
unit="requests",
description="Measures the number of concurrent HTTP requests that are currently in-flight.",
)
self.wsgi_app = _rewrapped_app(
self.wsgi_app,
@@ -826,11 +832,16 @@ class FlaskInstrumentor(BaseInstrumentor):
description="Duration of HTTP server requests.",
explicit_bucket_boundaries_advisory=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
)
active_requests_counter = meter.create_up_down_counter(
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
unit="{request}",
description="Number of active HTTP server requests.",
)
if _report_new(sem_conv_opt_in_mode):
active_requests_counter = create_http_server_active_requests(
meter
)
else:
active_requests_counter = meter.create_up_down_counter(
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
unit="requests",
description="Measures the number of concurrent HTTP requests that are currently in-flight.",
)
app._original_wsgi_app = app.wsgi_app
app.wsgi_app = _rewrapped_app(