简体   繁体   English

Python prometheus_client,CollectorRegistry 中的重复时间序列

[英]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?我可以这样做吗?

Background:背景:

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.

相关问题 运行普罗米修斯(python)时收集器注册表中的重复时间序列 - Duplicated timeseries in CollectorRegistry when running prometheus (python) python prometheus_client guage 指标未保存在 prometheus 中 - python prometheus_client guage metrics are not saved in prometheus 使用 prometheus_client 向 Prometheus 公开包指标 - Expose package metrics to Prometheus with prometheus_client 使用 Flask 的 prometheus_client 自定义指标 - prometheus_client custom metrics with Flask 使用 python prometheus_client 将 f5sdk 池节点详细信息推送到 Prometheus - push f5sdk pool node details to Prometheus using python prometheus_client Python。 prometheus_client ValueError:仪表指标缺少标签值 - Python. prometheus_client ValueError: gauge metric is missing label values 当我尝试导入存储孩子的字典时,我收到 ValueError: Duplicated timeseries in CollectorRegistry 错误 - I get the ValueError: Duplicated timeseries in CollectorRegistry error, when i try to import the Dict where the childs are stored ValueError:时间序列已存在于CollectorRegistry中 - ValueError: Timeseries already present in CollectorRegistry 是否有一个arg可以在start_http_server(prometheus_client)而非/中设置其他端点? - Is there an arg to set other endpoint in start_http_server (prometheus_client) instead of /? Prometheus Python 客户端库 - Prometheus Python client library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM