apifrom.monitoring.exporters ============================ Exporters for metrics data in APIFromAnything. This module provides exporters for converting metrics data to various formats and integrating with popular monitoring systems like Prometheus, Grafana, etc. .. py:currentmodule:: apifrom.monitoring.exporters Overview -------- **Classes** * :py:class:`JSONExporter` * :py:class:`LogExporter` * :py:class:`Metric` * :py:class:`MetricType` * :py:class:`MetricsCollector` * :py:class:`MetricsExporter` * :py:class:`PrometheusExporter` Classes ------- .. py:class:: JSONExporter(collector = None):bases: MetricsExporter Exports metrics data in JSON format. Initialize the exporter with a metrics collector. .. method:: 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. .. method:: 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. .. py:class:: 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. .. :: level .. :: logger .. method:: export(include_details = False, **kwargs) Export metrics to logs. :param include_details: Whether to include detailed metrics information. .. method:: 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. .. py:class:: Metric Represents a single metric with its metadata and values. .. py:class:: MetricType:bases: enum.Enum Types of metrics that can be collected. .. 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:: MetricsExporter(collector = None):bases: abc.ABC Base class for all metrics exporters. Initialize the exporter with a metrics collector. .. :: collector .. method:: export(**kwargs) :abstractmethod: Export metrics in the specific format. .. py:class:: PrometheusExporter(collector = None):bases: MetricsExporter Exports metrics in Prometheus text format. Initialize the exporter with a metrics collector. .. method:: _format_labels(labels) Format labels for Prometheus exposition format. .. method:: export(**kwargs) Export metrics in Prometheus text format. :returns: String in Prometheus exposition format. .. method:: export_to_file(file_path) Export metrics to a file in Prometheus text format. :param file_path: Path to the output file. .. method:: 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.