apifrom.monitoring.metrics ========================== Metrics collection and management for APIFromAnything. This module provides classes and utilities for collecting, managing, and tracking various metrics related to API performance and usage. .. py:currentmodule:: apifrom.monitoring.metrics Overview -------- **Classes** * :py:class:`Metric` * :py:class:`MetricType` * :py:class:`MetricsCollector` Classes ------- .. py:class:: Metric Represents a single metric with its metadata and values. .. :: buckets :annotation: List[float] .. :: description :annotation: str .. :: labels :annotation: Dict[str, str] .. :: name :annotation: str .. :: quantiles :annotation: List[float] .. :: type :annotation: MetricType .. :: value :annotation: Union[float, int, List[float]] .. method:: __post_init__() Initialize the metric based on its type. .. method:: get_histogram_buckets() Get histogram bucket counts. .. method:: get_stats() Get basic statistics for histogram and summary metrics. .. method:: get_summary_quantiles() Get summary quantiles. .. method:: increment(amount = 1.0, labels = None) Increment a counter metric. .. method:: observe(value, labels = None) Add an observation to a histogram or summary metric. .. method:: set(value, labels = None) Set a gauge metric to a specific value. .. py:class:: MetricType:bases: enum.Enum Types of metrics that can be collected. .. :: COUNTER .. :: GAUGE .. :: HISTOGRAM .. :: SUMMARY .. 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. .. :: _initialized .. :: _instance .. :: _metrics .. :: _timers .. method:: create_counter(name, description, labels = None) Create a new counter metric. .. method:: create_gauge(name, description, labels = None) Create a new gauge metric. .. method:: create_histogram(name, description, buckets = None, labels = None) Create a new histogram metric. .. method:: create_metric(metric) Register a new metric with the collector. :param metric: The metric to register :returns: The registered metric .. method:: create_summary(name, description, quantiles = None, labels = None) Create a new summary metric. .. method:: get_all_metrics() Get all metrics. .. method:: get_metric(name) Get a metric by name. .. method:: increment(name, amount = 1.0, labels = None) Increment a counter metric. .. method:: observe(name, value, labels = None) Add an observation to a histogram or summary metric. .. method:: reset() Reset all metrics. .. method:: set(name, value, labels = None) Set a gauge metric to a specific value. .. method:: 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 .. method:: 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 .. method:: track_error(error_type, endpoint = None) Track an API error. .. method:: 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 .. method:: 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