Skip to content

Instantly share code, notes, and snippets.

@breezewish
Created February 2, 2019 13:09
Show Gist options
  • Select an option

  • Save breezewish/bb10bccd13a7fe332ef534ff0306ceb5 to your computer and use it in GitHub Desktop.

Select an option

Save breezewish/bb10bccd13a7fe332ef534ff0306ceb5 to your computer and use it in GitHub Desktop.

Revisions

  1. breezewish created this gist Feb 2, 2019.
    58 changes: 58 additions & 0 deletions prometheus-sample.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #[macro_use]
    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!(
    "http_requests_duration",
    "Histogram of HTTP request duration in seconds",
    &["method"],
    exponential_buckets(0.005, 2.0, 20).unwrap()
    )
    .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);
    REQUEST_DURATION
    .with_label_values(&[method])
    .observe(duration);
    // One request per second
    std::thread::sleep(std::time::Duration::from_secs(1));
    }
    }

    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();
    Response::builder()
    .header(hyper::header::CONTENT_TYPE, encoder.format_type())
    .body(Body::from(buffer))
    .unwrap()
    }

    fn main() {
    std::thread::spawn(thread_simulate_requests);

    let addr = ([127, 0, 0, 1], 12345).into();
    let service = || service_fn_ok(metric_service);
    let server = Server::bind(&addr)
    .serve(service)
    .map_err(|e| panic!("{}", e));

    hyper::rt::run(server);
    }