A rust library that provides an interface to query a Prometheus server and offers the tools to gradually build queries before actually sending them by leveraging Rust's type system. Several kinds of metadata queries are also supported (e.g. retrieving time series, rules, alerts etc.). Check docs.rs for detailed information.
```rust use prometheushttpquery::{Aggregate, Client, Error, InstantVector, RangeVector, Selector}; use prometheushttpquery::aggregations::{sum, topk}; use prometheushttpquery::functions::rate; use std::convert::TryInto;
async fn main() -> Result<(), Error> { let client = Client::default();
// Construct an instant vector, execute a query, interpret the result
// again as an instant vector.
let vector: InstantVector = Selector::new()
.metric("prometheus_http_requests_total")
.try_into()?;
let q = topk(vector, Some(Aggregate::By(&["code"])), 5);
let response = client.query(q, None, None).await?;
assert!(response.as_instant().is_some());
// Construct a range vector, execute a query, interpret the result
// as an instant vector.
let vector: RangeVector = Selector::new()
.metric("node_cpu_seconds_total")?
.with("mode", "user")
.range("5m")?
.try_into()?;
let q = sum(rate(vector), Some(Aggregate::By(&["cpu"])));
let response = client.query(q, None, None).await?;
assert!(response.as_instant().is_some());
// It is also possible to bypass every kind of validation by supplying
// a custom query directly to the InstantVector | RangeVector types.
// The equivalent of the operation above would be:
let q = r#"sum by(cpu) (rate(node_cpu_seconds_total{mode="user"}[5m]))"#;
let v = RangeVector(q.to_string());
let response = client.query(v, None, None).await?;
assert!(response.as_instant().is_some());
Ok(())
} ```
The table below depicts the compatibility of this library with various versions of the Prometheus server.
| Features | v2.34 | v2.33 | v2.32 | v2.31 | v2.30 | v2.29 | |---|---|---|---|---|---|---| | Instant queries | yes | yes | yes | yes | yes | yes | | Range queries | yes | yes | yes | yes | yes | yes | | Time series metadata | yes | yes | yes | yes | yes | yes | | Label name metadata | yes | yes | yes | yes | yes | yes | | Label value metadata | yes | yes | yes | yes | yes | yes | | Targets | yes | yes | yes | yes | yes | yes | | Rules | yes | yes | yes | yes | yes | yes | | Alerts | yes | yes | yes | yes | yes | yes | | Alertmanagers | yes | yes | yes | yes | yes | yes | | Runtime flags | yes | yes | yes | yes | yes | yes | | Metric metadata | yes | yes | yes | yes | yes | yes | | Target metadata | yes | yes | yes | yes | yes | no | | Trigonometric functions | yes | yes | yes | yes | no | no |
Server versions below v2.29 are untested.
In order to run all tests a Prometheus server must be running at http://localhost:9090
. No special configuration is required at this point.
Run: RUSTFLAGS="--cfg unsound_local_offset" cargo test
Please do not hesitate to file issues in order to report bugs, ask questions or make suggestions. You are also welcome to tackle open issues if there are any.
If you are looking to submit code, please make sure that the tests pass successfully.