Files
Aleksei Sokol 53f14a8624
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 1m40s
Version 0.4.0
Changes:
- put ObservabilityMiddleware before ExceptionHandlerMiddleware to avoid repetative code
- add application startup and last metrics update metrics along with CPU usage metric and threads count
- move host and port to new uvicorn section at config along with new reload and forwarded_allow_ips
- add request_id and remove trace_id/span_id generation if tracing is disabled
- move logging logic from utils to observability
- pass trace_id/span_id in HEX form
2026-01-03 16:29:58 +03:00

30 lines
962 B
Django/Jinja

"""Metrics dependency functions are defined here."""
from fastapi import FastAPI, Request
from {{project_slug}}.observability.metrics import Metrics
def init_dispencer(app: FastAPI, metrics: Metrics) -> None:
"""Initialize Metrics dispencer at app's state."""
if hasattr(app.state, "metrics_dep"):
if not isinstance(app.state.metrics_dep, Metrics):
raise ValueError(
f"metrics_dep attribute of app's state is already set with other value ({app.state.metrics_dep})"
)
return
app.state.metrics_dep = metrics
def from_app(app: FastAPI) -> Metrics:
"""Get a Metrics from app state."""
if not hasattr(app.state, "metrics_dep"):
raise ValueError("Metrics dispencer was not initialized at app preparation")
return app.state.metrics_dep
async def from_request(request: Request) -> Metrics:
"""Get a Metrics from request's app state."""
return from_app(request.app)