From 76b7be67fce6375244e4b00f0fdda5fb896e056e Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:21:06 +0200 Subject: [PATCH] feat: let admins exclude routes like health checks from OpenTelemetry metrics (#33329) With OpenTelemetry metrics on, every request is counted in the HTTP request and duration metrics, so Kubernetes liveness and readiness probes on `/health` and `/health/db` fill the time series database and dashboards with noise. These metrics now skip any request whose URL matches `OTEL_PYTHON_FASTAPI_EXCLUDED_URLS` (or `OTEL_PYTHON_EXCLUDED_URLS` as a fallback), the standard OpenTelemetry variable that traces already honour. It takes a comma-separated list of regular expressions such as `/health,/_app/immutable/.*`, each matched anywhere in the URL, so `/health` also covers `/health/db`. One pattern now drops the same requests from traces and metrics alike. Nothing changes when the variable is unset. Closes #31609 --- backend/open_webui/utils/telemetry/metrics.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/open_webui/utils/telemetry/metrics.py b/backend/open_webui/utils/telemetry/metrics.py index 036e08389d..e730748489 100644 --- a/backend/open_webui/utils/telemetry/metrics.py +++ b/backend/open_webui/utils/telemetry/metrics.py @@ -41,12 +41,14 @@ from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import ( from opentelemetry.exporter.otlp.proto.http.metric_exporter import ( OTLPMetricExporter as OTLPHttpMetricExporter, ) +from opentelemetry.instrumentation.asgi import get_host_port_url_tuple from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import ( PeriodicExportingMetricReader, ) from opentelemetry.sdk.metrics.view import View from opentelemetry.sdk.resources import Resource +from opentelemetry.util.http import get_excluded_urls from sqlalchemy import Engine, func, select from sqlalchemy.orm import Session @@ -222,9 +224,16 @@ def setup_metrics(app: FastAPI, resource: Resource, db_engine: Engine) -> None: callbacks=[observe_users_active_today], ) + # OTEL_PYTHON_FASTAPI_EXCLUDED_URLS, shared with traces + excluded_urls = get_excluded_urls('FASTAPI') + # FastAPI middleware @app.middleware('http') async def _metrics_middleware(request: Request, call_next): + _, _, url = get_host_port_url_tuple(request.scope) + if excluded_urls.url_disabled(url): + return await call_next(request) + start_time = time.perf_counter() status_code = None