具有“已缓存”指标的Prometheus自定义收集器

时间:2020-05-09 15:42:24

标签: python prometheus

我想要一个可以通过提供度量标准的端口9100上的http查询的prometheus收集器。 curl应该总是只提供我正在使用我的函数创建的最新指标:

https://github.com/unnuetz/custom-prometheus-collector/blob/master/check.py#L5-L11

我现在的问题是,在每个100秒的周期之后,我在此处添加: https://github.com/unnuetz/custom-prometheus-collector/blob/master/main.py#L20-L23 我要创建的下一个度量标准已添加,但是我希望在每次调用checker()之后都有一个“重置”,而不是我创建的各个度量标准始终只是添加到我的允许说的注册表中。 有没有办法删除之前添加的所有指标? 重要的一点是,在100秒内仍可以使用start_http_server(9100,addr ='0.0.0.0')并传递上一次运行checker()时获得的最新度量标准数据。 在我的演示文件中,我总是只想拥有一个带有随机生成的标签名称的指标。

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用Gauge,而不是使用内置的custom collector

请注意,它不会将您保存在Prometheus中:

  1. 每次prometheus抓取您的应用程序时,它将创建一个新指标
  2. 以前的指标在Prometheus中仍然可见,直到被标记为陈旧(〜5分钟)为止。

如果不能选择使用自定义收集器,因为您需要使用 global 指标显示一些代码。您可以使用绑定方法注入类似于remove()函数的reset()函数。

import CollectorRegistry from prometheus_client.registry

def _resetCollector(self):
    with self._lock:
        # reset whatever name / value you want
        pass

CollectorRegistry.reset = _resetCollector


# wherever you want to reset it
import REGISTRY from prometheus_client.registry
REGISTRY.reset()

当然,只要CollectorRegistry的实现发生更改,它就会中断。