prometheus_exporter

Build Status crates.io docs.rs

Helper libary to export prometheus metrics using hyper. It's intended to help writing prometheus exporters without the need to setup and maintain a http webserver. If the program also uses a http server for other purposes this package is probably not the best way and rust-prometheus should be used directly.

It uses rust-prometheus for collecting and rendering the prometheus metrics and hyper for exposing the metrics through http.

NOTICE: You have to use the same prometheus crate version that is used by this crate to make sure that the global registrar use by the prometheus macros works as expected. This crate re-exports the prometheuse crate to make it easier to keep versions in sync (see examples). Currently this crate uses prometheus version 0.9.

Usage

Add this to your Cargo.toml:

toml [dependencies] prometheus_exporter = "0.5"

There are three ways on how to use the exporter:

For examples on how to use prometheus_exporter see the examples folder.

A very simple example looks like this (from examples/simple/src/main.rs):

```rust // Will create an exporter with a single metric that does not change

use envlogger::{ Builder, Env, }; use prometheusexporter::{ PrometheusExporter, prometheus::register_gauge, }; use std::net::SocketAddr;

fn main() { // Setup logger with default level info so we can see the messages from // prometheusexporter. Builder::fromenv(Env::default().defaultfilteror("info")).init();

// Parse address used to bind exporter to.
let addr_raw = "0.0.0.0:9184";
let addr: SocketAddr = addr_raw.parse().expect("can not parse listen addr");

// Create metric
let metric =
    register_gauge!("the_answer", "to everything").expect("can not create gauge the_answer");

metric.set(42.0);

// Start exporter and makes metrics available under http://0.0.0.0:9184/metrics
PrometheusExporter::run(&addr).expect("can not run exporter");

} ```