跳到主要内容

Summary

2024年06月17日
柏拉文
越努力,越幸运

一、认识


汇总指标,它和 **Histogram比较相似,也是复合指标,但是用的场景不多,默认的指标中没有使用Summary**的,可能是因为它不支持聚合操作,只能统计单实例的指标。看例子:

const h = new Summary({
name: 'test_summary',
help: 'Example of a summary',
labelNames: ['code'],
percentiles: [0.1, 0.3, 0.4, 0.5, 1],
});
h.labels('200').observe(0.2);
h.labels('200').observe(0.4);

h.labels('200').observe(0.5);
h.labels('200').observe(1);

register.metrics().then(str => console.log(str));

指标上报结果:

# HELP test_summary Example of a summary
# TYPE test_summary summary
test_summary{quantile="0.1",code="200"} 0.2
test_summary{quantile="0.3",code="200"} 0.33999999999999997
test_summary{quantile="0.4",code="200"} 0.41000000000000003
test_summary{quantile="0.5",code="200"} 0.45
test_summary{quantile="1",code="200"} 1
test_summary_sum{code="200"} 2.1
test_summary_count{code="200"} 4

可以看到它也是统计了总数和总的和,但是和histogram的区别是它统计的是百分比的分布情况,比如quantile="0.4"表示的是40%的值小于0.41000000000000003所以它是在客户端经过计算的,不是简单的增加,这种计算就不能够实现多个实例的聚合。而histogram的区间是可以多实例聚合的。