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

Classes

class apifrom.monitoring.metrics.Metric[source]

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.

class apifrom.monitoring.metrics.MetricsCollector[source]

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