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.
Overviewο
Classes
MetricType
Classesο
Represents a single metric with its metadata and values.
- apifrom.monitoring.metrics.__post_init__()ο
Initialize the metric based on its type.
- apifrom.monitoring.metrics.get_histogram_buckets()ο
Get histogram bucket counts.
- apifrom.monitoring.metrics.get_stats()ο
Get basic statistics for histogram and summary metrics.
- apifrom.monitoring.metrics.get_summary_quantiles()ο
Get summary quantiles.
- apifrom.monitoring.metrics.increment(amount=1.0, labels=None)ο
Increment a counter metric.
- apifrom.monitoring.metrics.observe(value, labels=None)ο
Add an observation to a histogram or summary metric.
- apifrom.monitoring.metrics.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.metrics.create_counter(name, description, labels=None)ο
Create a new counter metric.
- apifrom.monitoring.metrics.create_gauge(name, description, labels=None)ο
Create a new gauge metric.
- apifrom.monitoring.metrics.create_histogram(name, description, buckets=None, labels=None)ο
Create a new histogram metric.
- apifrom.monitoring.metrics.create_metric(metric)ο
Register a new metric with the collector.
- param metric:
The metric to register
- returns:
The registered metric
- apifrom.monitoring.metrics.create_summary(name, description, quantiles=None, labels=None)ο
Create a new summary metric.
- apifrom.monitoring.metrics.get_all_metrics()ο
Get all metrics.
- apifrom.monitoring.metrics.get_metric(name)ο
Get a metric by name.
- apifrom.monitoring.metrics.increment(name, amount=1.0, labels=None)ο
Increment a counter metric.
- apifrom.monitoring.metrics.observe(name, value, labels=None)ο
Add an observation to a histogram or summary metric.
- apifrom.monitoring.metrics.reset()ο
Reset all metrics.
- apifrom.monitoring.metrics.set(name, value, labels=None)ο
Set a gauge metric to a specific value.
- apifrom.monitoring.metrics.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.metrics.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.metrics.track_error(error_type, endpoint=None)ο
Track an API error.
- apifrom.monitoring.metrics.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.metrics.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