自值为零以来的时间

Time since a value was zero

我有一个应用程序使用来自 AWS 主题的工作。每天多次添加工作,我的应用程序快速消耗它,队列长度恢复为 0。我能够生成队列长度的指标。

我想要一个时间指标,因为队列长度最后为零。对如何开始有任何想法吗?

假设一个 queue_size 记录队列大小的量规,您可以这样定义一个记录规则:

# Timestamp of the most recent `queue_size` == 0 sample; else propagate the previous value
- record: last_empty_queue_timestamp
  expr: timestamp(queue_size == 0) or last_empty_queue_timestamp

然后您可以简单地计算自上次队列为空以来的时间:

timestamp(queue_size) - last_empty_queue_timestamp

但是请注意,因为这是一个量表(并且由于采样的限制),您最终可能会得到奇怪的结果。例如。如果每分钟添加一个工作项,您的采样间隔为一分钟,并且您恰好在添加工作项后进行采样,那么从普罗米修斯的角度来看,您的队列可能永远不会(或很少)显示为空。如果这是一个问题(或仅仅是一个问题),您最好让您的应用程序导出一个指标,该指标是将某些内容添加到空队列时的最后一个时间戳(基本上是记录的规则尝试计算的内容)。

与Alin的回答类似;重新审视这个问题后,我从普罗米修斯文档中发现了这一点:

https://prometheus.io/docs/practices/instrumentation/#timestamps,-not-time-since

If you want to track the amount of time since something happened, export the Unix timestamp at which it happened - not the time since it happened. With the timestamp exported, you can use the expression time() - my_timestamp_metric to calculate the time since the event, removing the need for update logic and protecting you against the update logic getting stuck.