feat: move from gunicorn to waitress

Waitress, unlike unicorn, is multi-threaded. As it does not do access logs
by default, the app needs to be wrapped in TransLogger before being passed to
Waitress.

To make the switch, a custom registry is now also used instead of the global REGISTRY
as the default registry for the app. As part of this change, the default
prometheus metrics are then also registered with this new registry.

Closes: #72
This commit is contained in:
Ana Custura 2024-12-04 12:57:09 +00:00 committed by irl
parent ffe097b24f
commit c50d341c26
4 changed files with 27 additions and 15 deletions

View file

@ -7,7 +7,7 @@ from flask import Flask, redirect, url_for, send_from_directory
from flask.typing import ResponseReturnValue
from prometheus_client import make_wsgi_app, Metric, CollectorRegistry
from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily
from prometheus_client.registry import Collector
from prometheus_client.registry import Collector, REGISTRY
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from werkzeug.middleware.dispatcher import DispatcherMiddleware
@ -24,10 +24,18 @@ from app.tfstate import tfstate
app = Flask(__name__)
app.config.from_file("../config.yaml", load=yaml.safe_load)
# create new registry to avoid multiple metrics registration in global REGISTRY
registry = CollectorRegistry()
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { # type: ignore[method-assign]
'/metrics': make_wsgi_app()
'/metrics': make_wsgi_app(registry)
})
#register default collectors to our new registry
collectors = list(REGISTRY._collector_to_names.keys())
for collector in collectors:
registry.register(collector)
db.init_app(app)
migrate.init_app(app, db, render_as_batch=True)
bootstrap.init_app(app)
@ -129,9 +137,8 @@ class AutomationCollector(Collector):
ok.add_metric(["automation_state"], 0)
yield ok
# register all custom collectors to registry
if not_migrating() and 'DISABLE_METRICS' not in os.environ:
registry = CollectorRegistry()
registry.register(DefinedProxiesCollector())
registry.register(BlockedProxiesCollector())
registry.register(AutomationCollector())