From 172738b262c56eb9f9e6a463208edc76db73a8e6 Mon Sep 17 00:00:00 2001 From: Shane Barratt Date: Sun, 14 Aug 2016 18:50:16 -0400 Subject: [PATCH] release 0.0.1 --- .gitignore | 15 +++++++ LICENSE.md => LICENSE | 0 README.md => README | 0 .../lib/flask_prometheus/__init__.py | 0 flask_prometheus/__init__.py | 40 +++++++++++++++++++ setup.py | 10 +++-- 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 .gitignore rename LICENSE.md => LICENSE (100%) rename README.md => README (100%) rename flask_prometheus/flask_prometheus.py => build/lib/flask_prometheus/__init__.py (100%) create mode 100644 flask_prometheus/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18412ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +tests_output/ +__pycache__/ + +.tox +.coverage + +*.py[co] +*.egg-info +*.swp + +dist/ +docs/_build/ + +virtualenv/ +venv/ diff --git a/LICENSE.md b/LICENSE similarity index 100% rename from LICENSE.md rename to LICENSE diff --git a/README.md b/README similarity index 100% rename from README.md rename to README diff --git a/flask_prometheus/flask_prometheus.py b/build/lib/flask_prometheus/__init__.py similarity index 100% rename from flask_prometheus/flask_prometheus.py rename to build/lib/flask_prometheus/__init__.py diff --git a/flask_prometheus/__init__.py b/flask_prometheus/__init__.py new file mode 100644 index 0000000..1578b16 --- /dev/null +++ b/flask_prometheus/__init__.py @@ -0,0 +1,40 @@ +import time + +from prometheus_client import Counter, Histogram +from prometheus_client import start_http_server +from flask import request + +FLASK_REQUEST_LATENCY = Histogram('flask_request_latency_seconds', 'Flask Request Latency', + ['method', 'endpoint']) +FLASK_REQUEST_COUNT = Counter('flask_request_count', 'Flask Request Count', + ['method', 'endpoint', 'http_status']) + + +def before_request(): + request.start_time = time.time() + + +def after_request(response): + request_latency = time.time() - request.start_time + FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency) + FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc() + + return response + +def monitor(app, port=8000, addr=''): + app.before_request(before_request) + app.after_request(after_request) + start_http_server(port, addr) + +if __name__ == '__main__': + from flask import Flask + app = Flask(__name__) + + monitor(app, port=8000) + + @app.route('/') + def index(): + return "Hello" + + # Run the application! + app.run() diff --git a/setup.py b/setup.py index e96fe40..290326f 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ url='http://github.com/sbarratt/flask-prometheus', license='BSD', author='Shane Barratt', - author_email='stbarratt@gmail.com' + author_email='stbarratt@gmail.com', maintainer='Shane Barratt', maintainer_email='stbarratt@gmail.com', description='Prometheus client instrumentation for flask.', @@ -19,13 +19,16 @@ 'prometheus-client>=0.0.14' ], classifiers=[ - 'Environment :: Web Environment', 'Intended Audience :: Developers', + 'Development Status :: 3 - Alpha', 'License :: OSI Approved :: BSD License', + 'Operating System :: OS Independent', + 'Environment :: Web Environment', 'Topic :: System :: Monitoring', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 'Topic :: Software Development :: Libraries :: Python Modules', + 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', @@ -34,5 +37,6 @@ 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', - ] + ], + keywords='prometheus monitoring' )