具有 "cached" 指标的普罗米修斯自定义收集器
prometheus custom collector with "cached" metrics
我想要一个普罗米修斯收集器,它可以通过 9100 端口上的 http 进行查询,它正在提供一个指标。 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() 后都有一个 "reset",而不是我创建的各个指标总是只是添加到我的注册表中。
有没有办法删除之前添加的所有指标?
重要的是,在 100 秒之间,start_http_server(9100, addr='0.0.0.0') 仍然可用,并提供它从最后一个 checker() 运行 获得的最新指标数据。
在我的演示文件中,我总是希望只有一个带有随机生成的标签名称的指标。
而不是使用内置 Gauge, you can use a custom collector。
请注意,它不会在 Prometheus 中拯救您:
- 每次 prometheus 抓取您的应用程序时,它都会创建一个新指标
- 之前的指标在 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 的实现发生变化,它就会中断。
我想要一个普罗米修斯收集器,它可以通过 9100 端口上的 http 进行查询,它正在提供一个指标。 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() 后都有一个 "reset",而不是我创建的各个指标总是只是添加到我的注册表中。 有没有办法删除之前添加的所有指标? 重要的是,在 100 秒之间,start_http_server(9100, addr='0.0.0.0') 仍然可用,并提供它从最后一个 checker() 运行 获得的最新指标数据。 在我的演示文件中,我总是希望只有一个带有随机生成的标签名称的指标。
而不是使用内置 Gauge, you can use a custom collector。
请注意,它不会在 Prometheus 中拯救您:
- 每次 prometheus 抓取您的应用程序时,它都会创建一个新指标
- 之前的指标在 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 的实现发生变化,它就会中断。