-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix double-counting exception with Gunicorn
Fixes #113
- Loading branch information
Viktor Adam
committed
Nov 21, 2021
1 parent
17ba4f7
commit c9ff2b1
Showing
13 changed files
with
171 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM python:3.8-alpine | ||
|
||
ADD examples/gunicorn-exceptions-113/requirements.txt /tmp/requirements.txt | ||
RUN pip install -r /tmp/requirements.txt | ||
|
||
ADD . /tmp/latest | ||
RUN pip install -e /tmp/latest --upgrade | ||
|
||
ADD examples/gunicorn-exceptions-113/server.py examples/gunicorn-exceptions-113/config.py /var/flask/ | ||
WORKDIR /var/flask | ||
|
||
ENV PROMETHEUS_MULTIPROC_DIR /tmp | ||
|
||
CMD gunicorn -c config.py -w 4 -b 0.0.0.0:4000 server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Gunicorn example | ||
|
||
This [Gunicorn](https://gunicorn.org/) example has a sample app in [server.py](server.py) with | ||
multiprocessing enabled for metrics collection. | ||
|
||
This example checks that exception metrics are not counted twice due to both the Flask `after_request` | ||
and `teardown_request` callbacks seeing that request. | ||
|
||
## Thanks | ||
|
||
Huge thanks for [@idlefella](https://github.com/idlefella) for | ||
bringing this to my attention in [prometheus_flask_exporter#113](https://github.com/rycus86/prometheus_flask_exporter/issues/113) ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics | ||
|
||
|
||
def child_exit(server, worker): | ||
GunicornInternalPrometheusMetrics.mark_process_dead_on_child_exit(worker.pid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
flask | ||
gunicorn | ||
prometheus_client | ||
prometheus_flask_exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/bash | ||
|
||
cd "$(dirname "$0")" | ||
|
||
_fail() { | ||
docker rm -f gunicorn-sample > /dev/null 2>&1 | ||
exit 1 | ||
} | ||
|
||
docker build -f Dockerfile -t gunicorn-sample ../../. > /dev/null || _fail | ||
docker run -d --name gunicorn-sample -p 4000:4000 -p 9200:9200 gunicorn-sample > /dev/null || _fail | ||
|
||
echo 'Waiting for Gunicorn to start...' | ||
|
||
for _ in $(seq 1 10); do | ||
PROCESS_COUNT=$(docker exec gunicorn-sample sh -c 'pgrep -a gunicorn | wc -l') | ||
if [ "$PROCESS_COUNT" -ge 5 ]; then | ||
break | ||
fi | ||
done | ||
|
||
echo 'Starting the tests...' | ||
|
||
for _ in $(seq 1 10); do | ||
curl -s http://localhost:4000/test > /dev/null | ||
done | ||
|
||
curl -s http://localhost:4000/metrics \ | ||
| grep 'flask_http_request_total{method="GET",status="500"} 10.0' \ | ||
> /dev/null | ||
|
||
if [ "$?" != "0" ]; then | ||
echo 'The expected metrics are not found' | ||
_fail | ||
fi | ||
|
||
curl -s http://localhost:4000/metrics \ | ||
| grep 'flask_http_request_duration_seconds_count{method="GET",path="/test",status="500"} 10.0' \ | ||
> /dev/null | ||
|
||
if [ "$?" != "0" ]; then | ||
echo 'The expected metrics are not found' | ||
_fail | ||
fi | ||
|
||
docker rm -f gunicorn-sample > /dev/null | ||
echo 'OK, all done' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from flask import Flask | ||
from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics | ||
|
||
application = Flask(__name__) | ||
metrics = GunicornInternalPrometheusMetrics(application) | ||
|
||
# static information as metric | ||
metrics.info('app_info', 'Application info', version='1.0.3') | ||
|
||
|
||
@application.route('/test') | ||
def main(): | ||
raise Exception("Crashing") | ||
pass # requests tracked by default | ||
|
||
|
||
if __name__ == '__main__': | ||
application.run(debug=False, port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters