From 8ace1bddf94269d6e05fb5219f8db7a81c7da186 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Mon, 6 Feb 2023 00:37:23 +0100 Subject: [PATCH] Fix metrics endpoint in non-gunicorn processes --- libretranslate/app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index 5f808fe..6333b33 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -197,6 +197,12 @@ def create_app(args): measure_request = None gauge_request = None if args.metrics: + if os.environ.get("PROMETHEUS_MULTIPROC_DIR") is None: + default_mp_dir = os.path.abspath(os.path.join("db", "prometheus")) + if not os.path.isdir(default_mp_dir): + os.mkdir(default_mp_dir) + os.environ["PROMETHEUS_MULTIPROC_DIR"] = default_mp_dir + from prometheus_client import CONTENT_TYPE_LATEST, Summary, Gauge, CollectorRegistry, multiprocess, generate_latest @bp.route("/metrics") @@ -206,7 +212,6 @@ def create_app(args): authorization = request.headers.get('Authorization') if authorization != "Bearer " + args.metrics_auth_token: abort(401, description=_("Unauthorized")) - registry = CollectorRegistry() multiprocess.MultiProcessCollector(registry) return Response(generate_latest(registry), mimetype=CONTENT_TYPE_LATEST)