From 53f1d8d32d15bd2f75c00fd978e882ee81a4b012 Mon Sep 17 00:00:00 2001 From: charlien Date: Thu, 3 Oct 2024 18:49:08 +0200 Subject: [PATCH] use GunicornPrometheusMetrics --- Dockerfile | 8 +++++++- container/gunicorn_promexp_config.py | 11 +++++++++++ container/wsgi.py | 3 +++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 container/gunicorn_promexp_config.py diff --git a/Dockerfile b/Dockerfile index 7a5f619..e141b3b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -99,5 +99,11 @@ VOLUME /archive # Catch interrupts and send to all sub-processes ENTRYPOINT ["dumb-init", "--"] +ENV PROMETHEUS_MULTIPROC_DIR /tmp +ENV prometheus_multiproc_dir /tmp +ENV METRICS_PORT 9200 + +COPY container/gunicorn_promexp_config.py /src/ + # Start application -CMD gunicorn --worker-class sync --workers 5 --bind 0.0.0.0:8000 wsgi:app --keep-alive 5 --log-level info +CMD gunicorn -c /src/gunicorn_promexp_config.py --worker-class sync --workers 5 --bind 0.0.0.0:8000 wsgi:app --keep-alive 5 --log-level info diff --git a/container/gunicorn_promexp_config.py b/container/gunicorn_promexp_config.py new file mode 100644 index 0000000..8176af4 --- /dev/null +++ b/container/gunicorn_promexp_config.py @@ -0,0 +1,11 @@ +import os + +from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics + + +def when_ready(server): + GunicornPrometheusMetrics.start_http_server_when_ready(int(os.getenv('METRICS_PORT'))) + + +def child_exit(server, worker): + GunicornPrometheusMetrics.mark_process_dead_on_child_exit(worker.pid) diff --git a/container/wsgi.py b/container/wsgi.py index 21037c2..7f4ea3f 100644 --- a/container/wsgi.py +++ b/container/wsgi.py @@ -20,6 +20,8 @@ import os import sys +from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics + from dmci.api import App from dmci import CONFIG @@ -27,3 +29,4 @@ sys.exit(1) app = App() +GunicornPrometheusMetrics(app)