apifrom.monitoringο
Monitoring and metrics collection module for APIFromAnything.
This module provides tools for monitoring API performance, collecting metrics, and integrating with popular monitoring systems like Prometheus, Grafana, and others.
Overviewο
Classes
JSONExporterLogExporterMetricTypePrometheusExporter
Classesο
- JSONExporter(collector = None):bases: MetricsExporter
Exports metrics data in JSON format.
Initialize the exporter with a metrics collector.
- apifrom.monitoring.export(pretty=False)ο
Export metrics as JSON.
- param pretty:
Whether to format the JSON with indentation for readability.
- returns:
JSON string representation of the metrics.
- apifrom.monitoring.export_to_file(file_path, pretty=True)ο
Export metrics to a JSON file.
- param file_path:
Path to the output file.
- param pretty:
Whether to format the JSON with indentation for readability.
- LogExporter(collector = None, logger = None, level = logging.INFO):bases: MetricsExporter
Exports metrics to Pythonβs logging system.
Initialize the log exporter.
- param collector:
Metrics collector to export from.
- param logger:
Logger to use for logging metrics.
- param level:
Logging level to use.
- apifrom.monitoring.export(include_details=False, **kwargs)ο
Export metrics to logs.
- param include_details:
Whether to include detailed metrics information.
- apifrom.monitoring.export_periodic(interval_seconds=60, include_details=False)ο
Start a background thread to periodically export metrics to logs.
- param interval_seconds:
Interval between exports in seconds.
- param include_details:
Whether to include detailed metrics information.
Represents a single metric with its metadata and values.
- apifrom.monitoring.__post_init__()ο
Initialize the metric based on its type.
- apifrom.monitoring.get_histogram_buckets()ο
Get histogram bucket counts.
- apifrom.monitoring.get_stats()ο
Get basic statistics for histogram and summary metrics.
- apifrom.monitoring.get_summary_quantiles()ο
Get summary quantiles.
- apifrom.monitoring.increment(amount=1.0, labels=None)ο
Increment a counter metric.
- apifrom.monitoring.observe(value, labels=None)ο
Add an observation to a histogram or summary metric.
- apifrom.monitoring.set(value, labels=None)ο
Set a gauge metric to a specific value.
- MetricType:bases: enum.Enum
Types of metrics that can be collected.
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.
- apifrom.monitoring.create_counter(name, description, labels=None)ο
Create a new counter metric.
- apifrom.monitoring.create_gauge(name, description, labels=None)ο
Create a new gauge metric.
- apifrom.monitoring.create_histogram(name, description, buckets=None, labels=None)ο
Create a new histogram metric.
- apifrom.monitoring.create_metric(metric)ο
Register a new metric with the collector.
- param metric:
The metric to register
- returns:
The registered metric
- apifrom.monitoring.create_summary(name, description, quantiles=None, labels=None)ο
Create a new summary metric.
- apifrom.monitoring.get_all_metrics()ο
Get all metrics.
- apifrom.monitoring.get_metric(name)ο
Get a metric by name.
- apifrom.monitoring.increment(name, amount=1.0, labels=None)ο
Increment a counter metric.
- apifrom.monitoring.observe(name, value, labels=None)ο
Add an observation to a histogram or summary metric.
- apifrom.monitoring.reset()ο
Reset all metrics.
- apifrom.monitoring.set(name, value, labels=None)ο
Set a gauge metric to a specific value.
- apifrom.monitoring.start_timer(name)ο
Start a timer for measuring durations.
- param name:
The name of the metric to record the duration
- returns:
A unique timer ID
- apifrom.monitoring.stop_timer(timer_id, labels=None)ο
Stop a timer and return the duration in milliseconds.
- param timer_id:
The timer ID
- param labels:
Optional labels to attach to the observation
- returns:
The duration in milliseconds
- apifrom.monitoring.track_error(error_type, endpoint=None)ο
Track an API error.
- apifrom.monitoring.track_request(endpoint)ο
Track the start of an API request.
- param endpoint:
The API endpoint being called
- returns:
A timer ID for tracking the request duration
- apifrom.monitoring.track_request_end(timer_id, endpoint, status_code)ο
Track the end of an API request.
- param timer_id:
The timer ID returned by track_request
- param endpoint:
The API endpoint that was called
- param status_code:
The HTTP status code of the response
- returns:
The request duration in milliseconds
- class apifrom.monitoring.MetricsMiddleware(collector=None)[source]ο
- Parameters:
collector (Optional[apifrom.monitoring.metrics.MetricsCollector])
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.
- apifrom.monitoring.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.
- apifrom.monitoring.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.
- apifrom.monitoring.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.
- apifrom.monitoring.register(app)ο
- :abstractmethod:
Register the middleware with the application.
- param app:
The application to register with.
- PrometheusExporter(collector = None):bases: MetricsExporter
Exports metrics in Prometheus text format.
Initialize the exporter with a metrics collector.
- apifrom.monitoring._format_labels(labels)ο
Format labels for Prometheus exposition format.
- apifrom.monitoring.export(**kwargs)ο
Export metrics in Prometheus text format.
- returns:
String in Prometheus exposition format.
- apifrom.monitoring.export_to_file(file_path)ο
Export metrics to a file in Prometheus text format.
- param file_path:
Path to the output file.
- apifrom.monitoring.serve_metrics(host='localhost', port=8000, endpoint='/metrics')ο
Start a simple HTTP server to expose metrics for Prometheus scraping.
- param host:
Host to bind the server to.
- param port:
Port to listen on.
- param endpoint:
URL endpoint for metrics.