我可以在Python中设置Prometheus标签的默认值吗?

huangapple go评论70阅读模式
英文:

Can I set a default value of Prometheus labels in python?

问题

我正在使用官方的Python(2.7)客户端。

我想要定义一个带有一些标签的指标,但并不总是有所有的标签要发送。当我只发送其中一些时,我会得到这个错误:

AttributeError: 'Counter' 对象没有 '_value' 属性

这是我使用的代码:

c = Counter("counterTest, explain this counter, labelnames=("label1", "label2",), namespace="namespace")
c.labels(label1="1").inc(1)

这是Python库的一个限制吗?或者可能是Prometheus的限制?

英文:

I'm using the official python (2.7) client.

I want to define a metric with some labels but I don't always have them all the labels to send. When I send only some of them I get the error:

> AttributeError: 'Counter' object has no attribute '_value'

This is the code I used:

c = Counter("counterTest, "explain this counter, labelnames=("label1", "label2",), namespace="namespace") 
c.labels(label1="1").inc(1)

Is this a limitation in the python library? Or maybe it's a limitation on the Prometheus end?

答案1

得分: 4

你必须始终指定所有的标签,否则我们怎么知道您想要增加哪个系列呢?您可以指定一个空字符串作为标签值,尽管这可能会让您的用户感到困惑。

英文:

You must always specify all labels, how else would we know which series it is that you want to increment? You can specify an empty string as a label value, though this may cause confusion for your users.

答案2

得分: 4

Global setting Yes (i.e. set only once at the time of Counter creation) -- but optionally Overridable later No.

即全局设置 (即在计数器创建时仅设置一次)-- 但稍后可选择覆盖

i.e. I can set a label once at Counter init ... but I will NOT be able to override at actual inc call.

也就是说,我可以在计数器初始化时设置标签一次...但我将无法在实际的增加调用时覆盖它。

Set 'labelValues' when creating your Counter.

在创建计数器时设置 'labelValues'。

counter = Counter(
name="some name", documentation="some doc",
labelnames=['label1', 'label2', 'label3'], labelvalues=['label 1 global value'])

counter.labels(label2='some value', label3='some value2').inc()

英文:

Global setting Yes (i.e. set only once at the time of Counter creation) -- but optionally Overridable later No.

i.e. I can set a label once at Counter init ... but I will NOT be able to override at actual inc call.

Set 'labelValues' when creating your Counter.

counter = Counter(
                name="some name", documentation="some doc",
                labelnames=['label1', 'label2', 'label3'], labelvalues=['label 1 global value])

counter.labels(label2='some value', label3='some value2').inc()

huangapple
  • 本文由 发表于 2020年1月6日 21:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定