Skip to content

Commit

Permalink
Prepare for 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rycus86 committed Feb 27, 2018
1 parent dc649b4 commit 2d95004
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -73,7 +73,9 @@ By default, the metrics are exposed on the same Flask application on the
`/metrics` endpoint and using the core Prometheus registry.
If this doesn't suit your needs, set the `path` argument to `None` and/or
the `export_defaults` argument to `False` plus change the `registry`
argument if needed.
argument if needed. The `group_by_endpoint` constructor flag makes
the default request duration metric tracked by endpoint (function)
instead of URI path.

The `register_endpoint` allows exposing the metrics endpoint on a specific path.
It also allows passing in a Flask application to register it on but defaults
Expand Down
24 changes: 15 additions & 9 deletions prometheus_flask_exporter/__init__.py
Expand Up @@ -63,30 +63,32 @@ def echo_status(status):
- Without an argument, possibly to use with the Flask `request` object
"""

def __init__(self, app, path='/metrics', export_defaults=True,
buckets=None, registry=DEFAULT_REGISTRY, group_by_endpoint=False):
def __init__(self, app, path='/metrics',
export_defaults=True, group_by_endpoint=False,
buckets=None, registry=DEFAULT_REGISTRY):
"""
Create a new Prometheus metrics export configuration.
:param app: the Flask application
:param path: the metrics path (defaults to `/metrics`)
:param export_defaults: expose all HTTP request latencies
and number of HTTP requests
:param group_by_endpoint: group default HTTP metrics
by the endpoints' function name instead of the URI path
:param buckets: the time buckets for request latencies
(will use the default when `None`)
:param registry: the Prometheus Registry to use
"""

self.app = app
self.group_by = 'endpoint' if group_by_endpoint else 'path'
self.registry = registry
self.version = __version__

if path:
self.register_endpoint(path)

if export_defaults:
self.export_defaults(buckets)
self.export_defaults(buckets, group_by_endpoint)

def register_endpoint(self, path, app=None):
"""
Expand Down Expand Up @@ -132,25 +134,29 @@ def run_app():
thread.setDaemon(True)
thread.start()

def export_defaults(self, buckets=None):
def export_defaults(self, buckets=None, group_by_endpoint=False):
"""
Export the default metrics:
- HTTP request latencies
- Number of HTTP requests
:param buckets: the time buckets for request latencies
(will use the default when `None`)
:param group_by_endpoint: group default HTTP metrics
by the endpoints' function name instead of the URI path
"""

# use the default buckets from prometheus_client if not given here
buckets_as_kwargs = {}
if buckets is not None:
buckets_as_kwargs['buckets'] = buckets

duration_group = 'endpoint' if group_by_endpoint else 'path'

histogram = Histogram(
'flask_http_request_duration_seconds',
'Flask HTTP request duration in seconds',
('method', self.group_by, 'status'),
('method', duration_group, 'status'),
registry=self.registry,
**buckets_as_kwargs
)
Expand All @@ -176,12 +182,12 @@ def after_request(response):
return response

total_time = max(default_timer() - request.prom_start_time, 0)

histogram.labels(
request.method,
getattr(request, self.group_by),
getattr(request, duration_group),
response.status_code
).observe(total_time)

counter.labels(request.method, response.status_code).inc()

return response
Expand Down Expand Up @@ -417,4 +423,4 @@ def info(self, name, description, labelnames=None, labelvalues=None, **labels):
return gauge


__version__ = '0.1.2'
__version__ = '0.2.0'
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -8,14 +8,14 @@
setup(
name='prometheus_flask_exporter',
packages=['prometheus_flask_exporter'],
version='0.1.2',
version='0.2.0',
description='Prometheus metrics exporter for Flask',
long_description=long_description,
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.1.2.tar.gz',
download_url='https://github.com/rycus86/prometheus_flask_exporter/archive/0.2.0.tar.gz',
keywords=['prometheus', 'flask', 'monitoring', 'exporter'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
6 changes: 4 additions & 2 deletions tests/test_defaults.py
Expand Up @@ -162,17 +162,19 @@ def a_test_endpoint(url):
self.assertMetric(
'flask_http_request_duration_seconds_bucket', '3.0',
('endpoint', 'a_test_endpoint'), ('status', 200),
('le', '+Inf'),('method', 'GET'),
('le', '+Inf'), ('method', 'GET'),
endpoint='/metrics'
)
self.assertMetric(
'flask_http_request_duration_seconds_count', '3.0',
('endpoint', 'a_test_endpoint'), ('status', 200), ('method', 'GET'),
endpoint='/metrics'
)

self.assertAbsent(
'flask_http_request_duration_seconds_bucket',
('path', '/test'),
('path', '/test'), ('status', 200),
('le', '+Inf'), ('method', 'GET'),
endpoint='/metrics'
)

Expand Down

0 comments on commit 2d95004

Please sign in to comment.