Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat: Add Prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Aug 29, 2023
1 parent dda4550 commit 5bdfcfc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from flask_socketio import SocketIO
from sqlalchemy import MetaData
from sqlalchemy.engine import URL
from prometheus_client import multiprocess
from prometheus_client.core import CollectorRegistry
from prometheus_flask_exporter import PrometheusMetrics
from werkzeug.exceptions import MethodNotAllowed
from app.errors import NotFoundRequest, UnauthorizedRequest, ForbiddenRequest, InvalidUsage
from app.util import KitchenOwlJSONProvider
from flask import Flask, request
from flask_basicauth import BasicAuth
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
Expand All @@ -27,6 +31,8 @@
OPEN_REGISTRATION = os.getenv('OPEN_REGISTRATION', "False").lower() == "true"
EMAIL_MANDATORY = os.getenv('EMAIL_MANDATORY', "False").lower() == "true"

COLLECT_METRICS = os.getenv('COLLECT_METRICS', "False").lower() == "true"

DB_URL = URL.create(
os.getenv('DB_DRIVER', "sqlite"),
username=os.getenv('DB_USER'),
Expand Down Expand Up @@ -73,7 +79,10 @@
app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET_KEY', 'super-secret')
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = JWT_ACCESS_TOKEN_EXPIRES
app.config["JWT_REFRESH_TOKEN_EXPIRES"] = JWT_REFRESH_TOKEN_EXPIRES

if COLLECT_METRICS:
# BASIC_AUTH
app.config['BASIC_AUTH_USERNAME'] = os.getenv('METRICS_USER', "kitchenowl")
app.config['BASIC_AUTH_PASSWORD'] = os.getenv('METRICS_PASSWORD', "ZqQtidgC5n3YXb")

convention = {
"ix": 'ix_%(column_0_label)s',
Expand All @@ -91,6 +100,12 @@
jwt = JWTManager(app)
socketio = SocketIO(app, json=app.json, logger=app.logger,
cors_allowed_origins=os.getenv('FRONT_URL'))
if COLLECT_METRICS:
basic_auth = BasicAuth(app)
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry, path='/tmp')
metrics = PrometheusMetrics(app, registry=registry, path="/metrics/", metrics_decorator=basic_auth.required)
metrics.info('app_info', 'Application info', version=BACKEND_VERSION)

scheduler = APScheduler()
# enable for debugging jobs: ../scheduler/jobs to see scheduled jobs
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extruct==0.16.0
flake8==6.1.0
Flask==2.3.3
Flask-APScheduler==1.12.4
Flask-BasicAuth==0.2.0
Flask-Bcrypt==1.0.1
Flask-JWT-Extended==4.5.2
Flask-Migrate==4.0.4
Expand Down

0 comments on commit 5bdfcfc

Please sign in to comment.