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 性能分析
每月1,211次下载
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百分位数。
- 根据配置在本地聚合为一个
Histogram
或ExponentialHistogram
。
指标.总和("名称",值)
将每个报告期内“名称”的所有总和调用加起来。
- 用于跟踪计数器,如存储在磁盘上的字节数或消耗的令牌数。
- 在本地聚合为一个
Sum
。
let_guard=指标.时间("名称")
- 用于测量代码给定作用域内某物所花费的时间。
- 根据配置在本地聚合为一个
Histogram
或ExponentialHistogram
。 - 计时器的单位是纳秒。
开发
使用cargo ws version minor
更新版本。
依赖项
16–26MB
~467K SLoC