apifrom.monitoring.middleware ============================= Middleware for metrics collection in APIFromAnything. This module provides middleware components for collecting metrics during API request processing. .. py:currentmodule:: apifrom.monitoring.middleware Overview -------- **Classes** * :py:class:`DjangoMetricsMiddleware` * :py:class:`FastAPIMetricsMiddleware` * :py:class:`FlaskMetricsMiddleware` * :py:class:`MetricsCollector` * :py:class:`MetricsMiddleware` Classes ------- .. py:class:: DjangoMetricsMiddleware(collector = None) Metrics middleware for Django applications. Initialize the Django metrics middleware. :param collector: Metrics collector to use for tracking metrics. .. :: collector .. method:: __call__(get_response) Process a request and response in a Django application. :param get_response: The next middleware or view in the chain. .. py:class:: FastAPIMetricsMiddleware(collector = None):bases: MetricsMiddleware Metrics middleware for FastAPI applications. Initialize the metrics middleware. :param collector: Metrics collector to use for tracking metrics. .. method:: register(app) Register the middleware with a FastAPI application. :param app: The FastAPI application to register with. .. py:class:: FlaskMetricsMiddleware(collector = None):bases: MetricsMiddleware Metrics middleware for Flask applications. Initialize the metrics middleware. :param collector: Metrics collector to use for tracking metrics. .. method:: register(app) Register the middleware with a Flask application. :param app: The Flask application to register with. .. py:class:: MetricsCollector Collects and manages metrics for the API. This class provides a centralized way to create, update, and retrieve metrics for monitoring API performance and usage. Initialize the metrics collector. .. py:class:: MetricsMiddleware(collector = None) Middleware for collecting metrics during API request processing. This middleware tracks request counts, durations, and error rates for each endpoint in the API. Initialize the metrics middleware. :param collector: Metrics collector to use for tracking metrics. .. :: _request_timers .. :: collector .. method:: on_error(request, error, endpoint) :async: Process an error that occurred during request handling. :param request: The request object. :param error: The exception that was raised. :param endpoint: The endpoint being called. .. method:: post_request(request, response) :async: Process a response after it is generated by the endpoint. :param request: The request object. :param response: The response object. .. method:: pre_request(request, endpoint) :async: Process a request before it is handled by the endpoint. :param request: The request object. :param endpoint: The endpoint being called. .. method:: register(app) :abstractmethod: Register the middleware with the application. :param app: The application to register with.