diff --git a/Dockerfile b/Dockerfile index a599984..8a4c717 100644 --- a/Dockerfile +++ b/Dockerfile @@ -101,6 +101,10 @@ ENTRYPOINT ["dumb-init", "--"] ENV PROMETHEUS_MULTIPROC_DIR /tmp ENV prometheus_multiproc_dir /tmp +ENV METRICS_PORT 9200 +EXPOSE 9200 + +COPY container/gunicorn_prometheus_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_prometheus_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_prometheus_config.py b/container/gunicorn_prometheus_config.py new file mode 100644 index 0000000..8176af4 --- /dev/null +++ b/container/gunicorn_prometheus_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 cdd6bae..8b2ed36 100644 --- a/container/wsgi.py +++ b/container/wsgi.py @@ -20,7 +20,7 @@ import os import sys -from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics +from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics from werkzeug.middleware.dispatcher import DispatcherMiddleware from prometheus_client import make_wsgi_app, CollectorRegistry from prometheus_client.core import REGISTRY @@ -35,6 +35,6 @@ app = App() REGISTRY.register(CollectorRegistry()) app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { - '/metrics': make_wsgi_app(REGISTRY) + '/metrics': make_wsgi_app(registry=REGISTRY) }) -GunicornInternalPrometheusMetrics(app, path='/metrics', registry=REGISTRY) +GunicornPrometheusMetrics(app, path='/metrics', registry=REGISTRY)