5 个版本 (3 个破坏性更新)

0.4.0 2021 年 9 月 2 日
0.3.0 2021 年 9 月 2 日
0.2.1 2021 年 8 月 26 日
0.2.0 2021 年 8 月 26 日
0.1.0 2021 年 7 月 14 日

#206 in 性能分析

Apache-2.0

26KB
440

度量目录

github crates.io docs.rs build status

这个库提供了一种从结构定义中自动推导度量目录的方法。它生成一个遵循根结构相同层次结构的模块和度量键的层次集合。这允许用户与 metrics 进行交互,而无需担心拼写错误或维护外部度量键列表。此外,还可以直接与度量进行交互,而无需使用 metrics 框架。

[dependencies]
metrics-catalogue = "0.1"

示例

use metric_catalogue::{Metrics, Counter, Gauge};

#[derive(Catalogue)]
#[metric(root)]
struct Foo {
  my_counter: Counter,
  my_gauge: Gauge,
  my_bar: Bar,
}

#[derive(Catalogue)]
struct Bar {
  my_counter: Counter,
}

生成以下度量键目录

#[allow(non_camel_case_types)]
pub mod catalogue {
  pub const MY_COUNTER: &str = "my_counter";
  pub const MY_GAUGE: &str = "my_gauge";
  
  #[allow(non_camel_case_types)]
  pub mod bar {
    pub const MY_COUNTER: &str = "bar.my_counter";
  }
}

允许更新相关度量而不会出现潜在的错误拼写

fn my_function() {
  metrics::increment_counter!(catalogue::my_sub::MY_COUNTER);
}

支持的度量类型

目前支持以下度量类型

  • Counter 一个单调递增的 AtomicU64 度量
  • Gauge 一个基于 AtomicU64 的度量,允许使用实际单位进行任意更新、增加和减少。
  • DiscreteGauge 一个基于 AtomicU64 的度量,允许使用离散单位进行任意更新、增加和减少。

度量注册表

添加 Catalogue 推导将实现所有相关结构的 Registry 特性

pub trait Registry {
    /// Find a registered counter under the provided name
    fn find_counter(&self, name: &str) -> Option<&Counter>;
    /// Find a registered gauge under the provided name
    fn find_gauge(&self, name: &str) -> Option<&Gauge>;
}

允许自动分层查找生成的目录。

度量记录器

Catalogue 推导还将实现针对 root 结构的 Recorder 特性

impl Recorder for Foo {
        fn register_counter(&self, _key: &Key, _unit: Option<Unit>, _desc: Option<&'static str>) {}
        fn register_gauge(&self, _key: &Key, _unit: Option<Unit>, _desc: Option<&'static str>) {}
        fn register_histogram(&self, _key: &Key, _unit: Option<Unit>, _desc: Option<&'static str>) {}
        fn record_histogram(&self, _key: &Key, _value: f64) {}
        fn increment_counter(&self, key: &Key, value: u64) {
            if let Some(metric) = self.find_counter(key.name()) {
                metric.increment(value);
            }
        }
        fn update_gauge(&self, key: &Key, value: GaugeValue) {
            if let Some(metric) = self.find_gauge(key.name()) {
                match value {
                    GaugeValue::Increment(val) => metric.increase(val),
                    GaugeValue::Decrement(val) => metric.decrease(val),
                    GaugeValue::Absolute(val) => metric.set(val),
                }
            }
        }
    }

详细信息

  • 必须通过使用 root 属性声明单个 root 结构。如果没有根,则不会生成目录。

  • 可以使用 skip 属性隐藏字段以从目录中排除。例如。

    #[derive(Catalogue)]
    struct Foo {
      #[metric(skip)]
      my_hidden_field: Counter,
    }
    

    这将防止生成 MY_HIDDEN_FIELD -> "my_hidden_field" 键及其相关的注册表条目。

  • Catalogue 宏仅限于 struct

许可证

版权所有 2021 Sam De Roeck

根据Apache许可证版本2.0(“许可证”);除非遵守许可证,否则不得使用此文件。您可以在以下位置获取许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言规定许可和限制,请参阅许可证。

依赖项

~5–15MB
~183K SLoC