【问题标题】:In the context o f prometheus instrumentation, should I use all prometheus labels when updating a metrics value在普罗米修斯仪器的上下文中,我应该在更新指标值时使用所有普罗米修斯标签吗
【发布时间】:2021-05-28 16:43:47
【问题描述】:

我有一个普罗米修斯指标,其标签声明为

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()

我的问题是我可以在增加指标时只使用一个标签吗?

【问题讨论】:

    标签: prometheus prometheus-python-client


    【解决方案1】:

    不,您必须为每个标签指定一个值。如果你尝试代码,你会得到一个异常:

    >>> 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" 之类的内容,具体取决于上下文。

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2020-01-10
      • 2020-11-12
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2021-11-01
      • 2020-12-23
      相关资源
      最近更新 更多