#metrics #macro #instrument #execution-time

function-timer

宏,允许使用 metrics 库计时一个函数并发布一个指标

8 个版本 (重大更新)

0.8.0 2024年6月3日
0.7.0 2024年1月19日
0.6.0 2023年4月17日
0.5.0 2023年4月7日
0.1.0 2023年3月1日

#86性能分析

Download history 1/week @ 2024-04-19 142/week @ 2024-05-31 26/week @ 2024-06-07 2/week @ 2024-06-14 21/week @ 2024-06-21 51/week @ 2024-06-28 78/week @ 2024-07-05 3/week @ 2024-07-12 12/week @ 2024-07-19 37/week @ 2024-07-26 93/week @ 2024-08-02

每月150 次下载

MIT/Apache

18KB
52

Crate Build Status codecov dependency status Documentation Crate Crate

函数计时器

宏,允许使用 metrics crate 计时一个函数并发布一个直方图指标。

注意:使用另一个属性宏时,声明顺序可能很重要。特别是使用 async-trait 时,根据哪一个先声明,如果在 time 宏之前声明,则计时函数的实际执行时间,如果在之后声明,则计时 future 的创建。

示例

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

输出

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

它也可以放在一个 impl 块中

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

它将输出

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

依赖项

~1–1.7MB
~31K SLoC