API Reference
This section provides detailed documentation for the aiopromql API.
Clients
- class aiopromql.client.PrometheusAsync(url, timeout=2.0)[source]
Bases:
PrometheusClientBaseAsynchronous Prometheus client using httpx.
- async query(promql, raw=False)[source]
Run an instant PromQL query.
- Parameters:
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
- async query_range(promql, start, end, step='30s', raw=False)[source]
Run a ranged PromQL query over a time window.
- Parameters:
promql (str) – The PromQL query string to execute.
start (datetime.datetime) – Start datetime of the query range.
end (datetime.datetime) – End datetime of the query range.
step (str) – Query resolution step width (e.g., ’30s’, ‘1m’).
raw (bool) – If True, return raw JSON response as dict; otherwise parse into model.
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
- class aiopromql.client.PrometheusClientBase(url)[source]
Bases:
objectBase Prometheus client with common utilities.
- class aiopromql.client.PrometheusSync(url, timeout=2.0)[source]
Bases:
PrometheusClientBaseSynchronous Prometheus client using httpx.
- query(promql, raw=False)[source]
Run an instant PromQL query.
- Parameters:
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
- query_range(promql, start, end, step='30s', raw=False)[source]
Run a ranged PromQL query over a time window.
- Parameters:
promql (str) – The PromQL query string to execute.
start (datetime.datetime) – Start datetime of the query range.
end (datetime.datetime) – End datetime of the query range.
step (str) – Query resolution step width (e.g., ’30s’, ‘1m’).
raw (bool) – If True, return raw JSON response as dict; otherwise parse into model.
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
Models
Core Models
Generic data structures for modeling time series and labeled metrics.
- class aiopromql.models.core.MetricLabelSet(metric)[source]
Bases:
objectHashable wrapper around a Prometheus metric label dictionary.
Prometheus metrics are identified by a set of key-value labels (e.g., {“job”: “api”, “instance”: “localhost:9090”}). This class allows such a label set to be used as a key in Python dictionaries by making it hashable and comparable.
Instances of this class are used as keys in the dictionary returned by PrometheusResponseModel.to_metric_map(), where each MetricLabelSet maps to a TimeSeries object.
- class aiopromql.models.core.TimeSeries(values)[source]
Bases:
objectA sequence of timestamped float values (TimeSeriesPoint) with utility methods.
This class abstracts a Prometheus time series and provides methods for inspection, aggregation, and manipulation. Used in PrometheusResponseModel.to_metric_map() where each MetricLabelSet maps to a TimeSeries.
- __init__(values)[source]
- Parameters:
values (
List[TimeSeriesPoint]) – List of initial TimeSeriesPoint objects.
- class aiopromql.models.core.TimeSeriesPoint(timestamp: datetime, value: float)[source]
Bases:
NamedTupleA single timestamped data point from a Prometheus time series.
Represents one (timestamp, value) pair, where the timestamp is a datetime object and the value is a float. Useful for building time series from Prometheus query results.
Prometheus Models
Pydantic models for parsing and transforming Prometheus query json responses.
- class aiopromql.models.prometheus.MatrixDataModel(**data)[source]
Bases:
BaseModelParsed matrix data block from Prometheus.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
-
result:
List[MatrixResultModel]
- class aiopromql.models.prometheus.MatrixResultModel(**data)[source]
Bases:
BaseModelSingle Prometheus matrix result entry.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class aiopromql.models.prometheus.PrometheusResponseModel(**data)[source]
Bases:
BaseModelTop-level Prometheus query response wrapper.
-
data:
Union[VectorDataModel,MatrixDataModel]
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- to_metric_map()[source]
Converts the response into a mapping from metric label sets to time series.
The resulting data structure uses generic, reusable types: - MetricLabelSet: a hashable representation of metric labels. - TimeSeries: a sequence of timestamped values.
- Return type:
- Returns:
A dictionary mapping MetricLabelSet to TimeSeries.
-
data:
- class aiopromql.models.prometheus.VectorDataModel(**data)[source]
Bases:
BaseModelParsed vector data block from Prometheus.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
-
result:
List[VectorResultModel]
Package
- class aiopromql.PrometheusAsync(url, timeout=2.0)[source]
Bases:
PrometheusClientBaseAsynchronous Prometheus client using httpx.
- async query(promql, raw=False)[source]
Run an instant PromQL query.
- Parameters:
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
- async query_range(promql, start, end, step='30s', raw=False)[source]
Run a ranged PromQL query over a time window.
- Parameters:
promql (str) – The PromQL query string to execute.
start (datetime.datetime) – Start datetime of the query range.
end (datetime.datetime) – End datetime of the query range.
step (str) – Query resolution step width (e.g., ’30s’, ‘1m’).
raw (bool) – If True, return raw JSON response as dict; otherwise parse into model.
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
- class aiopromql.PrometheusSync(url, timeout=2.0)[source]
Bases:
PrometheusClientBaseSynchronous Prometheus client using httpx.
- query(promql, raw=False)[source]
Run an instant PromQL query.
- Parameters:
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.
- query_range(promql, start, end, step='30s', raw=False)[source]
Run a ranged PromQL query over a time window.
- Parameters:
promql (str) – The PromQL query string to execute.
start (datetime.datetime) – Start datetime of the query range.
end (datetime.datetime) – End datetime of the query range.
step (str) – Query resolution step width (e.g., ’30s’, ‘1m’).
raw (bool) – If True, return raw JSON response as dict; otherwise parse into model.
- Return type:
- Returns:
Parsed Prometheus response or raw JSON dict.