Crates.io | prometheus-reqwest-remote-write |
lib.rs | prometheus-reqwest-remote-write |
version | 0.2.1 |
source | src |
created_at | 2024-10-15 13:48:06.518509 |
updated_at | 2024-10-16 12:40:18.736599 |
description | A prometheus remote write client for reqwest |
homepage | https://github.com/chriswk/prometheus_request_remote_write |
repository | https://github.com/chriswk/prometheus_reqwest_remote_write |
max_upload_size | |
id | 1409477 |
size | 31,160 |
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
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);
}
}