#metrics #instrument #macro

macro function-timer-macro

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

8 releases (5 breaking)

0.6.1 Aug 11, 2024
0.6.0 Jan 19, 2024
0.5.1 Jun 1, 2023
0.5.0 Apr 7, 2023
0.1.0 Mar 1, 2023

#68 in #instrument

Download history 81/week @ 2024-06-30 37/week @ 2024-07-07 15/week @ 2024-07-21 42/week @ 2024-07-28 142/week @ 2024-08-04 273/week @ 2024-08-11 102/week @ 2024-08-18 29/week @ 2024-08-25 11/week @ 2024-09-01 48/week @ 2024-09-08 97/week @ 2024-09-15 257/week @ 2024-09-22 63/week @ 2024-09-29 36/week @ 2024-10-06 18/week @ 2024-10-13

376 downloads per month
Used in function-timer

MIT/Apache

17KB
182 lines

Crate Build Status codecov dependency status Documentation Crate Crate

Function timer

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

Note: with the use of another attribut macro, declaration order might matter. Especially using async-trait, depending on which one is first, you time the actual execution of the function if time macro is declared before, or the creation of the future if it's declared after.

Example

use std::error::Error;
use metrics_exporter_prometheus::PrometheusBuilder;
use function_timer::time;

struct Test {}

impl Test {
    #[time("my_metric")]
    pub fn impl_function(&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<dyn Error>> {
    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
another_metric{function="impl_fail_function",quantile="0"} 0.000000677
another_metric{function="impl_fail_function",quantile="0.5"} 0.0000006770639874327633
another_metric{function="impl_fail_function",quantile="0.9"} 0.0000006770639874327633
another_metric{function="impl_fail_function",quantile="0.95"} 0.0000006770639874327633
another_metric{function="impl_fail_function",quantile="0.99"} 0.0000006770639874327633
another_metric{function="impl_fail_function",quantile="0.999"} 0.0000006770639874327633
another_metric{function="impl_fail_function",quantile="1"} 0.000012062
another_metric_sum{function="impl_fail_function"} 0.000012739000000000001
another_metric_count{function="impl_fail_function"} 2

# TYPE my_metric summary
my_metric{function="free_function",quantile="0"} 0.000005702
my_metric{function="free_function",quantile="0.5"} 0.000005701963063845405
my_metric{function="free_function",quantile="0.9"} 0.000005701963063845405
my_metric{function="free_function",quantile="0.95"} 0.000005701963063845405
my_metric{function="free_function",quantile="0.99"} 0.000005701963063845405
my_metric{function="free_function",quantile="0.999"} 0.000005701963063845405
my_metric{function="free_function",quantile="1"} 0.000005702
my_metric_sum{function="free_function"} 0.000005702
my_metric_count{function="free_function"} 1
my_metric{function="impl_function",quantile="0"} 0.000002602
my_metric{function="impl_function",quantile="0.5"} 0.0000026018182046361393
my_metric{function="impl_function",quantile="0.9"} 0.0000026018182046361393
my_metric{function="impl_function",quantile="0.95"} 0.0000026018182046361393
my_metric{function="impl_function",quantile="0.99"} 0.0000026018182046361393
my_metric{function="impl_function",quantile="0.999"} 0.0000026018182046361393
my_metric{function="impl_function",quantile="1"} 0.000002602
my_metric_sum{function="impl_function"} 0.000002602
my_metric_count{function="impl_function"} 1
my_metric{function="static_function",quantile="0"} 0.000002894
my_metric{function="static_function",quantile="0.5"} 0.0000028939157344447597
my_metric{function="static_function",quantile="0.9"} 0.0000028939157344447597
my_metric{function="static_function",quantile="0.95"} 0.0000028939157344447597
my_metric{function="static_function",quantile="0.99"} 0.0000028939157344447597
my_metric{function="static_function",quantile="0.999"} 0.0000028939157344447597
my_metric{function="static_function",quantile="1"} 0.000002894
my_metric_sum{function="static_function"} 0.000002894
my_metric_count{function="static_function"} 1

It can also be put on an impl block :

use std::error::Error;
use metrics_exporter_prometheus::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<dyn Error>> {
    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
my_metric{struct="Test",function="static_function",quantile="0"} 0.000005976
my_metric{struct="Test",function="static_function",quantile="0.5"} 0.000005976352983111928
my_metric{struct="Test",function="static_function",quantile="0.9"} 0.000005976352983111928
my_metric{struct="Test",function="static_function",quantile="0.95"} 0.000005976352983111928
my_metric{struct="Test",function="static_function",quantile="0.99"} 0.000005976352983111928
my_metric{struct="Test",function="static_function",quantile="0.999"} 0.000005976352983111928
my_metric{struct="Test",function="static_function",quantile="1"} 0.000005976
my_metric_sum{struct="Test",function="static_function"} 0.000005976
my_metric_count{struct="Test",function="static_function"} 1
my_metric{struct="Test",function="impl_fail_function",quantile="0"} 0.000000771
my_metric{struct="Test",function="impl_fail_function",quantile="0.5"} 0.0000007710596865495025
my_metric{struct="Test",function="impl_fail_function",quantile="0.9"} 0.0000007710596865495025
my_metric{struct="Test",function="impl_fail_function",quantile="0.95"} 0.0000007710596865495025
my_metric{struct="Test",function="impl_fail_function",quantile="0.99"} 0.0000007710596865495025
my_metric{struct="Test",function="impl_fail_function",quantile="0.999"} 0.0000007710596865495025
my_metric{struct="Test",function="impl_fail_function",quantile="1"} 0.00000257
my_metric_sum{struct="Test",function="impl_fail_function"} 0.000003341
my_metric_count{struct="Test",function="impl_fail_function"} 2
my_metric{struct="Test",function="impl_function",quantile="0"} 0.000003853
my_metric{struct="Test",function="impl_function",quantile="0.5"} 0.000003852839894857494
my_metric{struct="Test",function="impl_function",quantile="0.9"} 0.000003852839894857494
my_metric{struct="Test",function="impl_function",quantile="0.95"} 0.000003852839894857494
my_metric{struct="Test",function="impl_function",quantile="0.99"} 0.000003852839894857494
my_metric{struct="Test",function="impl_function",quantile="0.999"} 0.000003852839894857494
my_metric{struct="Test",function="impl_function",quantile="1"} 0.000003853
my_metric_sum{struct="Test",function="impl_function"} 0.000003853
my_metric_count{struct="Test",function="impl_function"} 1

Dependencies

~260–710KB
~17K SLoC