16 releases (7 breaking)

0.8.0 Dec 28, 2020
0.7.2 Nov 21, 2020
0.6.2 Nov 18, 2020
0.5.0 Apr 29, 2020
0.3.0 Mar 14, 2019

#31 in Debugging

Download history 228/week @ 2020-09-23 200/week @ 2020-09-30 319/week @ 2020-10-07 463/week @ 2020-10-14 381/week @ 2020-10-21 346/week @ 2020-10-28 279/week @ 2020-11-04 351/week @ 2020-11-11 413/week @ 2020-11-18 346/week @ 2020-11-25 449/week @ 2020-12-02 634/week @ 2020-12-09 788/week @ 2020-12-16 524/week @ 2020-12-23 585/week @ 2020-12-30 824/week @ 2021-01-06

1,792 downloads per month
Used in 3 crates

MIT license

22KB
276 lines

prometheus_exporter

Build Status crates.io docs.rs

Helper libary to export prometheus metrics using tiny-http. 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 tiny-http 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.11.

For information on how to migrate from an older crate version follow MIGRATION.

Usage

Add this to your Cargo.toml:

[dependencies]
prometheus_exporter = "0.8"

The most basic way to use this crate is to run the following:

prometheus_exporter::start("0.0.0.0:9184".parse().unwrap()).unwrap())

This will start the exporter and bind the http server to 0.0.0.0:9184. After that you can just update the metrics how you see fit. As long as those metrics are put into the global prometheus registry the changed metrics will be exported.

Another way to use the crate is like this:

let exporter = prometheus_exporter::start("0.0.0.0:9184".parse().unwrap()).unwrap());
let guard = exporter.wait_request()

This will block the current thread until a request has been received on the http server. It also returns a guard which will make the http server wait until the guard is dropped. This is useful to always export consistent metrics as all of the metrics can be updated before they get exported.

See the documentation and the examples for more information on how to use this crate.

Basic Example

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

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

use env_logger::{
    Builder,
    Env,
};
use log::info;
use prometheus_exporter::prometheus::register_gauge;
use std::net::SocketAddr;

fn main() {
    // Setup logger with default level info so we can see the messages from
    // prometheus_exporter.
    Builder::from_env(Env::default().default_filter_or("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!("simple_the_answer", "to everything")
        .expect("can not create gauge simple_the_answer");

    metric.set(42.0);

    // Start exporter
    prometheus_exporter::start(addr).expect("can not start exporter");

    // Get metrics from exporter
    let body = reqwest::blocking::get(&format!("http://{}/metrics", addr_raw))
        .expect("can not get metrics from exporter")
        .text()
        .expect("can not body text from request");

    info!("Exporter metrics:\n{}", body);
}

Dependencies

~6MB
~147K SLoC