lomas_server.routes package
Submodules
lomas_server.routes.middlewares module
- class lomas_server.routes.middlewares.FastAPIMetricMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], app_name: str = 'lomas-server-app')[source]
Bases:
BaseHTTPMiddlewareMiddleware to collect and expose Prometheus metrics for a FastAPI application.
This middleware tracks various metrics related to HTTP requests, including: - Total requests (fastapi_requests_total) - Total responses (fastapi_responses_total) - Exceptions raised (fastapi_exceptions_total) - Request processing duration (fastapi_requests_duration_seconds) - Current requests in progress (fastapi_requests_in_progress)
It also supports integration with an OpenTelemetry exporter for exporting metrics to a metrics collector (e.g., Prometheus or any other OTLP-compatible collector).
- async dispatch(request: Request, call_next: Callable[[Request], Awaitable[Response]]) Response[source]
Processes HTTP request, records metrics and returns the HTTP response.
This method performs the following steps: 1. Tracks the current request in progress using the
fastapi_requests_in_progress gauge.
Records the request count with the fastapi_requests_total counter.
- Records the time taken to process the request using the
fastapi_requests_duration_seconds histogram.
- Handles exceptions, if raised, and records the exception details using the
fastapi_exceptions_total counter.
Records the response status code with the fastapi_responses_total counter.
Decrements the in-progress request gauge after processing.
- Parameters:
request (Request) – The incoming HTTP request to be processed.
call_next (RequestResponseEndpoint) – The endpoint function that processes the request and returns a response.
- Returns:
The HTTP response after processing the request.
- Return type:
Response
- Raises:
BaseException – If an exception occurs during request processing, it is raised after logging it.
- static get_path(request: Request) Tuple[str, bool][source]
Attempts to match the request’ route to a defined route.
- Parameters:
request (Request) – The HTTP request to check for a matching path.
- Returns:
- A tuple containing:
The matched path (str) from the request URL.
Boolean (True if the path was handled by one of the routes).
- Return type:
Tuple[str, bool]
- class lomas_server.routes.middlewares.LoggingAndTracingMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], dispatch: Callable[[Request, Callable[[Request], Awaitable[Response]]], Awaitable[Response]] | None = None)[source]
Bases:
BaseHTTPMiddlewareMiddleware for logging and tracing incoming HTTP requests.
This middleware logs the incoming requests, including the user name the route being accessed, and any query parameters. Additionally, it creates a trace span to trace the user’s request and adds attributes to the span related to the user name and query parameters.
- async dispatch(request: Request, call_next)[source]
Handles the request and performs logging and tracing.
Logs the user name, the route and the query parameters. Creates a trace span to monitor the request and adds relevant attributes.
- Parameters:
request (Request) – The incoming request object.
call_next (Callable) – A function that, when called, passes the request to the next middleware or request handler.
- Returns:
The HTTP response generated by calling call_next(request).
- Return type:
Response