alexanderthaller/prometheus_exporter

prometheus_exporter

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.

NOTICE Version 0.8.0 used a vulnerable version of tiny_http please update to version 0.8.1 or higher. See issue #18 for more information.

Usage

Add this to your Cargo.toml:

[dependencies]
 = "0.8"

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

::start("0.0.0.0:9184".parse().expect(
"failed to parse binding",
))
.expect("failed to start prometheus exporter");

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 = ::start("0.0.0.0:9184".parse().expect(
"failed to parse binding",
))
.expect("failed to start prometheus exporter");

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::register_gauge;
use std::net::SocketAddr;

fn main() {
    // Setup logger with default level info so we can see the messages from
    // .
    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
    ::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);
}
Issues

Collection of the latest Issues

vi

vi

Comment Icon0

I assume gathering metrics is a rare, relatively lightweight and not very performance-sensitive task, so just one additional thread is enough for it. But metric exporter seems to start multiple additional threads, wasting PIDs and other system resources just for rare occasional replies.

AlexanderThaller

AlexanderThaller

Comment Icon0

It would be nice to be able to use a different registry than the global one. For that the code needs to be extended to have a registry field that can be something like Option. In the None case the global one would be used. Maybe that would also allow different prometheus crate versions to be used. Probably not tho as the different versions would lead to different types for the Registry.

Versions

Find the latest versions by id

0.8.4 - Jan 19, 2022

What's Changed

New Contributors

Full Changelog: https://github.com/AlexanderThaller/prometheus_exporter/compare/0.8.3...0.8.4

0.8.3 - Oct 18, 2021

What's Changed

New Contributors

Full Changelog: https://github.com/AlexanderThaller/prometheus_exporter/compare/v0.8.2...0.8.3

v0.8.2 - Apr 26, 2021

  • ADD: Add function with_endpoint to Builder that allows to change the metric endpoint from the default /metrics. (thanks @jward-bw)

v0.8.1 - Apr 20, 2021

v0.8.0 - Dec 28, 2020

  • CHANGE: Update prometheus dependency from 0.10 to 0.11.

Information - Updated May 19, 2022

Stars: 11
Forks: 10
Issues: 6

Rust library for Self Organising Maps (SOM)

Add rusticsom as a dependency in Cargo

Rust library for Self Organising Maps (SOM)

Rust library for parsing configuration files

The 'option' can be any string with no whitespace

Rust library for parsing configuration files

Rust library for the Pimoroni Four Letter pHAT

This library aims to port ht16k33 (or rather a fork, as of right now) so credit goes to ht16k33-diet

Rust library for the Pimoroni Four Letter pHAT

Rust library for emulating 32-bit RISC-V

This library can execute instructions against any memory and register file that implements

Rust library for emulating 32-bit RISC-V

Rust library for connecting to the IPFS HTTP API using Hyper/Actix

You can use actix-web as a backend instead of hyper

Rust library for connecting to the IPFS HTTP API using Hyper/Actix

Rust library to manipulate file system access control lists (ACL) on macOS, Linux, and FreeBSD

This module provides two high level functions, getfacl and setfacl

Rust library to manipulate file system access control lists (ACL) on macOS, Linux, and FreeBSD

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

This is the place to translate Having a documentation in your native language is essential if you don't speak English, and still enjoyable even if...

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder),

enabling remote control support for embedded project

Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder),

Rust library for interaction with the OriginTrail Decentralized Knowledge Graph

open up an issue on this repository and let us know

Rust library for interaction with the OriginTrail Decentralized Knowledge Graph

Rust library for parsing COLLADA files

Notice: This library is built around files exported from Blender 2

Rust library for parsing COLLADA files

Rust library for low-level abstraction of MIPS32 processors

This project is licensed under the terms of the MIT license

Rust library for low-level abstraction of MIPS32 processors
Facebook Instagram Twitter GitHub Dribbble
Privacy