fix(fastapi): utilize PROMETHEUS_MULTIPROC_DIR in our own /metrics

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-01 13:17:24 -07:00
parent 1be4ac2fde
commit dc397f6bd8
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 34 additions and 3 deletions

View file

@ -9,6 +9,7 @@ from urllib.parse import quote_plus
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from prometheus_client import multiprocess
from sqlalchemy import and_, or_
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.sessions import SessionMiddleware
@ -29,7 +30,7 @@ app = FastAPI(exception_handlers=errors.exceptions)
# library with custom collectors and expose /metrics.
instrumentator().add(http_api_requests_total())
instrumentator().add(http_requests_total())
instrumentator().instrument(app).expose(app)
instrumentator().instrument(app)
@app.on_event("startup")
@ -79,6 +80,12 @@ async def app_startup():
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)
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
"""