use std::thread;
use std::time::Duration;
use nm::{Event, MetricsPusher, Push, Report};
const BATCH_SIZE: usize = 10;
const BAGEL_WEIGHT_GRAMS: i64 = 100;
fn main() {
if std::env::var("IS_TESTING").is_ok() {
println!("Running in testing mode - exiting immediately to prevent infinite loop");
return;
}
thread::spawn(move || {
loop {
let report = Report::collect();
println!("## Latest published data:");
println!("{report}");
thread::sleep(Duration::from_secs(3));
}
});
loop {
println!("Cooking a batch of {BATCH_SIZE} bagels...");
for _ in 0..BATCH_SIZE {
thread::sleep(Duration::from_secs(1));
BAGELS_COOKED_WEIGHT_GRAMS.with(|x| x.observe(BAGEL_WEIGHT_GRAMS));
}
METRICS_PUSHER.with(MetricsPusher::push);
}
}
thread_local! {
static METRICS_PUSHER: MetricsPusher = MetricsPusher::new();
static BAGELS_COOKED_WEIGHT_GRAMS: Event<Push> = Event::builder()
.name("bagels_cooked_weight_grams")
.pusher_local(&METRICS_PUSHER)
.build();
}