Version 0.4.0
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 1m40s

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
This commit is contained in:
2026-01-03 11:01:43 +03:00
parent b8acb017fd
commit 53f14a8624
26 changed files with 901 additions and 730 deletions

View File

@@ -9,7 +9,7 @@ def init_dispencer(app: FastAPI, logger: BoundLogger) -> None:
if hasattr(app.state, "logger"):
if not isinstance(app.state.logger_dep, BoundLogger):
raise ValueError(
"logger attribute of app's state is already set" f"with other value ({app.state.logger_dep})"
f"logger attribute of app's state is already set with other value ({app.state.logger_dep})"
)
return
@@ -24,14 +24,17 @@ def attach_to_request(request: Request, logger: BoundLogger) -> None:
request.state.logger_dep = logger
def obtain(app_or_request: FastAPI | Request) -> BoundLogger:
"""Get a logger from request or app state."""
if isinstance(app_or_request, Request):
if hasattr(app_or_request.state, "logger_dep"):
logger = app_or_request.state.logger_dep
if isinstance(logger, BoundLogger):
return logger
app_or_request = app_or_request.app
if not hasattr(app_or_request.state, "logger_dep"):
def from_app(app: FastAPI) -> BoundLogger:
"""Get a logger from app state."""
if not hasattr(app.state, "logger_dep"):
raise ValueError("BoundLogger dispencer was not initialized at app preparation")
return app_or_request.state.logger_dep
return app.state.logger_dep
def from_request(request: Request) -> BoundLogger:
"""Get a logger from request or app state."""
if hasattr(request.state, "logger_dep"):
logger = request.state.logger_dep
if isinstance(logger, BoundLogger):
return logger
return from_app(request.app)