Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A getting started example for rust-prometheus + hyper
extern crate lazy_static;
use hyper::rt::Future;
use hyper::service::service_fn_ok;
use hyper::{Body, Request, Response, Server};
use prometheus::*;
use rand::prelude::*;
lazy_static! {
static ref REQUEST_DURATION: HistogramVec = register_histogram_vec!(
"Histogram of HTTP request duration in seconds",
exponential_buckets(0.005, 2.0, 20).unwrap()
fn thread_simulate_requests() {
let mut rng = rand::thread_rng();
loop {
// Simulate duration 0s ~ 2s
let duration = rng.gen_range(0f64, 2f64);
// Simulate HTTP method
let method = ["GET", "POST", "PUT", "DELETE"].choose(&mut rng).unwrap();
// Record metrics
println!("{}\t{:.3}s", method, duration);
// One request per second
fn metric_service(_req: Request<Body>) -> Response<Body> {
let encoder = TextEncoder::new();
let mut buffer = vec![];
let mf = prometheus::gather();
encoder.encode(&mf, &mut buffer).unwrap();
.header(hyper::header::CONTENT_TYPE, encoder.format_type())
fn main() {
let addr = ([127, 0, 0, 1], 12345).into();
let service = || service_fn_ok(metric_service);
let server = Server::bind(&addr)
.map_err(|e| panic!("{}", e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment