Macro that allow to time a function and emit a histogram metric using metrics crate.
```rust use std::error::Error; use metricsexporterprometheus::PrometheusBuilder; use function_timer::time;
struct Test {}
impl Test { #[time("mymetric")] pub fn implfunction(&self) { println!("This another test"); }
#[time("another_metric")]
pub fn impl_fail_function(&self, text:&str) -> Result<(), Box<dyn Error>>{
let number:usize = text.parse()?;
println!("{number}");
Ok(())
}
#[time("my_metric")]
pub fn static_function() {
println!("This another test");
}
}
pub fn free_function() { println!("This a test"); }
fn main() -> Result<(), Box
free_function();
Test::static_function();
let t = Test {};
t.impl_function();
let result = t.impl_fail_function("azerty");
assert!(result.is_err());
let result = t.impl_fail_function("1");
assert!(result.is_ok());
println!("{}", handle.render());
Ok(())
} ```
Output : ``` This a test This another test This another test 1
anothermetric{function="implfailfunction",quantile="0"} 0.000000677 anothermetric{function="implfailfunction",quantile="0.5"} 0.0000006770639874327633 anothermetric{function="implfailfunction",quantile="0.9"} 0.0000006770639874327633 anothermetric{function="implfailfunction",quantile="0.95"} 0.0000006770639874327633 anothermetric{function="implfailfunction",quantile="0.99"} 0.0000006770639874327633 anothermetric{function="implfailfunction",quantile="0.999"} 0.0000006770639874327633 anothermetric{function="implfailfunction",quantile="1"} 0.000012062 anothermetricsum{function="implfailfunction"} 0.000012739000000000001 anothermetriccount{function="implfail_function"} 2
mymetric{function="freefunction",quantile="0"} 0.000005702 mymetric{function="freefunction",quantile="0.5"} 0.000005701963063845405 mymetric{function="freefunction",quantile="0.9"} 0.000005701963063845405 mymetric{function="freefunction",quantile="0.95"} 0.000005701963063845405 mymetric{function="freefunction",quantile="0.99"} 0.000005701963063845405 mymetric{function="freefunction",quantile="0.999"} 0.000005701963063845405 mymetric{function="freefunction",quantile="1"} 0.000005702 mymetricsum{function="freefunction"} 0.000005702 mymetriccount{function="freefunction"} 1 mymetric{function="implfunction",quantile="0"} 0.000002602 mymetric{function="implfunction",quantile="0.5"} 0.0000026018182046361393 mymetric{function="implfunction",quantile="0.9"} 0.0000026018182046361393 mymetric{function="implfunction",quantile="0.95"} 0.0000026018182046361393 mymetric{function="implfunction",quantile="0.99"} 0.0000026018182046361393 mymetric{function="implfunction",quantile="0.999"} 0.0000026018182046361393 mymetric{function="implfunction",quantile="1"} 0.000002602 mymetricsum{function="implfunction"} 0.000002602 mymetriccount{function="implfunction"} 1 mymetric{function="staticfunction",quantile="0"} 0.000002894 mymetric{function="staticfunction",quantile="0.5"} 0.0000028939157344447597 mymetric{function="staticfunction",quantile="0.9"} 0.0000028939157344447597 mymetric{function="staticfunction",quantile="0.95"} 0.0000028939157344447597 mymetric{function="staticfunction",quantile="0.99"} 0.0000028939157344447597 mymetric{function="staticfunction",quantile="0.999"} 0.0000028939157344447597 mymetric{function="staticfunction",quantile="1"} 0.000002894 mymetricsum{function="staticfunction"} 0.000002894 mymetriccount{function="staticfunction"} 1 ```