#prometheus #write #remote #reqwest #reqwest-client #header #metrics

prometheus-reqwest-remote-write

A prometheus remote write client for reqwest

4 releases

0.2.1 Oct 16, 2024
0.2.0 Oct 16, 2024
0.1.1 Oct 15, 2024
0.1.0 Oct 15, 2024

#297 in HTTP client

Download history 354/week @ 2024-10-12 76/week @ 2024-10-19 30/week @ 2024-10-26 20/week @ 2024-11-02 10/week @ 2024-11-09 17/week @ 2024-11-16 48/week @ 2024-11-23 64/week @ 2024-11-30

143 downloads per month
Used in unleash-edge

MIT license

25KB
472 lines

Prometheus Reqwest Remote Write

This repository contains a Rust library for sending metrics to Prometheus over the remote write API. It uses Reqwest APIs to build a snappy compressed payload and sends it to the Prometheus server. Building directly from the prometheus protobuff format, if you have a prometheus registry, Since a reqwest client can have default headers baked in, the library does not provide a way to configure extra headers. It does set the correct headers according to remote write 1.0 spec though, so if you need basic authentication, bake it into the client you pass to the build_http_request call

Basic Usage


use prometheus_reqwest_remote_write::WriteRequest;

pub async fn from_prometheus_registry_and_request_client(client: reqwest::Client, remote_write_url: &str, registry: prometheus::Registry) {
    let write_request = WriteRequest::from_metric_families(registry.gather()).expect("Could not format write request");
    let http_request = write_request.build_http_request(client.clone(), remote_write_url, "your_user_agent").expect("Could not build http request");
    match client.execute(http_request).await {
        Ok(r) =>  {
            if r.status().is_success() {
                println!("Metrics sent successfully");
            } else {
                println!("Failed to send metrics: {:?}", r.text().await.expect("Could not read body from response");
            }
        },
        Err(e) => {
            println!("Failed to send metrics: {:?}", e);
        }
    }

Dependencies

~7–18MB
~242K SLoC