Nicolas Berthier
Nicolas Berthier

Reputation: 508

prometheus_client custom metrics with Flask

I am trying to service custom Prometheus metrics through Flask. Looking at https://github.com/prometheus/client_python, I have a code similar to:

from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from prometheus_client import make_wsgi_app

# Create my app
app = Flask(__name__)

# Add prometheus wsgi middleware to route /metrics requests
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
    '/metrics': make_wsgi_app()
})

With this setup, I'm not really sure where I should declare my custom metrics?

Upvotes: 2

Views: 2805

Answers (1)

Nicolas Berthier
Nicolas Berthier

Reputation: 508

Solved the issue by registering my custom collector to the REGISTRY with

REGISTRY.register(CustomCollector())

and then using

DispatcherMiddleware(
    app.wsgi_app, {"/metrics": make_wsgi_app(REGISTRY),}
)

Upvotes: 2

Related Questions