Struct prometheus_http_query::Client
source · pub struct Client { /* private fields */ }
Expand description
A client used to execute queries. It uses a reqwest::Client
internally
that manages connections for us.
Implementations§
source§impl Client
impl Client
sourcepub fn inner(&self) -> &Client
pub fn inner(&self) -> &Client
Return a reference to the wrapped reqwest::Client
, i.e. to
use it for other requests unrelated to the Prometheus API.
use prometheus_http_query::{Client};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
// An amittedly bad example, but that is not the point.
let response = client
.inner()
.head("http://127.0.0.1:9090")
.send()
.await?;
// Prometheus does not allow HEAD requests.
assert_eq!(response.status(), reqwest::StatusCode::METHOD_NOT_ALLOWED);
Ok(())
}
sourcepub fn base_url(&self) -> &Url
pub fn base_url(&self) -> &Url
Return a reference to the base URL that is used in requests to the Prometheus API.
use prometheus_http_query::Client;
use std::str::FromStr;
let client = Client::default();
assert_eq!(client.base_url().as_str(), "http://127.0.0.1:9090/");
let client = Client::from_str("https://proxy.example.com:8443/prometheus").unwrap();
assert_eq!(client.base_url().as_str(), "https://proxy.example.com:8443/prometheus");
sourcepub fn from(client: Client, url: &str) -> Result<Self, Error>
pub fn from(client: Client, url: &str) -> Result<Self, Error>
Create a Client from a custom reqwest::Client
and URL.
This way you can account for all extra parameters (e.g. x509 authentication)
that may be needed to connect to Prometheus or an intermediate proxy,
by building it into the reqwest::Client
.
use prometheus_http_query::Client;
fn main() -> Result<(), anyhow::Error> {
let client = {
let c = reqwest::Client::builder()
.no_proxy()
.build()?;
Client::from(c, "https://prometheus.example.com")
};
assert!(client.is_ok());
Ok(())
}
sourcepub fn query(&self, query: impl Display) -> InstantQueryBuilder
pub fn query(&self, query: impl Display) -> InstantQueryBuilder
Create an InstantQueryBuilder
from a PromQL query allowing you to set some query parameters
(e.g. evaluation timeout) before finally sending the instant query to the server.
§Arguments
query
- PromQL query to exeute
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.query("prometheus_http_request_total").get().await?;
assert!(response.data().as_vector().is_some());
// Or make a POST request.
let response = client.query("prometheus_http_request_total").post().await?;
assert!(response.data().as_vector().is_some());
Ok(())
}
sourcepub fn query_range(
&self,
query: impl Display,
start: i64,
end: i64,
step: f64
) -> RangeQueryBuilder
pub fn query_range( &self, query: impl Display, start: i64, end: i64, step: f64 ) -> RangeQueryBuilder
Create a RangeQueryBuilder
from a PromQL query allowing you to set some query parameters
(e.g. evaluation timeout) before finally sending the range query to the server.
§Arguments
query
- PromQL query to exeutestart
- Start timestamp as Unix timestamp (seconds)end
- End timestamp as Unix timestamp (seconds)step
- Query resolution step width as float number of seconds
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let q = "prometheus_http_requests_total";
let response = client.query_range(q, 1648373100, 1648373300, 10.0).get().await?;
assert!(response.data().as_matrix().is_some());
// Or make a POST request.
let response = client.query_range(q, 1648373100, 1648373300, 10.0).post().await?;
assert!(response.data().as_matrix().is_some());
Ok(())
}
sourcepub fn series<'a, T>(&self, selectors: T) -> Result<SeriesQueryBuilder, Error>
pub fn series<'a, T>(&self, selectors: T) -> Result<SeriesQueryBuilder, Error>
Create a SeriesQueryBuilder
to apply filters to a series metadata
query before sending it to Prometheus.
§Arguments
selectors
- Iterable container ofSelector
s that tells Prometheus which series to return. Must not be empty!
See also: Prometheus API documentation
use prometheus_http_query::{Client, Selector};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let s1 = Selector::new()
.eq("handler", "/api/v1/query");
let s2 = Selector::new()
.eq("job", "node")
.regex_eq("mode", ".+");
let response = client.series(&[s1, s2])?.get().await;
assert!(response.is_ok());
Ok(())
}
sourcepub fn label_names(&self) -> LabelNamesQueryBuilder
pub fn label_names(&self) -> LabelNamesQueryBuilder
Create a LabelNamesQueryBuilder
to apply filters to a query for the label
names endpoint before sending it to Prometheus.
See also: Prometheus API documentation
use prometheus_http_query::{Client, Selector};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
// To retrieve a list of all labels:
let response = client.label_names().get().await;
assert!(response.is_ok());
// Use a selector to retrieve a list of labels that appear in specific time series:
let s1 = Selector::new()
.eq("handler", "/api/v1/query");
let s2 = Selector::new()
.eq("job", "node")
.regex_eq("mode", ".+");
let response = client.label_names().selectors(&[s1, s2]).get().await;
assert!(response.is_ok());
Ok(())
}
sourcepub fn label_values(&self, label: impl Display) -> LabelValuesQueryBuilder
pub fn label_values(&self, label: impl Display) -> LabelValuesQueryBuilder
Create a LabelValuesQueryBuilder
to apply filters to a query for the label
values endpoint before sending it to Prometheus.
§Arguments
label
- Name of the label to return all occuring label values for.
See also: Prometheus API documentation
use prometheus_http_query::{Client, Selector};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
// To retrieve a list of all label values for a specific label name:
let response = client.label_values("job").get().await;
assert!(response.is_ok());
// To retrieve a list of label values of labels that appear in specific time series:
let s1 = Selector::new()
.regex_eq("instance", ".+");
let response = client.label_values("job").selectors(&[s1]).get().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn targets(
&self,
state: Option<TargetState>
) -> Result<Targets, Error>
pub async fn targets( &self, state: Option<TargetState> ) -> Result<Targets, Error>
Query the current state of target discovery.
See also: Prometheus API documentation
use prometheus_http_query::{Client, TargetState};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.targets(None).await;
assert!(response.is_ok());
// Filter targets by type:
let response = client.targets(Some(TargetState::Active)).await;
assert!(response.is_ok());
Ok(())
}
sourcepub fn rules(&self) -> RulesQueryBuilder
pub fn rules(&self) -> RulesQueryBuilder
Create a RulesQueryBuilder
to apply filters to the rules query before
sending it to Prometheus.
See also: Prometheus API documentation
use prometheus_http_query::{Client, RuleKind};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.rules().get().await;
assert!(response.is_ok());
// Filter rules by type:
let response = client.rules().kind(RuleKind::Alerting).get().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn alerts(&self) -> Result<Vec<Alert>, Error>
pub async fn alerts(&self) -> Result<Vec<Alert>, Error>
Retrieve a list of active alerts.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.alerts().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn flags(&self) -> Result<HashMap<String, String>, Error>
pub async fn flags(&self) -> Result<HashMap<String, String>, Error>
Retrieve a list of flags that Prometheus was configured with.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.flags().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn build_information(&self) -> Result<BuildInformation, Error>
pub async fn build_information(&self) -> Result<BuildInformation, Error>
Retrieve Prometheus server build information.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.build_information().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn runtime_information(&self) -> Result<RuntimeInformation, Error>
pub async fn runtime_information(&self) -> Result<RuntimeInformation, Error>
Retrieve Prometheus server runtime information.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.runtime_information().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn tsdb_statistics(&self) -> Result<TsdbStatistics, Error>
pub async fn tsdb_statistics(&self) -> Result<TsdbStatistics, Error>
Retrieve Prometheus TSDB statistics.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.tsdb_statistics().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn wal_replay_statistics(&self) -> Result<WalReplayStatistics, Error>
pub async fn wal_replay_statistics(&self) -> Result<WalReplayStatistics, Error>
Retrieve WAL replay statistics.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.wal_replay_statistics().await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn alertmanagers(&self) -> Result<Alertmanagers, Error>
pub async fn alertmanagers(&self) -> Result<Alertmanagers, Error>
Query the current state of alertmanager discovery.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
let response = client.alertmanagers().await;
assert!(response.is_ok());
Ok(())
}
sourcepub fn target_metadata<'a>(&self) -> TargetMetadataQueryBuilder<'a>
pub fn target_metadata<'a>(&self) -> TargetMetadataQueryBuilder<'a>
Create a TargetMetadataQueryBuilder
to apply filters to a target metadata
query before sending it to Prometheus.
See also: Prometheus API documentation
use prometheus_http_query::{Client, Selector};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
// Retrieve metadata for a specific metric from all targets.
let response = client.target_metadata().metric("go_goroutines").get().await;
assert!(response.is_ok());
// Retrieve metric metadata from specific targets.
let s = Selector::new().eq("job", "prometheus");
let response = client.target_metadata().match_target(&s).get().await;
assert!(response.is_ok());
// Retrieve metadata for a specific metric from targets that match a specific label set.
let s = Selector::new().eq("job", "node");
let response = client.target_metadata()
.metric("node_cpu_seconds_total")
.match_target(&s)
.get()
.await;
assert!(response.is_ok());
Ok(())
}
sourcepub fn metric_metadata(&self) -> MetricMetadataQueryBuilder
pub fn metric_metadata(&self) -> MetricMetadataQueryBuilder
Create a MetricMetadataQueryBuilder
to apply filters to a metric metadata
query before sending it to Prometheus.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
// Retrieve metadata for a all metrics.
let response = client.metric_metadata().get().await;
assert!(response.is_ok());
// Limit the number of returned metrics.
let response = client.metric_metadata().limit(100).get().await;
assert!(response.is_ok());
// Retrieve metadata for a specific metric but with a per-metric
// metadata limit.
let response = client.metric_metadata()
.metric("go_goroutines")
.limit_per_metric(5)
.get()
.await;
assert!(response.is_ok());
Ok(())
}
sourcepub async fn is_server_healthy(&self) -> Result<bool, Error>
pub async fn is_server_healthy(&self) -> Result<bool, Error>
Check Prometheus server health.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
assert!(client.is_server_healthy().await?);
Ok(())
}
sourcepub async fn is_server_ready(&self) -> Result<bool, Error>
pub async fn is_server_ready(&self) -> Result<bool, Error>
Check Prometheus server readiness.
See also: Prometheus API documentation
use prometheus_http_query::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let client = Client::default();
assert!(client.is_server_ready().await?);
Ok(())
}
Trait Implementations§
source§impl FromStr for Client
impl FromStr for Client
source§fn from_str(url: &str) -> Result<Self, Self::Err>
fn from_str(url: &str) -> Result<Self, Self::Err>
Create a Client from a custom base URL. Note that the API-specific
path segments (like /api/v1/query
) are added automatically.
use prometheus_http_query::Client;
use std::str::FromStr;
let client = Client::from_str("http://proxy.example.com/prometheus");
assert!(client.is_ok());
source§impl TryFrom<&str> for Client
impl TryFrom<&str> for Client
source§fn try_from(url: &str) -> Result<Self, Self::Error>
fn try_from(url: &str) -> Result<Self, Self::Error>
Create a Client
from a custom base URL. Note that the API-specific
path segments (like /api/v1/query
) are added automatically.
use prometheus_http_query::Client;
use std::convert::TryFrom;
let client = Client::try_from("http://proxy.example.com/prometheus");
assert!(client.is_ok());
source§impl TryFrom<String> for Client
impl TryFrom<String> for Client
source§fn try_from(url: String) -> Result<Self, Self::Error>
fn try_from(url: String) -> Result<Self, Self::Error>
Create a Client
from a custom base URL. Note that the API-specific
path segments (like /api/v1/query
) are added automatically.
use prometheus_http_query::Client;
use std::convert::TryFrom;
let url = String::from("http://proxy.example.com/prometheus");
let client = Client::try_from(url);
assert!(client.is_ok());