fix: Cleanup prometheus metrics for dead workers

The current "cleanup" function that is removing orphan prometheus files
is actually never invoked.
We move this to a default gunicorn config file to register our hook(s).

https://docs.gunicorn.org/en/stable/configure.html
https://docs.gunicorn.org/en/stable/settings.html#child-exit
Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2023-08-18 22:04:55 +02:00
parent f05f1dbac7
commit 3005e82f60
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
2 changed files with 7 additions and 7 deletions

View file

@ -13,7 +13,6 @@ from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from jinja2 import TemplateNotFound from jinja2 import TemplateNotFound
from prometheus_client import multiprocess
from sqlalchemy import and_ from sqlalchemy import and_
from starlette.exceptions import HTTPException as StarletteHTTPException from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.authentication import AuthenticationMiddleware
@ -91,12 +90,6 @@ async def app_startup():
get_engine() get_engine()
def child_exit(server, worker): # pragma: no cover
"""This function is required for gunicorn customization
of prometheus multiprocessing."""
multiprocess.mark_process_dead(worker.pid)
async def internal_server_error(request: Request, exc: Exception) -> Response: async def internal_server_error(request: Request, exc: Exception) -> Response:
""" """
Catch all uncaught Exceptions thrown in a route. Catch all uncaught Exceptions thrown in a route.

7
gunicorn.conf.py Normal file
View file

@ -0,0 +1,7 @@
from prometheus_client import multiprocess
def child_exit(server, worker): # pragma: no cover
"""This function is required for gunicorn customization
of prometheus multiprocessing."""
multiprocess.mark_process_dead(worker.pid)