在普罗米修斯仪器的上下文中,我是否应该在更新指标值时使用所有普罗米修斯标签
In the context o f prometheus instrumentation, should I use all prometheus labels when updating a metrics value
我有一个 prometheus 指标,其标签声明为
errors_total = prometheus_client.Counter("errors_total", "Total errors", ["source", "code])
errors_total.labels("source"="initialization", code="100")
errors_total.labels("source"="shutingdown", code="200")
当我在发生错误的监控代码中递增指标时,我可以将其用作:
errors_total.labels(source="initialization").inc()
或
errors_total.labels(code="200").inc()
我的问题是我可以在增加指标时只使用一个标签吗?
不,您必须为每个标签指定一个值。如果你尝试代码,你会得到一个异常:
>>> c.labels(source="shutingdown").inc()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/anemyte/.local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 146, in labels
raise ValueError('Incorrect label names')
ValueError: Incorrect label names
>>> c.labels(["shutingdown"]).inc()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/anemyte/.local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 150, in labels
raise ValueError('Incorrect label count')
ValueError: Incorrect label count
如果您有一个没有标签值的事件,您可以传递一个占位符值。破折号 ("-"
) 是最佳选择,因为它只占用一个字节,但您也可以使用 "undefined"
、"none"
或 "other"
之类的内容,具体取决于上下文。
我有一个 prometheus 指标,其标签声明为
errors_total = prometheus_client.Counter("errors_total", "Total errors", ["source", "code])
errors_total.labels("source"="initialization", code="100")
errors_total.labels("source"="shutingdown", code="200")
当我在发生错误的监控代码中递增指标时,我可以将其用作:
errors_total.labels(source="initialization").inc()
或
errors_total.labels(code="200").inc()
我的问题是我可以在增加指标时只使用一个标签吗?
不,您必须为每个标签指定一个值。如果你尝试代码,你会得到一个异常:
>>> c.labels(source="shutingdown").inc()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/anemyte/.local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 146, in labels
raise ValueError('Incorrect label names')
ValueError: Incorrect label names
>>> c.labels(["shutingdown"]).inc()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/anemyte/.local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 150, in labels
raise ValueError('Incorrect label count')
ValueError: Incorrect label count
如果您有一个没有标签值的事件,您可以传递一个占位符值。破折号 ("-"
) 是最佳选择,因为它只占用一个字节,但您也可以使用 "undefined"
、"none"
或 "other"
之类的内容,具体取决于上下文。