feat(fastapi): add prometheus /metrics

This commit provides custom metrics, so we can group requests
into their route paths and not by the arguments given, e.g.
/pkgbase/some-package -> /pkgbase/{name}. We also count RPC
requests as `http_api_requests_total`, split by the RPC
query "type" argument.

- `http_api_requests_total`
    - Labels: ["type", "status"]
- `http_requests_total`
    - Number of HTTP requests in total.
    - Labels: ["method", "path", "status"]

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-31 02:16:50 -07:00
parent cc45290ec2
commit f21765bfe4
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 310 additions and 0 deletions

View file

@ -19,11 +19,18 @@ import aurweb.logging
from aurweb.auth import BasicAuthBackend
from aurweb.db import get_engine, query
from aurweb.models import AcceptedTerm, Term
from aurweb.prometheus import http_api_requests_total, http_requests_total, instrumentator
from aurweb.routers import accounts, auth, errors, html, packages, rpc, rss, sso, trusted_user
# Setup the FastAPI app.
app = FastAPI(exception_handlers=errors.exceptions)
# Instrument routes with the prometheus-fastapi-instrumentator
# library with custom collectors and expose /metrics.
instrumentator().add(http_api_requests_total())
instrumentator().add(http_requests_total())
instrumentator().instrument(app).expose(app)
@app.on_event("startup")
async def app_startup():
@ -67,6 +74,7 @@ async def app_startup():
app.include_router(rss.router)
app.include_router(packages.router)
app.include_router(rpc.router)
# Initialize the database engine and ORM.
get_engine()