web3-proxy/web3_proxy/examples/metrics.rs

33 lines
792 B
Rust
Raw Normal View History

2022-09-09 00:01:36 +03:00
use metered::{metered, HitCount, Throughput};
use serde::Serialize;
2022-11-12 09:11:58 +03:00
use thread_fast_rng::{rand::Rng, thread_fast_rng};
2022-09-09 00:01:36 +03:00
#[derive(Default, Debug, Serialize)]
pub struct Biz {
metrics: BizMetrics,
}
#[metered(registry = BizMetrics)]
impl Biz {
#[measure([HitCount, Throughput])]
pub fn biz(&self) {
2022-11-12 09:11:58 +03:00
let delay = std::time::Duration::from_millis(thread_fast_rng().gen::<u64>() % 200);
2022-09-09 00:01:36 +03:00
std::thread::sleep(delay);
}
}
fn main() {
let buz = Biz::default();
for _ in 0..100 {
buz.biz();
}
let mut globals = std::collections::HashMap::new();
globals.insert("service", "web3_proxy_prometheus_example");
let serialized = serde_prometheus::to_string(&buz.metrics, Some("example"), globals).unwrap();
println!("{}", serialized);
}