feat: add prometheus flask metrics

This commit is contained in:
Ana Custura 2024-12-04 13:02:01 +00:00 committed by irl
parent c50d341c26
commit 4dd0c4ed22
2 changed files with 5 additions and 1 deletions

View file

@ -20,12 +20,14 @@ from app.models.automation import Automation, AutomationState
from app.portal import portal from app.portal import portal
from app.portal.report import report from app.portal.report import report
from app.tfstate import tfstate from app.tfstate import tfstate
from prometheus_flask_exporter import PrometheusMetrics
app = Flask(__name__) app = Flask(__name__)
app.config.from_file("../config.yaml", load=yaml.safe_load) app.config.from_file("../config.yaml", load=yaml.safe_load)
# create new registry to avoid multiple metrics registration in global REGISTRY # create new registry to avoid multiple metrics registration in global REGISTRY
registry = CollectorRegistry() registry = CollectorRegistry()
metrics = PrometheusMetrics(app, registry=registry)
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { # type: ignore[method-assign] app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { # type: ignore[method-assign]
'/metrics': make_wsgi_app(registry) '/metrics': make_wsgi_app(registry)
@ -137,6 +139,7 @@ class AutomationCollector(Collector):
ok.add_metric(["automation_state"], 0) ok.add_metric(["automation_state"], 0)
yield ok yield ok
# register all custom collectors to registry # register all custom collectors to registry
if not_migrating() and 'DISABLE_METRICS' not in os.environ: if not_migrating() and 'DISABLE_METRICS' not in os.environ:
registry.register(DefinedProxiesCollector()) registry.register(DefinedProxiesCollector())

View file

@ -17,6 +17,7 @@ markupsafe
nose nose
openpyxl openpyxl
prometheus_client prometheus_client
prometheus_flask_exporter
pytest pytest
python-dateutil python-dateutil
python-gitlab python-gitlab