Skip to content

Commit 4880e33

Browse files
rite7shemdnetoxrmx
authored
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>
1 parent b865493 commit 4880e33

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6363

6464
### Fixed
6565

66+
- `opentelemetry-instrumentation-flask`: Align `http.server.active_requests` initialization with semantic convention helpers to ensure consistent names, units, and descriptions.
67+
([#4094](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4094))
6668
- `opentelemetry-instrumentation-asyncio`: Fix environment variables not appearing in Read the Docs documentation
6769
([#4256](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4256))
6870
- `opentelemetry-instrumentation-mysql`: Refactor MySQL integration test mocks to use concrete DBAPI connection attributes, reducing noisy attribute type warnings.

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ def response_hook(span: Span, status: str, response_headers: List):
287287
HTTP_ROUTE,
288288
HTTP_TARGET,
289289
)
290+
from opentelemetry.semconv._incubating.metrics.http_metrics import (
291+
create_http_server_active_requests,
292+
)
290293
from opentelemetry.semconv.metrics import MetricInstruments
291294
from opentelemetry.semconv.metrics.http_metrics import (
292295
HTTP_SERVER_REQUEST_DURATION,
@@ -299,7 +302,6 @@ def response_hook(span: Span, status: str, response_headers: List):
299302
)
300303

301304
_logger = getLogger(__name__)
302-
303305
# Global constants for Flask 3.1+ streaming context cleanup
304306
_IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(
305307
flask.__version__
@@ -692,11 +694,15 @@ def __init__(self, *args, **kwargs):
692694
description="Duration of HTTP server requests.",
693695
explicit_bucket_boundaries_advisory=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
694696
)
695-
active_requests_counter = meter.create_up_down_counter(
696-
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
697-
unit="requests",
698-
description="measures the number of concurrent HTTP requests that are currently in-flight",
699-
)
697+
698+
if _report_new(_InstrumentedFlask._sem_conv_opt_in_mode):
699+
active_requests_counter = create_http_server_active_requests(meter)
700+
else:
701+
active_requests_counter = meter.create_up_down_counter(
702+
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
703+
unit="requests",
704+
description="Measures the number of concurrent HTTP requests that are currently in-flight.",
705+
)
700706

701707
self.wsgi_app = _rewrapped_app(
702708
self.wsgi_app,
@@ -826,11 +832,16 @@ def instrument_app(
826832
description="Duration of HTTP server requests.",
827833
explicit_bucket_boundaries_advisory=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
828834
)
829-
active_requests_counter = meter.create_up_down_counter(
830-
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
831-
unit="{request}",
832-
description="Number of active HTTP server requests.",
833-
)
835+
if _report_new(sem_conv_opt_in_mode):
836+
active_requests_counter = create_http_server_active_requests(
837+
meter
838+
)
839+
else:
840+
active_requests_counter = meter.create_up_down_counter(
841+
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
842+
unit="requests",
843+
description="Measures the number of concurrent HTTP requests that are currently in-flight.",
844+
)
834845

835846
app._original_wsgi_app = app.wsgi_app
836847
app.wsgi_app = _rewrapped_app(

0 commit comments

Comments
 (0)