Crate Build Status codecov dependency status Documentation Crate Crate

Function timer

Macro that allow to time a function and emit a histogram metric using metrics crate.

Example

```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");
}

}

[time("my_metric")]

pub fn free_function() { println!("This a test"); }

fn main() -> Result<(), Box> { let builder = PrometheusBuilder::new(); let handle = builder.install_recorder()?;

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

TYPE another_metric summary

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

TYPE my_metric summary

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 ```

It can also be put on an impl block :

```rust use std::error::Error; use metricsexporterprometheus::PrometheusBuilder; use function_timer::time;

struct Test {}

[time("my_metric")]

impl Test { pub fn impl_function(&self) { println!("This another test"); }

pub fn impl_fail_function(&self, text:&str) -> Result<(), Box<dyn Error>>{
    let number:usize = text.parse()?;
    println!("{number}");

    Ok(())
}

pub fn static_function() {
    println!("This another test");
}

}

fn main() -> Result<(), Box> { let builder = PrometheusBuilder::new(); let handle = builder.install_recorder()?;

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(())

} ```

It will output :

``` This another test This another test 1

TYPE my_metric summary

mymetric{struct="Test",function="staticfunction",quantile="0"} 0.000005976 mymetric{struct="Test",function="staticfunction",quantile="0.5"} 0.000005976352983111928 mymetric{struct="Test",function="staticfunction",quantile="0.9"} 0.000005976352983111928 mymetric{struct="Test",function="staticfunction",quantile="0.95"} 0.000005976352983111928 mymetric{struct="Test",function="staticfunction",quantile="0.99"} 0.000005976352983111928 mymetric{struct="Test",function="staticfunction",quantile="0.999"} 0.000005976352983111928 mymetric{struct="Test",function="staticfunction",quantile="1"} 0.000005976 mymetricsum{struct="Test",function="staticfunction"} 0.000005976 mymetriccount{struct="Test",function="staticfunction"} 1 mymetric{struct="Test",function="implfailfunction",quantile="0"} 0.000000771 mymetric{struct="Test",function="implfailfunction",quantile="0.5"} 0.0000007710596865495025 mymetric{struct="Test",function="implfailfunction",quantile="0.9"} 0.0000007710596865495025 mymetric{struct="Test",function="implfailfunction",quantile="0.95"} 0.0000007710596865495025 mymetric{struct="Test",function="implfailfunction",quantile="0.99"} 0.0000007710596865495025 mymetric{struct="Test",function="implfailfunction",quantile="0.999"} 0.0000007710596865495025 mymetric{struct="Test",function="implfailfunction",quantile="1"} 0.00000257 mymetricsum{struct="Test",function="implfailfunction"} 0.000003341 mymetriccount{struct="Test",function="implfailfunction"} 2 mymetric{struct="Test",function="implfunction",quantile="0"} 0.000003853 mymetric{struct="Test",function="implfunction",quantile="0.5"} 0.000003852839894857494 mymetric{struct="Test",function="implfunction",quantile="0.9"} 0.000003852839894857494 mymetric{struct="Test",function="implfunction",quantile="0.95"} 0.000003852839894857494 mymetric{struct="Test",function="implfunction",quantile="0.99"} 0.000003852839894857494 mymetric{struct="Test",function="implfunction",quantile="0.999"} 0.000003852839894857494 mymetric{struct="Test",function="implfunction",quantile="1"} 0.000003853 mymetricsum{struct="Test",function="implfunction"} 0.000003853 mymetriccount{struct="Test",function="impl_function"} 1 ```