Struct prometheus_http_query::Client [−][src]
pub struct Client { /* fields omitted */ }
Expand description
A client used to execute queries. It uses a reqwest::Client internally that manages connections for us.
Implementations
Create a Client that connects to a Prometheus instance at the given FQDN/domain and port, using either HTTP or HTTPS.
Note that possible errors regarding domain name resolution or connection establishment will only be propagated from the underlying reqwest::Client when a query is executed.
use prometheus_http_query::{Client, Scheme};
let client = Client::new(Scheme::Http, "localhost", 9090);
Perform an instant query using a crate::RangeVector or crate::InstantVector.
use prometheus_http_query::{Client, Scheme, InstantVector, Selector, Aggregate, Error};
use prometheus_http_query::aggregations::sum;
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let v: InstantVector = Selector::new()
.metric("node_cpu_seconds_total")?
.try_into()?;
let s = sum(v, Some(Aggregate::By(&["cpu"])));
let response = tokio_test::block_on( async { client.query(s, None, None).await.unwrap() });
assert!(response.as_instant().is_some());
Ok(())
}
pub async fn query_range(
&self,
vector: impl Display,
start: i64,
end: i64,
step: &str,
timeout: Option<&str>
) -> Result<QueryResultType, Error>
Find time series that match certain label sets (Selectors).
use prometheus_http_query::{Client, Scheme, Selector, Error};
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let s1 = Selector::new()
.with("handler", "/api/v1/query");
let s2 = Selector::new()
.with("job", "node")
.regex_match("mode", ".+");
let set = vec![s1, s2];
let response = tokio_test::block_on( async { client.series(&set, None, None).await });
assert!(response.is_ok());
Ok(())
}
Retrieve all label names (or use Selectors to select time series to read label names from).
use prometheus_http_query::{Client, Scheme, Selector, Error};
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
// To retrieve a list of all labels:
let response = tokio_test::block_on( async { client.label_names(None, None, None).await });
assert!(response.is_ok());
// To retrieve a list of labels that appear in specific time series, use Selectors:
let s1 = Selector::new()
.with("handler", "/api/v1/query");
let s2 = Selector::new()
.with("job", "node")
.regex_match("mode", ".+");
let set = Some(vec![s1, s2]);
let response = tokio_test::block_on( async { client.label_names(set, None, None).await });
assert!(response.is_ok());
Ok(())
}
Retrieve all label values for a label name (or use Selectors to select the time series to read label values from)
use prometheus_http_query::{Client, Scheme, Selector, Error};
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
// To retrieve a list of all label values for a specific label name:
let response = tokio_test::block_on( async { client.label_values("job", None, None, None).await });
assert!(response.is_ok());
// To retrieve a list of label values of labels in specific time series instead:
let s1 = Selector::new()
.regex_match("instance", ".+");
let set = Some(vec![s1]);
let response = tokio_test::block_on( async { client.label_values("job", set, None, None).await });
assert!(response.is_ok());
Ok(())
}
Query the current state of target discovery.
use prometheus_http_query::{Client, Scheme, Error, TargetState};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let response = tokio_test::block_on( async { client.targets(None).await });
assert!(response.is_ok());
// Filter targets by type:
let response = tokio_test::block_on( async { client.targets(Some(TargetState::Active)).await });
assert!(response.is_ok());
Ok(())
}
Retrieve a list of rule groups of recording and alerting rules.
use prometheus_http_query::{Client, Scheme, Error, RuleType};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let response = tokio_test::block_on( async { client.rules(None).await });
assert!(response.is_ok());
// Filter rules by type:
let response = tokio_test::block_on( async { client.rules(Some(RuleType::Alert)).await });
assert!(response.is_ok());
Ok(())
}
Retrieve a list of active alerts.
use prometheus_http_query::{Client, Scheme, Error};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let response = tokio_test::block_on( async { client.alerts().await });
assert!(response.is_ok());
Ok(())
}
Retrieve a list of flags that Prometheus was configured with.
use prometheus_http_query::{Client, Scheme, Error};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let response = tokio_test::block_on( async { client.flags().await });
assert!(response.is_ok());
Ok(())
}
Query the current state of alertmanager discovery.
use prometheus_http_query::{Client, Scheme, Error, TargetState};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
let response = tokio_test::block_on( async { client.alertmanagers().await });
assert!(response.is_ok());
Ok(())
}
Retrieve metadata about metrics that are currently scraped from targets, along with target information.
use prometheus_http_query::{Client, Scheme, Error, Selector};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
// Retrieve metadata for a specific metric from all targets.
let response = tokio_test::block_on( async { client.target_metadata(Some("go_routines"), None, None).await });
assert!(response.is_ok());
// Retrieve metric metadata from specific targets.
let s = Selector::new().with("job", "prometheus");
let response = tokio_test::block_on( async { client.target_metadata(None, Some(&s), None).await });
assert!(response.is_ok());
// Retrieve metadata for a specific metric from targets that match a specific label set.
let s = Selector::new().with("job", "node");
let response = tokio_test::block_on( async { client.target_metadata(Some("node_cpu_seconds_total"), Some(&s), None).await });
assert!(response.is_ok());
Ok(())
}
Retrieve metadata about metrics that are currently scraped from targets.
use prometheus_http_query::{Client, Scheme, Error};
use std::convert::TryInto;
fn main() -> Result<(), Error> {
let client = Client::new(Scheme::Http, "localhost", 9090);
// Retrieve metadata for a all metrics.
let response = tokio_test::block_on( async { client.metric_metadata(None, None).await });
assert!(response.is_ok());
// Limit the number of returned metrics
let response = tokio_test::block_on( async { client.metric_metadata(None, Some(10)).await });
assert!(response.is_ok());
// Retrieve metadata of a specific metric.
let response = tokio_test::block_on( async { client.metric_metadata(Some("go_routines"), None).await });
assert!(response.is_ok());
Ok(())
}
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Client
impl !UnwindSafe for Client
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more