Skip to content

Commit

Permalink
Allow metrics in debug mode with DEBUG_METRICS
Browse files Browse the repository at this point in the history
  • Loading branch information
rycus86 committed Dec 17, 2018
1 parent 35839fa commit fcdc892
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 7 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions 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)
76 changes: 76 additions & 0 deletions 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'
4 changes: 2 additions & 2 deletions prometheus_flask_exporter/__init__.py
Expand Up @@ -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:
Expand Down Expand Up @@ -522,4 +522,4 @@ def info(self, name, description, labelnames=None, labelvalues=None, **labels):
return gauge


__version__ = '0.5.0'
__version__ = '0.5.1'
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -6,15 +6,15 @@
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',
license='MIT',
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',
Expand Down

0 comments on commit fcdc892

Please sign in to comment.