Ejemplo n.º 1
0
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.]+')
Ejemplo n.º 2
0
    def init_prometheus_flask_exporter(self, app):
        enable_exporter_flask = app.config.get(
            "PROMETHEUS_ENABLE_EXPORTER_FLASK", False)

        if not enable_exporter_flask:
            LOGGER.debug(
                f"Prometheus Flask exporter is not enabled for {app.name}.")
            return

        prefix = app.name
        metrics_flask = PrometheusMetrics(app=None, defaults_prefix=prefix)
        metrics_flask.init_app(app)
        LOGGER.debug(
            f"Prometheus Flask exporter is initialized with prefix {prefix}.")
Ejemplo n.º 3
0
class AppFactoryTest(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)
        self.metrics.init_app(self.app)

    def test_restricted(self):
        @self.app.route('/test')
        def test():
            return 'OK'

        self.client.get('/test')

        response = self.client.get('/metrics')
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/metrics?name[]=flask_exporter_info')
        self.assertEqual(response.status_code, 200)

        response = self.client.get(
            '/metrics'
            '?name[]=flask_http_request_duration_seconds_bucket'
            '&name[]=flask_http_request_duration_seconds_count'
            '&name[]=flask_http_request_duration_seconds_sum')
        self.assertEqual(response.status_code, 200)

        self.assertIn(
            'flask_http_request_duration_seconds_bucket{le="0.1",method="GET",path="/test",status="200"} 1.0',
            str(response.data))
        self.assertIn(
            'flask_http_request_duration_seconds_count{method="GET",path="/test",status="200"} 1.0',
            str(response.data))
Ejemplo n.º 4
0
from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app=None, path='/metrics')

app.debug = True


@app.route("/", methods=['GET'])
def index():
    return "hello world"


if __name__ == '__main__':
    metrics.init_app(app)
    app.run(host='0.0.0.0', port=80)
Ejemplo n.º 5
0
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.]+')

    def test_restful_with_blueprints(self):
        try:
            from flask_restful import Resource, Api
        except ImportError:
            self.skipTest('Flask-RESTful is not available')
            return

        class SampleResource(Resource):
            status = 200

            @self.metrics.summary('requests_by_status',
                                  'Request latencies by status',
                                  labels={'status': lambda r: r.status_code})
            def get(self):
                if 'fail' in request.args:
                    return 'Not OK', 400
                else:
                    return 'OK'

        blueprint = Blueprint('v1', __name__, url_prefix='/v1')
        api = Api(blueprint)

        api.add_resource(SampleResource, '/sample', endpoint='api_sample')

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

        self.client.get('/v1/sample')
        self.client.get('/v1/sample')
        self.client.get('/v1/sample?fail=1')

        response = self.client.get('/metrics')
        self.assertEqual(response.status_code, 200)

        self.assertIn('requests_by_status_count{status="200"} 2.0',
                      str(response.data))
        self.assertRegex(str(response.data),
                         'requests_by_status_sum{status="200"} [0-9.]+')

        self.assertIn('requests_by_status_count{status="400"} 1.0',
                      str(response.data))
        self.assertRegex(str(response.data),
                         'requests_by_status_sum{status="400"} [0-9.]+')
Ejemplo n.º 6
0
def start_prometheus_metrics(app):
    _prometheus_metrics = PrometheusMetrics(app=None)
    _prometheus_metrics.init_app(app)
    from lorem_ipsum.views import JsonCollector
    REGISTRY.register(JsonCollector())