Python prometheus_client.make_wsgi_app() Examples

The following are 4 code examples of prometheus_client.make_wsgi_app(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module prometheus_client , or try the search function .
Example #1
Source File: __init__.py    From squealy with MIT License 6 votes vote down vote up
def _add_promethueus_middleware(app):
    # Add prometheus wsgi middleware to route /metrics requests
    # application object is then used by wsgi / gunicorn to startup the application
    # NOTE: This means prometheus metrics are not exposed in development mode
    wsgi_app = DispatcherMiddleware(app, {
        '/metrics': make_wsgi_app()
    })
    return wsgi_app 
Example #2
Source File: test_wsgi.py    From client_python with Apache License 2.0 6 votes vote down vote up
def validate_metrics(self, metric_name, help_text, increments):
        """
        WSGI app serves the metrics from the provided registry.
        """
        c = Counter(metric_name, help_text, registry=self.registry)
        for _ in range(increments):
            c.inc()
        # Create and run WSGI app
        app = make_wsgi_app(self.registry)
        outputs = app(self.environ, self.capture)
        # Assert outputs
        self.assertEqual(len(outputs), 1)
        output = outputs[0].decode('utf8')
        # Status code
        self.assertEqual(self.captured_status, "200 OK")
        # Headers
        self.assertEqual(len(self.captured_headers), 1)
        self.assertEqual(self.captured_headers[0], ("Content-Type", CONTENT_TYPE_LATEST))
        # Body
        self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
        self.assertIn("# TYPE " + metric_name + "_total counter\n", output)
        self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output) 
Example #3
Source File: 4-4-app.py    From examples with Apache License 2.0 5 votes vote down vote up
def app(environ, start_fn):
    REQUESTS.inc()
    if environ['PATH_INFO'] == '/metrics':
        registry = CollectorRegistry()
        multiprocess.MultiProcessCollector(registry)
        metrics_app = make_wsgi_app(registry)
        return metrics_app(environ, start_fn)
    start_fn('200 OK', [])
    return [b'Hello World'] 
Example #4
Source File: web.py    From aliyun-exporter with Apache License 2.0 4 votes vote down vote up
def create_app(config: CollectorConfig):

    app = Flask(__name__, instance_relative_config=True)

    client = AcsClient(
        ak=config.credential['access_key_id'],
        secret=config.credential['access_key_secret'],
        region_id=config.credential['region_id']
    )

    @app.route("/")
    def projectIndex():
        req = QueryProjectMetaRequest()
        req.set_PageSize(100)
        try:
            resp = client.do_action_with_exception(req)
        except Exception as e:
            return render_template("error.html", errorMsg=e)
        data = json.loads(resp)
        return render_template("index.html", projects=data["Resources"]["Resource"])

    @app.route("/projects/<string:name>")
    def projectDetail(name):
        req = QueryMetricMetaRequest()
        req.set_PageSize(100)
        req.set_Project(name)
        try:
            resp = client.do_action_with_exception(req)
        except Exception as e:
            return render_template("error.html", errorMsg=e)
        data = json.loads(resp)
        return render_template("detail.html", metrics=data["Resources"]["Resource"], project=name)

    @app.route("/yaml/<string:name>")
    def projectYaml(name):
        req = QueryMetricMetaRequest()
        req.set_PageSize(100)
        req.set_Project(name)
        try:
            resp = client.do_action_with_exception(req)
        except Exception as e:
            return render_template("error.html", errorMsg=e)
        data = json.loads(resp)
        return render_template("yaml.html", metrics=data["Resources"]["Resource"], project=name)

    app.jinja_env.filters['formatmetric'] = format_metric
    app.jinja_env.filters['formatperiod'] = format_period

    app_dispatch = DispatcherMiddleware(app, {
        '/metrics': make_wsgi_app()
    })
    return app_dispatch