From fcdc892a090061ce933c8922f30a1025e81ca0be Mon Sep 17 00:00:00 2001 From: Viktor Adam Date: Mon, 17 Dec 2018 23:48:29 +1100 Subject: [PATCH] Allow metrics in debug mode with DEBUG_METRICS --- .travis.yml | 10 ++-- README.md | 6 +++ examples/reload/Dockerfile | 13 +++++ examples/reload/reload_example.py | 20 +++++++ examples/reload/run_tests.sh | 76 +++++++++++++++++++++++++++ prometheus_flask_exporter/__init__.py | 4 +- setup.py | 4 +- 7 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 examples/reload/Dockerfile create mode 100644 examples/reload/reload_example.py create mode 100755 examples/reload/run_tests.sh diff --git a/.travis.yml b/.travis.yml index e71f2eb..b3cc52d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,18 +28,22 @@ script: jobs: include: - - stage: wsgi + - stage: integration sudo: true script: - sh examples/gunicorn/run_tests.sh - - stage: wsgi + - stage: integration sudo: true script: - sh examples/uwsgi/run_tests.sh - - stage: wsgi + - stage: integration sudo: true script: - sh examples/wsgi/run_tests.sh + - stage: integration + sudo: true + script: + - sh examples/reload/run_tests.sh - stage: deploy script: skip diff --git a/README.md b/README.md index 4fee58c..960e5de 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,12 @@ app with `debug=True`, are not going to be reflected in the metrics. See [https://github.com/rycus86/prometheus_flask_exporter/issues/4](https://github.com/rycus86/prometheus_flask_exporter/issues/4) for more details. +Alternatively - since version 0.5.1 -, if you set the `DEBUG_METRICS` +environment variable, you will get metrics for the latest reloaded code. +These will be exported on the main Flask app, serving the metrics on +a different port is not going to work most probably - e.g. +`PrometheusMetrics.start_http_server(..)` is not expected to work. + ## WSGI Getting accurate metrics for WSGI apps might require a bit more setup. diff --git a/examples/reload/Dockerfile b/examples/reload/Dockerfile new file mode 100644 index 0000000..20c1f58 --- /dev/null +++ b/examples/reload/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.7-alpine + +RUN apk add --no-cache curl && pip install flask prometheus_client + +ADD . /tmp/latest +RUN pip install -e /tmp/latest --upgrade + +ADD examples/reload/reload_example.py /var/flask/example.py +WORKDIR /var/flask + +ENV DEBUG_METRICS 1 + +CMD python /var/flask/example.py diff --git a/examples/reload/reload_example.py b/examples/reload/reload_example.py new file mode 100644 index 0000000..43e8d2e --- /dev/null +++ b/examples/reload/reload_example.py @@ -0,0 +1,20 @@ +from flask import Flask +from prometheus_flask_exporter import PrometheusMetrics + +app = Flask(__name__) +metrics = PrometheusMetrics(app) + + +@app.route('/test') +def index(): + return 'Hello world' + + +@app.route('/ping') +@metrics.do_not_track() +def ping(): + return 'pong' + + +if __name__ == '__main__': + app.run('0.0.0.0', 4000, debug=True) diff --git a/examples/reload/run_tests.sh b/examples/reload/run_tests.sh new file mode 100755 index 0000000..5f9da83 --- /dev/null +++ b/examples/reload/run_tests.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +cd "$(dirname "$0")" + +_fail() { + docker rm -f reload-sample > /dev/null 2>&1 + exit 1 +} + +docker build -f Dockerfile -t reload-sample ../../. > /dev/null || _fail +docker run -d --name reload-sample -p 4000:4000 reload-sample > /dev/null || _fail + +echo 'Waiting for the server to start...' + +for _ in $(seq 1 10); do + if curl -fs http://localhost:4000/ping > /dev/null; then + break + else + sleep 0.2 + fi +done + +echo 'Starting the initial tests...' + +for _ in $(seq 1 10); do + curl -s http://localhost:4000/test > /dev/null + if [ "$?" != "0" ]; then + echo 'Failed to request the test endpoint' + _fail + fi +done + +curl -s http://localhost:4000/metrics \ + | grep 'flask_http_request_duration_seconds_count{method="GET",path="/test",status="200"} 10.0' \ + > /dev/null + +if [ "$?" != "0" ]; then + echo 'The expected metrics are not found' + _fail +fi + +echo 'Changing the server...' +docker exec -it reload-sample sed -i "s#@app.route('/test')#@app.route('/changed')#" /var/flask/example.py +docker exec -it reload-sample sed -i "s#@app.route('/ping')#@app.route('/ping2')#" /var/flask/example.py + +echo 'Waiting for the server to apply the changes...' + +for _ in $(seq 1 10); do + if curl -fs http://localhost:4000/ping2 > /dev/null; then + break + else + sleep 0.2 + fi +done + +echo 'Starting the changed tests...' + +for _ in $(seq 1 12); do + curl -s http://localhost:4000/changed > /dev/null + if [ "$?" != "0" ]; then + echo 'Failed to request the test endpoint' + _fail + fi +done + +curl -s http://localhost:4000/metrics \ + | grep 'flask_http_request_duration_seconds_count{method="GET",path="/changed",status="200"} 12.0' \ + > /dev/null + +if [ "$?" != "0" ]; then + echo 'The expected metrics are not found' + _fail +fi + +docker rm -f reload-sample > /dev/null +echo 'OK, all done' diff --git a/prometheus_flask_exporter/__init__.py b/prometheus_flask_exporter/__init__.py index 5d0d426..9fc6984 100644 --- a/prometheus_flask_exporter/__init__.py +++ b/prometheus_flask_exporter/__init__.py @@ -146,7 +146,7 @@ def register_endpoint(self, path, app=None): (by default it is the application registered with this class) """ - if is_running_from_reloader(): + if is_running_from_reloader() and not os.environ.get('DEBUG_METRICS'): return if app is None: @@ -522,4 +522,4 @@ def info(self, name, description, labelnames=None, labelvalues=None, **labels): return gauge -__version__ = '0.5.0' +__version__ = '0.5.1' diff --git a/setup.py b/setup.py index cdd1b74..141cd6a 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='prometheus_flask_exporter', packages=['prometheus_flask_exporter'], - version='0.5.0', + version='0.5.1', description='Prometheus metrics exporter for Flask', long_description=long_description, long_description_content_type='text/markdown', @@ -14,7 +14,7 @@ author='Viktor Adam', author_email='rycus86@gmail.com', url='https://github.com/rycus86/prometheus_flask_exporter', - download_url='https://github.com/rycus86/prometheus_flask_exporter/archive/0.5.0.tar.gz', + download_url='https://github.com/rycus86/prometheus_flask_exporter/archive/0.5.1.tar.gz', keywords=['prometheus', 'flask', 'monitoring', 'exporter'], classifiers=[ 'Development Status :: 4 - Beta',