From 8a41a0879c94836c6b20ef3df6b0f956e7c5ecf6 Mon Sep 17 00:00:00 2001 From: Roman Plevka Date: Thu, 21 Nov 2024 16:26:26 +0100 Subject: [PATCH] implement basic prometheus metric endpoint --- rekuper.py | 13 +++++++++++++ requirements.txt | 1 + 2 files changed, 14 insertions(+) diff --git a/rekuper.py b/rekuper.py index d9e154d..41730ab 100755 --- a/rekuper.py +++ b/rekuper.py @@ -4,10 +4,13 @@ from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_restx import Api, Resource, fields +from prometheus_flask_exporter import PrometheusMetrics from sqlalchemy import text from sqlalchemy.exc import IntegrityError, SQLAlchemyError app = Flask("__name__") +metrics = PrometheusMetrics(app) + if settings.database.get("connection_string") is None: db_creds = f"{settings.database.username}:{settings.database.password}" db_host = settings.database.host @@ -362,5 +365,15 @@ def get(self): return [container.to_dict() for container in containers], 200 +# register routes to the prometheus metrics +metrics.register_default( + metrics.counter( + "by_path_counter", + "Request count by request paths", + labels={"path": lambda: request.path}, + ) +) + + if __name__ == "__main__": app.run(debug=True) diff --git a/requirements.txt b/requirements.txt index 6d6fbff..d3de8a2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ Flask Flask-SQLAlchemy Flask-Migrate flask-restx +prometheus-flask-exporter psycopg2-binary requests