Prometheus:如何在查询中获取label_value()

NIc*_*Ick 4 metrics grafana prometheus promql

所以我有 2 个指标,我需要从第一个指标获取标签值,然后查询第二个指标,其中 {param="label_values_from_1st_metric"}。例如:

metric_1{instance="abc"}- 返回 3 个时间序列:

metric_1{name="my-service", env="production", host="example-0.org"}

metric_1{name="my-service", env="production", host="example-1.org"}

metric_1{name="my-service", env="production", host="example-2.org"}

接下来,我需要使用第一个查询中的主机值对第二个指标进行查询。

metric_2{domain="exmple-0,1,2.org"}

所以问题是,如何将 label_values 传递给第二个查询?据我了解,我只能对 grafana 面板中的变量使用 label_values() ,所以我无法编写一个查询来为我做到这一点。

Pul*_*ick 5

您可以通过以下方式尝试:

metric_2 * on(instance) group_left(host) metric_1{instance="abc"}
Run Code Online (Sandbox Code Playgroud)
  • on(instance) =>这是如何加入标签实例。

  • group_left(host) metric_1{instance="abc"} => 表示,在结果中保留 metric_1 中的标签主机。

  • 天哪,为什么这么复杂。有什么解决方法吗?也许有什么技巧可以让我使用从另一个查询检索到的标签对 metric_2 进行查询? (2认同)