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

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, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), 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
        .map_err(Error::Client)?;

    // Prometheus does not allow HEAD requests.
    assert_eq!(response.status(), reqwest::StatusCode::METHOD_NOT_ALLOWED);
    Ok(())
}

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(), "http://127.0.0.1:9090/api/v1");

let client = Client::from_str("https://proxy.example.com:8443/prometheus").unwrap();

assert_eq!(client.base_url(), "https://proxy.example.com:8443/prometheus/api/v1");

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, Error};

fn main() -> Result<(), Error> {
    let client = {
        let c = reqwest::Client::builder()
            .no_proxy()
            .build()
            .map_err(Error::Client)?;
        Client::from(c, "https://prometheus.example.com")
    };

    assert!(client.is_ok());
    Ok(())
}

Execute an instant query.

Arguments
  • query - PromQL query to exeute
  • time - Evaluation timestamp as Unix timestamp (seconds). Optional, defaults to Prometheus server time.
  • timeout - Evaluation timeout in milliseconds. Optional.

See also: Prometheus API documentation

use prometheus_http_query::{Error, Client};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let q = "prometheus_http_requests_total";

    let response = client.query(q, Some(1648379069), Some(1000)).await?;

    assert!(response.as_instant().is_some());

    Ok(())
}

Execute a range query.

Arguments
  • query - PromQL query to exeute
  • start - Start timestamp as Unix timestamp (seconds)
  • end - End timestamp as Unix timestamp (seconds)
  • step - Query resolution step width as float number of seconds
  • timeout - Evaluation timeout in milliseconds. Optional.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let q = "prometheus_http_requests_total";

    let response = client.query_range(q, 1648373100, 1648373300, 10.0, None).await?;

    assert!(response.as_range().is_some());

    Ok(())
}

Find time series that match certain label sets (Selectors).

Arguments
  • selectors - List of Selectors that select the series to return. Must not be empty.
  • start - Start timestamp as Unix timestamp (seconds). Optional.
  • end - End timestamp as Unix timestamp (seconds). Optional.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Selector, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), 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], None, None).await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve label names.

Arguments
  • selectors - List of Selectors to restrict the set of time series to read the label names from. Optional.
  • start - Start timestamp as Unix timestamp (seconds). Optional.
  • end - End timestamp as Unix timestamp (seconds). Optional.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Selector, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    // To retrieve a list of all labels:
    let response = client.label_names(None, None, None).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 set = Some(vec![s1, s2]);

    let response = client.label_names(set, None, None).await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve all label values for a specific label name.

Arguments
  • label - Name of the label to return all occuring label values for.
  • selectors - List of Selectors to restrict the set of time series to read the label values from. Optional.
  • start - Start timestamp as Unix timestamp (seconds). Optional.
  • end - End timestamp as Unix timestamp (seconds). Optional.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Selector, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    // To retrieve a list of all label values for a specific label name:
    let response = client.label_values("job", None, None, None).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 set = Some(vec![s1]);

    let response = client.label_values("job", set, None, None).await;

    assert!(response.is_ok());

    Ok(())
}

Query the current state of target discovery.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error, TargetState};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), 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(())
}

Retrieve a list of rule groups of recording and alerting rules.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error, RuleType};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.rules(None).await;

    assert!(response.is_ok());

    // Filter rules by type:
    let response = client.rules(Some(RuleType::Alert)).await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve a list of active alerts.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.alerts().await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve a list of flags that Prometheus was configured with.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.flags().await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve Prometheus server build information.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.build_information().await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve Prometheus server runtime information.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.runtime_information().await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve Prometheus TSDB statistics.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.tsdb_statistics().await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve WAL replay statistics.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.wal_replay_statistics().await;

    assert!(response.is_ok());

    Ok(())
}

Query the current state of alertmanager discovery.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    let response = client.alertmanagers().await;

    assert!(response.is_ok());

    Ok(())
}

Retrieve metadata about metrics that are currently scraped from targets, along with target information.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error, Selector};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    // Retrieve metadata for a specific metric from all targets.
    let response = client.target_metadata(Some("go_routines"), None, None).await;

    assert!(response.is_ok());

    // Retrieve metric metadata from specific targets.
    let s = Selector::new().eq("job", "prometheus");

    let response = 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().eq("job", "node");

    let response = 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.

See also: Prometheus API documentation

use prometheus_http_query::{Client, Error};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let client = Client::default();

    // Retrieve metadata for a all metrics.
    let response = client.metric_metadata(None, None).await;

    assert!(response.is_ok());

    // Limit the number of returned metrics
    let response = client.metric_metadata(None, Some(10)).await;

    assert!(response.is_ok());

    // Retrieve metadata of a specific metric.
    let response = client.metric_metadata(Some("go_routines"), None).await;

    assert!(response.is_ok());

    Ok(())
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Create a standard Client that sends requests to “http://127.0.0.1:9090/api/v1”.

use prometheus_http_query::Client;

let client = Client::default();

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());

The associated error which can be returned from parsing.

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());

The type returned in the event of a conversion 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());

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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