Code Monkey home page Code Monkey logo

prometheus-http-query's Introduction

prometheus-http-query

This crate provides an interface to the Prometheus HTTP API and leverage Rust's type system in the process where applicable.

Example

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

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

    // Evaluate a PromQL query.
    let q = "topk by (code) (5, prometheus_http_requests_total)";
    let response = client.query(q).get().await?;
    assert!(response.data().as_vector().is_some());

    // Retrieve active alerts.
    let alerts = client.alerts().await;
    assert!(alerts.is_ok());

    // Retrieve recording rules.
    let recording_rules = client.rules().kind(RuleKind::Recording).get().await;
    assert!(recording_rules.is_ok());

    // Retrieve a list of time series that match certain labels sets ("series selectors").
    let select1 = Selector::new()
        .eq("handler", "/api/v1/query");

    let select2 = Selector::new()
        .eq("job", "node")
        .regex_eq("mode", ".+");

    let time_series = client.series(&[select1, select2]).get().await;
    assert!(time_series.is_ok());

    Ok(())
}

Compatibility

This library is generally compatible with Prometheus versions starting from v2.30. Individual client methods might fail with older versions as newer versions of Prometheus server support additional methods and query parameters. Run Prometheus server version >= 2.46 to ensure maximum compatibility.

Tests

In order to run all tests a Prometheus server must be running at http://localhost:9090. No special configuration is required at this point, simply run: cargo test

Contributing

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.

prometheus-http-query's People

Contributors

alesharik avatar chris13524 avatar lasantosr avatar nickmonad avatar puetzp avatar quodlibetor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

prometheus-http-query's Issues

Option to include custom headers on QueryBuilders

I've seen #3 but the workaround only provides a way of setting static headers for every request.

There is no way to include a dynamic header (for example the tenant being queried to Cortex) without re-creating a reqwest client on each query.

Add a blocking client API

In the spirit of reqwest, add an implementation of prometheus_http_query::Client that can be used in synchronous code.

Currently the async Client and all async shortcut functions like label_values are situated directly under the crate root.
The blocking API could be realized in a blocking module that contains a blocking::Client and all the shortcut functions e.g. blocking::label_values.

Client panicked when content-type in response is `application/json; charset=utf-8`

I'm dealing with a prometheus server that sets its content type in responses to application/json; charset=utf-8. This line causes panic with message: called Result::unwrap_err() on an Ok value:

let content_type = HeaderValue::from_static("application/json");
if headers.get(CONTENT_TYPE) == Some(&content_type) {
response.json().await.map_err(Error::Client)
} else {
Err(Error::Client(response.error_for_status().unwrap_err()))
}

Breaking changes in 0.7.0

Updating to 0.7.0 broke quite a bit of code on our end. In my opinion this should only happen in major versions.

A few questions regarding migrating to 0.7.0:

  • What is the new Error::ResponseParse. Is Error::ParseUrl the same thing?
  • Client::metric_metadata lost its parameters. Is there a replacement or migration guide?

Ability to pass request headers

Hi there,

Would it be possible to add ability to add request headers ? Use case - I need to pass some authentication headers to reach prometheus , also need to identify myself via User-Agent.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.