From 09942e57a97fb943e5ff740749ce149f018d6da6 Mon Sep 17 00:00:00 2001 From: Nicolas Ledez Date: Mon, 2 Oct 2023 16:48:57 +0200 Subject: [PATCH] Add prometheus /metrics --- setup.cfg | 1 + zou/app/__init__.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/setup.cfg b/setup.cfg index 2e31c788d2..437b579040 100644 --- a/setup.cfg +++ b/setup.cfg @@ -58,6 +58,7 @@ install_requires = OpenTimelineIO==0.15.0 orjson==3.9.7 pillow==10.0.1 + prometheus-client==0.17.1 psutil==5.9.5 psycopg[binary]==3.1.12 pygelf==0.4.2 diff --git a/zou/app/__init__.py b/zou/app/__init__.py index 2dd2852471..73555085e1 100644 --- a/zou/app/__init__.py +++ b/zou/app/__init__.py @@ -1,6 +1,8 @@ import os import flask_fs import traceback +import time +import threading from flask import Flask, jsonify, current_app from flasgger import Swagger @@ -10,6 +12,9 @@ from flask_migrate import Migrate from flask_mail import Mail +from werkzeug.middleware.dispatcher import DispatcherMiddleware +import prometheus_client + from jwt import ExpiredSignatureError from babel.core import UnknownLocaleError from meilisearch.errors import ( @@ -58,6 +63,21 @@ app, template=swagger.swagger_template, config=swagger.swagger_config ) +BACKGROUND_TASKS = prometheus_client.Counter( + "background_tasks_total", "Background tasks executed." +) + + +def background_task(): + """Background task to simulate and update metrics""" + while True: + time.sleep(5) # Run every 5 seconds + BACKGROUND_TASKS.inc() # Increment our counter + + +app.wsgi_app = DispatcherMiddleware( + app.wsgi_app, {"/metrics": prometheus_client.make_wsgi_app()} +) if config.SENTRY_DEBUG_URL: @@ -168,5 +188,8 @@ def load_api(): fs.mkdir_p(app.config["TMP_DIR"]) configure_auth() + t = threading.Thread(target=background_task) + t.start() + load_api()