[英]Python prometheus_client, Duplicated timeseries in CollectorRegistry
Using the Python Prometheus client, I'm trying add a metric with differing labels to the same collector registry.使用 Python Prometheus 客户端,我正在尝试将具有不同标签的指标添加到同一个收集器注册表。
My goal is to essentially collect together multiple metrics in one CollectorRegistry() object and push them all at once to push gateway.我的目标基本上是在一个 CollectorRegistry() object 中收集多个指标,并同时将它们全部推送到推送网关。 Ex:
前任:
my_metric_name{name="foo",tag="1"} 1.0
my_metric_name{name="foo",tag="2"} 5.0
my_metric_name{name="foo",tag="3"} 7.0
From that above set I want to push them all at once from one registry, rather than creating a new registry for each one.从上述设置中,我想从一个注册表一次全部推送它们,而不是为每个注册表创建一个新注册表。 Can I do that?
我可以这样做吗?
Consider this working snippet:考虑这个工作片段:
#!/usr/bin/env python3
from prometheus_client import CollectorRegistry, Gauge
registry = CollectorRegistry()
g = Gauge("test", "some gauge",
labelnames=["name", "tag"],
registry=registry).labels("foo", 1)
g.set(1)
Which creates a Gauge metric like this:这会创建一个像这样的 Gauge 度量:
test{name="foo",tag="1"} 1.0
However, this does not work:但是,这不起作用:
#!/usr/bin/env python3
from prometheus_client import CollectorRegistry, Gauge
title = "foo"
dataset = [1, 2, 3]
registry = CollectorRegistry()
for item in dataset:
g = Gauge("test", "some gauge",
labelnames=["name", "tag"],
registry=registry).labels(title, item)
g.set(1)
I was expecting it to create something like this, with the labels making unique items (not duplicates):我期待它创造出这样的东西,标签制作独特的物品(不重复):
test{name="foo",tag="1"} 1.0
test{name="foo",tag="2"} 1.0
test{name="foo",tag="3"} 1.0
Instead it fails on the second loop with this error:相反,它在第二个循环中失败并出现以下错误:
ValueError: Duplicated timeseries in CollectorRegistry
Wouldn't the different label names make them not duplicates?不同的 label 名称不会使它们不重复吗?
I can work around this by pushing the metrics to push gateway more often (on every loop iteration) and creating a new registry.我可以通过更频繁地推送指标来推送网关(在每次循环迭代中)并创建一个新注册表来解决这个问题。 But it would really be helpful to package up a bunch of metrics and send them all at once instead of making many calls over the network.
但这确实有助于 package 收集一堆指标并一次发送它们,而不是通过网络进行多次调用。
I came to the realization that you only need to declare the Gauge object once, then each time you call set() on it you'll get a new metric added to the registry:我意识到您只需要声明 Gauge object 一次,然后每次调用 set() 时,都会在注册表中添加一个新指标:
#!/usr/bin/env python3
from prometheus_client import CollectorRegistry, Gauge
title = "foo"
dataset = [1, 2, 3]
registry = CollectorRegistry()
g = Gauge("test", "some gauge",
labelnames=["name", "tag"],
registry=registry)
for item in dataset:
g.labels(title, item).set(1)
Results in:结果是:
test{name="foo",tag="1"} 1.0
test{name="foo",tag="2"} 1.0
test{name="foo",tag="3"} 1.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.