Skip to content

Commit

Permalink
Better handling of blueprint handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
rycus86 committed May 18, 2019
1 parent 678dbf3 commit 0203d62
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
10 changes: 7 additions & 3 deletions prometheus_flask_exporter/__init__.py
Expand Up @@ -12,7 +12,6 @@
from prometheus_client import Counter, Histogram, Gauge, Summary
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST


NO_PREFIX = '#no_prefix'
"""
Constant indicating that default metrics should not have any prefix applied.
Expand Down Expand Up @@ -463,7 +462,12 @@ def func(*args, **kwargs):
response_for_metric = response

if not isinstance(response, Response):
if request.endpoint == f.__name__:
endpoint_name = request.endpoint or ''

if request.blueprint and '.' in endpoint_name:
endpoint_name = endpoint_name.rsplit('.', 1)[1]

if endpoint_name == f.__name__:
# we are in a request handler method
response_for_metric = make_response(response)

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


__version__ = '0.7.2'
__version__ = '0.7.3'
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.7.2',
version='0.7.3',
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.7.2.tar.gz',
download_url='https://github.com/rycus86/prometheus_flask_exporter/archive/0.7.3.tar.gz',
keywords=['prometheus', 'flask', 'monitoring', 'exporter'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
39 changes: 39 additions & 0 deletions tests/test_blueprint.py
@@ -0,0 +1,39 @@
import sys
import unittest

from flask import Flask, Blueprint
from prometheus_client import CollectorRegistry
from prometheus_flask_exporter import PrometheusMetrics


class BlueprintTest(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.testing = True
self.client = self.app.test_client()

if sys.version_info.major < 3:
self.assertRegex = self.assertRegexpMatches
self.assertNotRegex = self.assertNotRegexpMatches

registry = CollectorRegistry(auto_describe=True)
self.metrics = PrometheusMetrics(app=None, registry=registry)

def test_blueprint(self):
blueprint = Blueprint('test-blueprint', __name__)

@blueprint.route('/test')
@self.metrics.summary('requests_by_status', 'Request latencies by status',
labels={'status': lambda r: r.status_code})
def test():
return 'OK'

self.app.register_blueprint(blueprint)
self.metrics.init_app(self.app)

self.client.get('/test')

response = self.client.get('/metrics')
self.assertEqual(response.status_code, 200)
self.assertIn('requests_by_status_count{status="200"} 1.0', str(response.data))
self.assertRegex(str(response.data), 'requests_by_status_sum{status="200"} [0-9.]+')

0 comments on commit 0203d62

Please sign in to comment.