如果仪表可以充当计数器,为什么普罗米修斯既有计数器又有仪表? [英] Why there are both counters and gauges in Prometheus if gauges can act as counters?

查看:187
本文介绍了如果仪表可以充当计数器,为什么普罗米修斯既有计数器又有仪表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在确定CounterGauge之间时,

要在计数器和量规之间进行选择,有一个简单的经验法则: 该值可以下降,这是一个量表.计数器只能上升(并且 重置,例如进程重新启动时.

To pick between counter and gauge, there is a simple rule of thumb: if the value can go down, it is a gauge. Counters can only go up (and reset, such as when a process restarts).

它们似乎涵盖了重叠的用例:您可以使用只会不断增加的量规.那么,为什么还要首先创建计数器"指标类型呢?为什么不简单地对两者都使用量规呢?

They seem to cover overlapping use cases: you could use a Gauge that only ever increases. So why even create the Counter metric type in the first place? Why don't you simply use Gauges for both?

推荐答案

从概念上讲,仪表和计数器的目的不同

From a conceptual point of view, gauge and counter have different purposes

  • 量规通常代表一种状态,通常是为了检测饱和度.
  • 计数器的绝对值不是很有意义,真正的目的是使用irate/rate()increase() ...
  • 之类的函数来计算演化(通常是利用率).
  • a gauge typically represent a state, usually with the purpose of detecting saturation.
  • the absolute value of a counter is not really meaningful, the real purpose is rather to compute an evolution (usually a utilization) with functions like irate/rate(), increase() ...

这些演化操作需要可靠地计算量规无法达到的增长,因为您需要检测该值的重置.

Those evolution operations requires a reliable computation of the increase that you could not achieve with a gauge because you need to detect resets of the value.

从技术上讲,计数器具有两个重要属性:

Technically, a counter has two important properties:

  1. 它总是从0开始
  2. 它总是增加(即代码中增加)

如果应用程序在两次Prometheus刮擦之间重新启动,则第二个刮擦的值可能小于前一个刮擦的值,并且可以恢复增加的值(之所以这样,是因为您总是会在最后一个刮擦和重置之间松开增加的值).

If the application restarts between two Prometheus scrapes, the value of the second scrape in likely to be less than the previous scrape and the increase can be recovered (somewhat because you'll always loose the increase between the last scrape and the reset).

一种简单的算法来计算从t1到t2的废料之间的计数增加:

A simple algorithm to compute the increase of counter between scrapes from t1 to t2 is:

  • 如果counter(t2) >= counter(t1),则increase=counter(t2)-counter(t1)
  • 如果counter(2) < counter(t1)然后increase=counter(t2)
  • if counter(t2) >= counter(t1) then increase=counter(t2)-counter(t1)
  • if counter(2) < counter(t1)then increase=counter(t2)

结论是,从技术角度来看,您可以使用仪表而不是计数器,前提是您在启动时将其重置为0,并且仅将其递增,但是任何违反合同的行为都将导致错误的值.

As a conclusion, from a technical point of view, you can use a gauge instead of a counter provided you reset it to 0 at startup and only increment it but any violation of contract will lead to wrong values.

作为旁注,我还希望计数器实现使用无符号整数表示,而gauge则宁愿使用浮点表示.这对代码有一些小的影响,例如能够自动溢出到0的能力以及更好地支持当前cpus上的原子操作.

As a side note, I also expect a counter implementation to use unsigned integer representation while gauge will rather use a floating point representation. This has some minor impacts on the code such as the ability to overflow to 0 automatically and better support for atomic operations on current cpus.

这篇关于如果仪表可以充当计数器,为什么普罗米修斯既有计数器又有仪表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆