#metrics #performance #service #default-value

goodmetrics

无限制基数,快速指标记录 - 适用于服务

38个稳定版本 (5个主要版本)

6.0.0 2024年7月30日
5.0.4 2024年7月15日
5.0.3 2024年6月7日
4.1.0 2024年5月30日
1.0.3 2022年11月30日

#17 in 性能分析

Download history 255/week @ 2024-04-22 164/week @ 2024-04-29 186/week @ 2024-05-06 187/week @ 2024-05-13 76/week @ 2024-05-20 255/week @ 2024-05-27 671/week @ 2024-06-03 189/week @ 2024-06-10 67/week @ 2024-06-17 197/week @ 2024-06-24 163/week @ 2024-07-01 164/week @ 2024-07-08 382/week @ 2024-07-15 271/week @ 2024-07-22 393/week @ 2024-07-29 157/week @ 2024-08-05

每月1,211次下载

Apache-2.0

255KB
5K SLoC

Goodmetrics: Rust

关于

这是Rust的goodmetrics客户端。它包含了一个OpenTelemetry协议的下游和像PooledMetricsAllocator这样的性能工具。要使用任何grpc下游(goodmetrics或opentelemetry),你需要一个tokio运行时。

如何使用

请参阅lightstep演示,以获取使用opentelemetry的完整设置和使用示例。

简单示例

一旦你有了配置好的MetricsFactory,你使用Metrics的方式不会随着配置的下游(们)的后续更新而改变

let mut metrics = metrics_factory.record_scope("demo"); // By default, includes a "demo_totaltime" histogram measurement, capturing the time it took to complete the unit of work
{
    let _scope = metrics.time("timed_delay"); // you can time additional scopes
    my_timed_delay();
}
metrics.measurement("ran", 1); // measurements can be plain numbers; when preaggregated they are StatisticSets (min/max/sum/count)
metrics.sum("runs_count", 1); // If you do not need a StatisticSet but rather a simple counter,
                              // sum will produce a simple Gauge
metrics.dimension("mod", i % 8); // you can add dimensions to a Metrics whenever you want. All measurements in this Metrics record are dimensioned by this value.
metrics.distribution("some_continuous_value", instantaneous_network_bandwidth); // histograms are aggregated sparsely, and truncated to 2 significant figures (base 10).

一旦metrics对象离开作用域,它就会将指标导出到你希望的数据源。如果你需要在某个立即的点发布指标,你可以手动drop()对象。

记录范围而不测量时间

并非每个工作单元都需要测量其完成所需的时间。只需通过调用record_scope_with_behavior并传入你希望的行为来创建一个指标对象。

例如

let mut metrics = metrics_factory.record_scope_with_behavior(
    "demo",
    MetricsBehavior::SuppressTotalTime,
);

创建一个具有默认值的维度

在一个分布式系统中,一个工作单元可能不会完全完成,并且metrics对象将离开作用域,导致任何现有的维度或度量被导出。如果期望存在维度但不存在,这可能会导致令人困惑的行为。

使用guarded_dimension可以提供一个默认值,并在对象在预期之前被丢弃时导出它

let mut metrics = metrics_factory.record_scope("demo");
let result_dimension = metrics.guarded_dimension("my_api_result", "dropped_early");

// Perform work...
let result = serve_api_request(request);

match result {
    Ok(_) => {
        // Explicitly set the guarded dimension and overwrite the default value
        result_dimension.set("ok")
    },
    Err(_e) => {
        // Explicitly set the guarded dimension and overwrite the default value
        result_dimension.set("error");
        metrics.sum("errors", 1_u64)
    }
}

可用的指标函数及其使用场景

最新文档始终可在docs.rs找到。

指标.测量("名称",)

  • 当您只需要接收到的数据点的最小值、最大值、总和和计数时使用。
  • 如果只需要总和或计数,则使用sum()代替。
  • 如果需要值百分位数,则使用distribution()代替。
  • 在本地聚合为一个StatisticsSet

指标.分布("名称",)

  • 用于识别数据的百分位数,如请求大小的第99百分位数。
  • 根据配置在本地聚合为一个HistogramExponentialHistogram

指标.总和("名称",)

将每个报告期内“名称”的所有总和调用加起来。

  • 用于跟踪计数器,如存储在磁盘上的字节数或消耗的令牌数。
  • 在本地聚合为一个Sum

let_guard=指标.时间("名称")

  • 用于测量代码给定作用域内某物所花费的时间。
  • 根据配置在本地聚合为一个HistogramExponentialHistogram
  • 计时器的单位是纳秒。

开发

使用cargo ws version minor更新版本。

依赖项

16–26MB
~467K SLoC