use crate::error::{ClientError, Error};
use crate::response::*;
use crate::util::{self, build_final_url, Duration, RuleKind, TargetState, ToBaseUrl};
use reqwest::header::{HeaderMap, HeaderValue, IntoHeaderName, CONTENT_TYPE};
use reqwest::Method as HttpMethod;
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use url::Url;
#[derive(Clone, Debug)]
pub struct InstantQueryBuilder {
client: Client,
params: Vec<(&'static str, String)>,
headers: Option<HeaderMap<HeaderValue>>,
}
impl InstantQueryBuilder {
pub fn time(mut self, time: i64) -> Self {
self.params.push(("time", time.to_string()));
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.params.push(("timeout", timeout.to_string()));
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.params.push(("limit", limit.to_string()));
self
}
pub fn lookback_delta(mut self, duration: Duration) -> Self {
self.params.push(("lookback_delta", duration.to_string()));
self
}
pub fn stats(mut self) -> Self {
self.params.push(("stats", String::from("all")));
self
}
pub fn header<K: IntoHeaderName, T: Into<HeaderValue>>(mut self, name: K, value: T) -> Self {
self.headers
.get_or_insert_with(Default::default)
.append(name, value.into());
self
}
pub fn query(mut self, name: &'static str, value: impl ToString) -> Self {
self.params.push((name, value.to_string()));
self
}
pub async fn get(self) -> Result<PromqlResult, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn post(self) -> Result<PromqlResult, Error> {
let response = self.post_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/query", &self.params, HttpMethod::GET, self.headers)
.await
}
pub async fn post_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/query", &self.params, HttpMethod::POST, self.headers)
.await
}
}
#[derive(Clone, Debug)]
pub struct RangeQueryBuilder {
client: Client,
params: Vec<(&'static str, String)>,
headers: Option<HeaderMap<HeaderValue>>,
}
impl RangeQueryBuilder {
pub fn timeout(mut self, timeout: Duration) -> Self {
self.params.push(("timeout", timeout.to_string()));
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.params.push(("limit", limit.to_string()));
self
}
pub fn lookback_delta(mut self, duration: Duration) -> Self {
self.params.push(("lookback_delta", duration.to_string()));
self
}
pub fn stats(mut self) -> Self {
self.params.push(("stats", String::from("all")));
self
}
pub fn header<K: IntoHeaderName, T: Into<HeaderValue>>(mut self, name: K, value: T) -> Self {
self.headers
.get_or_insert_with(Default::default)
.append(name, value.into());
self
}
pub fn query(mut self, name: &'static str, value: impl ToString) -> Self {
self.params.push((name, value.to_string()));
self
}
pub async fn get(self) -> Result<PromqlResult, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn post(self) -> Result<PromqlResult, Error> {
let response = self.post_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send(
"api/v1/query_range",
&self.params,
HttpMethod::GET,
self.headers,
)
.await
}
pub async fn post_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send(
"api/v1/query_range",
&self.params,
HttpMethod::POST,
self.headers,
)
.await
}
}
#[derive(Clone, Debug)]
pub struct RulesQueryBuilder {
client: Client,
kind: Option<RuleKind>,
rule_names: Vec<String>,
rule_groups: Vec<String>,
files: Vec<String>,
exclude_alerts: Option<bool>,
selectors: Vec<String>,
}
impl RulesQueryBuilder {
pub fn kind(mut self, kind: RuleKind) -> Self {
self.kind = Some(kind);
self
}
pub fn rule_names<T>(mut self, names: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.rule_names
.extend(names.into_iter().map(|n| n.to_string()));
self
}
pub fn rule_name(mut self, name: impl std::fmt::Display) -> Self {
self.rule_names.push(name.to_string());
self
}
pub fn rule_groups<T>(mut self, groups: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.rule_groups
.extend(groups.into_iter().map(|g| g.to_string()));
self
}
pub fn rule_group(mut self, group: impl std::fmt::Display) -> Self {
self.rule_groups.push(group.to_string());
self
}
pub fn files<T>(mut self, files: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.files.extend(files.into_iter().map(|f| f.to_string()));
self
}
pub fn file(mut self, file: impl std::fmt::Display) -> Self {
self.files.push(file.to_string());
self
}
pub fn exclude_alerts(mut self, exclude_alerts: bool) -> Self {
self.exclude_alerts = Some(exclude_alerts);
self
}
pub fn match_many<T>(mut self, selectors: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.selectors
.extend(selectors.into_iter().map(|item| item.to_string()));
self
}
pub fn match_one(mut self, selector: impl std::fmt::Display) -> Self {
self.selectors.push(selector.to_string());
self
}
pub async fn get(self) -> Result<Vec<RuleGroup>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response)
.await
.map(|r: RuleGroups| r.groups)
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
if let Some(k) = self.kind {
params.push(("type", k.to_query_param()))
}
for name in self.rule_names {
params.push(("rule_name[]", name))
}
for group in self.rule_groups {
params.push(("rule_group[]", group))
}
for file in self.files {
params.push(("file[]", file))
}
if let Some(exclude_alerts) = self.exclude_alerts {
params.push(("exclude_alerts", exclude_alerts.to_string()));
}
for selector in self.selectors {
params.push(("match[]", selector.to_string()));
}
self.client
.send("api/v1/rules", ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct TargetMetadataQueryBuilder {
client: Client,
match_target: Option<String>,
metric: Option<String>,
limit: Option<i64>,
}
impl TargetMetadataQueryBuilder {
pub fn match_target(mut self, match_target: impl std::fmt::Display) -> Self {
self.match_target = Some(match_target.to_string());
self
}
pub fn metric(mut self, metric: impl std::fmt::Display) -> Self {
self.metric = Some(metric.to_string());
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub async fn get(self) -> Result<Vec<TargetMetadata>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
if let Some(metric) = self.metric {
params.push(("metric", metric))
}
if let Some(match_target) = self.match_target {
params.push(("match_target", match_target))
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()))
}
self.client
.send("api/v1/targets/metadata", ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct TargetsQueryBuilder {
client: Client,
state: Option<TargetState>,
scrape_pool: Option<String>,
}
impl TargetsQueryBuilder {
pub fn state(mut self, state: TargetState) -> Self {
self.state = Some(state);
self
}
pub fn scrape_pool(mut self, name: impl std::fmt::Display) -> Self {
self.scrape_pool = Some(name.to_string());
self
}
pub async fn get(self) -> Result<Targets, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
if let Some(state) = self.state {
params.push(("state", state.to_string()))
}
if let Some(scrape_pool) = self.scrape_pool {
params.push(("scrapePool", scrape_pool))
}
self.client
.send("api/v1/targets", ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct MetricMetadataQueryBuilder {
client: Client,
metric: Option<String>,
limit: Option<i64>,
limit_per_metric: Option<i64>,
}
impl MetricMetadataQueryBuilder {
pub fn metric(mut self, metric: impl std::fmt::Display) -> Self {
self.metric = Some(metric.to_string());
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_per_metric(mut self, limit_per_metric: i64) -> Self {
self.limit_per_metric = Some(limit_per_metric);
self
}
pub async fn get(self) -> Result<HashMap<String, Vec<MetricMetadata>>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
if let Some(metric) = self.metric {
params.push(("metric", metric.to_string()))
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()))
}
if let Some(limit_per_metric) = self.limit_per_metric {
params.push(("limit_per_metric", limit_per_metric.to_string()))
}
self.client
.send("api/v1/metadata", ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct SeriesQueryBuilder {
client: Client,
selectors: Vec<String>,
start: Option<i64>,
end: Option<i64>,
limit: Option<i64>,
}
impl SeriesQueryBuilder {
pub fn match_many<T>(mut self, selectors: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.selectors
.extend(selectors.into_iter().map(|item| item.to_string()));
self
}
pub fn match_one(mut self, selector: impl std::fmt::Display) -> Self {
self.selectors.push(selector.to_string());
self
}
pub fn start(mut self, start: i64) -> Self {
self.start = Some(start);
self
}
pub fn end(mut self, end: i64) -> Self {
self.end = Some(end);
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub async fn get(self) -> Result<Vec<HashMap<String, String>>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
for selector in self.selectors {
params.push(("match[]", selector.to_string()));
}
if let Some(start) = self.start {
params.push(("start", start.to_string()));
}
if let Some(end) = self.end {
params.push(("end", end.to_string()));
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
self.client
.send("api/v1/series", ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct LabelNamesQueryBuilder {
client: Client,
selectors: Vec<String>,
start: Option<i64>,
end: Option<i64>,
limit: Option<i64>,
}
impl LabelNamesQueryBuilder {
pub fn match_many<T>(mut self, selectors: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.selectors
.extend(selectors.into_iter().map(|item| item.to_string()));
self
}
pub fn match_one(mut self, selector: impl std::fmt::Display) -> Self {
self.selectors.push(selector.to_string());
self
}
pub fn start(mut self, start: i64) -> Self {
self.start = Some(start);
self
}
pub fn end(mut self, end: i64) -> Self {
self.end = Some(end);
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub async fn get(self) -> Result<Vec<String>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
for selector in self.selectors {
params.push(("match[]", selector.to_string()));
}
if let Some(start) = self.start {
params.push(("start", start.to_string()));
}
if let Some(end) = self.end {
params.push(("end", end.to_string()));
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
self.client
.send("api/v1/labels", ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct LabelValuesQueryBuilder {
client: Client,
label: String,
selectors: Vec<String>,
start: Option<i64>,
end: Option<i64>,
limit: Option<i64>,
}
impl LabelValuesQueryBuilder {
pub fn match_many<T>(mut self, selectors: T) -> Self
where
T: IntoIterator,
T::Item: std::fmt::Display,
{
self.selectors
.extend(selectors.into_iter().map(|item| item.to_string()));
self
}
pub fn match_one(mut self, selector: impl std::fmt::Display) -> Self {
self.selectors.push(selector.to_string());
self
}
pub fn start(mut self, start: i64) -> Self {
self.start = Some(start);
self
}
pub fn end(mut self, end: i64) -> Self {
self.end = Some(end);
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub async fn get(self) -> Result<Vec<String>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
let mut params = vec![];
for selector in self.selectors {
params.push(("match[]", selector.to_string()));
}
if let Some(start) = self.start {
params.push(("start", start.to_string()));
}
if let Some(end) = self.end {
params.push(("end", end.to_string()));
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
let path = format!("api/v1/label/{}/values", self.label);
self.client
.send(&path, ¶ms, HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct AlertsQueryBuilder {
client: Client,
}
impl AlertsQueryBuilder {
pub async fn get(self) -> Result<Vec<Alert>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response)
.await
.map(|r: Alerts| r.alerts)
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/alerts", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct AlertmanagersQueryBuilder {
client: Client,
}
impl AlertmanagersQueryBuilder {
pub async fn get(self) -> Result<Alertmanagers, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/alertmanagers", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct FlagsQueryBuilder {
client: Client,
}
impl FlagsQueryBuilder {
pub async fn get(self) -> Result<HashMap<String, String>, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/status/flags", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct BuildInformationQueryBuilder {
client: Client,
}
impl BuildInformationQueryBuilder {
pub async fn get(self) -> Result<BuildInformation, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/status/buildinfo", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct RuntimeInformationQueryBuilder {
client: Client,
}
impl RuntimeInformationQueryBuilder {
pub async fn get(self) -> Result<RuntimeInformation, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/status/runtimeinfo", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct TsdbStatisticsQueryBuilder {
client: Client,
}
impl TsdbStatisticsQueryBuilder {
pub async fn get(self) -> Result<TsdbStatistics, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/status/tsdb", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct WalReplayStatisticsQueryBuilder {
client: Client,
}
impl WalReplayStatisticsQueryBuilder {
pub async fn get(self) -> Result<WalReplayStatistics, Error> {
let response = self.get_raw().await?;
Client::deserialize(response).await
}
pub async fn get_raw(self) -> Result<reqwest::Response, Error> {
self.client
.send("api/v1/status/walreplay", &(), HttpMethod::GET, None)
.await
}
}
#[derive(Clone, Debug)]
pub struct Client {
pub(crate) client: reqwest::Client,
pub(crate) base_url: Url,
}
impl Default for Client {
fn default() -> Self {
Client {
client: reqwest::Client::new(),
base_url: Url::parse("http://127.0.0.1:9090/").unwrap(),
}
}
}
impl std::str::FromStr for Client {
type Err = crate::error::Error;
fn from_str(url: &str) -> Result<Self, Self::Err> {
let client = Client {
base_url: url.to_base_url()?,
client: reqwest::Client::new(),
};
Ok(client)
}
}
impl std::convert::TryFrom<&str> for Client {
type Error = crate::error::Error;
fn try_from(url: &str) -> Result<Self, Self::Error> {
let client = Client {
base_url: url.to_base_url()?,
client: reqwest::Client::new(),
};
Ok(client)
}
}
impl std::convert::TryFrom<String> for Client {
type Error = crate::error::Error;
fn try_from(url: String) -> Result<Self, Self::Error> {
let client = Client {
base_url: url.to_base_url()?,
client: reqwest::Client::new(),
};
Ok(client)
}
}
impl Client {
pub fn inner(&self) -> &reqwest::Client {
&self.client
}
pub fn base_url(&self) -> &Url {
&self.base_url
}
pub fn from(client: reqwest::Client, url: &str) -> Result<Self, Error> {
let base_url = url.to_base_url()?;
Ok(Client { base_url, client })
}
async fn send<S: Serialize>(
&self,
path: &str,
params: &S,
method: HttpMethod,
headers: Option<HeaderMap<HeaderValue>>,
) -> Result<reqwest::Response, Error> {
let url = build_final_url(self.base_url.clone(), path);
let mut request = match method {
HttpMethod::GET => self.client.get(url).query(params),
HttpMethod::POST => self.client.post(url).form(params),
_ => unreachable!(),
};
if let Some(headers) = headers {
request = request.headers(headers);
}
let response = request.send().await.map_err(|source| {
Error::Client(ClientError {
message: "failed to send request to server",
source: Some(source),
})
})?;
Ok(response)
}
pub fn query(&self, query: impl std::fmt::Display) -> InstantQueryBuilder {
InstantQueryBuilder {
client: self.clone(),
params: vec![("query", query.to_string())],
headers: Default::default(),
}
}
pub fn query_range(
&self,
query: impl std::fmt::Display,
start: i64,
end: i64,
step: f64,
) -> RangeQueryBuilder {
RangeQueryBuilder {
client: self.clone(),
params: vec![
("query", query.to_string()),
("start", start.to_string()),
("end", end.to_string()),
("step", step.to_string()),
],
headers: Default::default(),
}
}
pub fn series(&self) -> SeriesQueryBuilder {
SeriesQueryBuilder {
client: self.clone(),
selectors: vec![],
start: None,
end: None,
limit: None,
}
}
pub fn label_names(&self) -> LabelNamesQueryBuilder {
LabelNamesQueryBuilder {
client: self.clone(),
selectors: vec![],
start: None,
end: None,
limit: None,
}
}
pub fn label_values(&self, label: impl std::fmt::Display) -> LabelValuesQueryBuilder {
LabelValuesQueryBuilder {
client: self.clone(),
label: label.to_string(),
selectors: vec![],
start: None,
end: None,
limit: None,
}
}
pub fn targets(&self) -> TargetsQueryBuilder {
TargetsQueryBuilder {
client: self.clone(),
state: None,
scrape_pool: None,
}
}
pub fn rules(&self) -> RulesQueryBuilder {
RulesQueryBuilder {
client: self.clone(),
kind: None,
rule_names: vec![],
rule_groups: vec![],
files: vec![],
exclude_alerts: None,
selectors: vec![],
}
}
pub fn alerts(&self) -> AlertsQueryBuilder {
AlertsQueryBuilder {
client: self.clone(),
}
}
pub fn flags(&self) -> FlagsQueryBuilder {
FlagsQueryBuilder {
client: self.clone(),
}
}
pub fn build_information(&self) -> BuildInformationQueryBuilder {
BuildInformationQueryBuilder {
client: self.clone(),
}
}
pub fn runtime_information(&self) -> RuntimeInformationQueryBuilder {
RuntimeInformationQueryBuilder {
client: self.clone(),
}
}
pub fn tsdb_statistics(&self) -> TsdbStatisticsQueryBuilder {
TsdbStatisticsQueryBuilder {
client: self.clone(),
}
}
pub fn wal_replay_statistics(&self) -> WalReplayStatisticsQueryBuilder {
WalReplayStatisticsQueryBuilder {
client: self.clone(),
}
}
pub fn alertmanagers(&self) -> AlertmanagersQueryBuilder {
AlertmanagersQueryBuilder {
client: self.clone(),
}
}
pub fn target_metadata(&self) -> TargetMetadataQueryBuilder {
TargetMetadataQueryBuilder {
client: self.clone(),
match_target: None,
metric: None,
limit: None,
}
}
pub fn metric_metadata(&self) -> MetricMetadataQueryBuilder {
MetricMetadataQueryBuilder {
client: self.clone(),
metric: None,
limit: None,
limit_per_metric: None,
}
}
pub async fn is_server_healthy(&self) -> Result<bool, Error> {
let url = build_final_url(self.base_url.clone(), "-/healthy");
self.client
.get(url)
.send()
.await
.map_err(|source| {
Error::Client(ClientError {
message: "failed to send request to health endpoint",
source: Some(source),
})
})?
.error_for_status()
.map_err(|source| {
Error::Client(ClientError {
message: "request to health endpoint returned an error",
source: Some(source),
})
})
.map(|_| true)
}
pub async fn is_server_ready(&self) -> Result<bool, Error> {
let url = build_final_url(self.base_url.clone(), "-/ready");
self.client
.get(url)
.send()
.await
.map_err(|source| {
Error::Client(ClientError {
message: "failed to send request to readiness endpoint",
source: Some(source),
})
})?
.error_for_status()
.map_err(|source| {
Error::Client(ClientError {
message: "request to readiness endpoint returned an error",
source: Some(source),
})
})
.map(|_| true)
}
async fn deserialize<D: DeserializeOwned>(response: reqwest::Response) -> Result<D, Error> {
let header = CONTENT_TYPE;
if !util::is_json(response.headers().get(header)) {
return Err(Error::Client(ClientError {
message: "failed to parse response from server due to invalid media type",
source: response.error_for_status().err(),
}));
}
let response = response.json::<ApiResponse<D>>().await.map_err(|source| {
Error::Client(ClientError {
message: "failed to parse JSON response from server",
source: Some(source),
})
})?;
match response {
ApiResponse::Success { data } => Ok(data),
ApiResponse::Error(e) => Err(Error::Prometheus(e)),
}
}
}