【问题标题】:Prometheus Library Python - Dynamic instance namePrometheus 库 Python - 动态实例名称
【发布时间】:2021-11-11 07:46:58
【问题描述】:

我正在使用 Python Prometheus 库来检测我的应用程序。假设我有以下代码:

from prometheus_client import start_http_server, Counter

data = {}
data['locations'] = 'berlin'

for i in data['location']
  metric = Counter("location_service_" + i + "_http_requests_count", "This metric tracks the requests from location: " + i)

metric.inc(1)

我正在为“指标”而苦苦挣扎,如何根据“位置”值进行动态调整?

【问题讨论】:

  • 你目前的解决方案怎么样?
  • 这其实是目前的做法。但是,我想要“度量”动态。所以:berlin.inc(1), munich.inc(1)

标签: python prometheus


【解决方案1】:

最好使用标签而不是更改指标名称。使用标签可以更轻松地创建仪表板/警报。方法如下:

from prometheus_client import start_http_server, Counter

label_names = ['location']
c = Counter("metric_name", "metric_descr", labelnames=label_names)
c.labels(location="berlin").inc()

然后你可以像这样查询这个指标的值:

metric_name{location="berlin"}

更多关于查询here 和 我还鼓励您阅读 best practices 的命名。

【讨论】:

  • 我不想使用标签是有原因的 --> 基数。我将拥有多达 6000 个位置,并且我想使用 Prometheus 更改功能。我假设 Prometheus 无法处理具有 6000 个不同标签值的指标。
  • @newduino Metric name 也是一个标签(__name__),所以它不能帮你解决这个问题。
  • 那么即使是 6000 个不同的指标也会导致问题?我认为 Prometheus 可以轻松处理数千个指标。
  • @newduino 是的,Prometheus 可以处理数百万个时间序列。关键是,一个具有 6000 个标签变体的度量名称和具有不同名称的 6000 个度量没有区别。无论哪种方式,这将是 6000 个时间序列。未绑定标签的基数问题来自缩放。假设您有一个实例 (hostA),它公开了 6000 个时间序列。然后设置另一个(hostB),现在有 12,000 个指标,因为来自一个实例的指标具有“hostA”标签,而来自另一个实例的指标具有“hostB”标签。
  • 好吧。伟大的。我不知道。
猜你喜欢
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
  • 2013-10-04
相关资源
最近更新 更多