如何在Prometheus查询中“联接"两个指标? [英] How can I 'join' two metrics in a Prometheus query?

查看:3261
本文介绍了如何在Prometheus查询中“联接"两个指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用领事出口商将我的服务的健康状况和状态吸收到Prometheus中.我想在Consul中的服务和节点的状态为紧急状态时发出警报,然后在路由这些警报时使用从Consul提取的标签.

I am using the consul exporter to ingest the health and status of my services into Prometheus. I'd like to fire alerts when the status of services and nodes in Consul is critical and then use tags extracted from Consul when routing those alerts.

通过此讨论,我了解到指标,但我不确定如何将一个系列与另一个系列结合使用,因此我可以利用具有健康状态的标签.

I understand from this discussion that service tags are likely to be exported as a separate metric, but I'm not sure how to join one series with another so I can leverage the tags with the health status.

例如,以下查询:

max(consul_health_service_status{status="critical"}) by (service_name, status,node) == 1

可以返回:

{node="app-server-02",service_name="app-server",status="critical"} 1

但是我也想从这个系列中获得"env":

but I'd also like 'env' from this series:

consul_service_tags{node="app-server-02",service_name="app-server",env="prod"} 1

沿着节点和service_name加入,将以下内容作为一个系列传递给Alertmanager:

to get joined along node and service_name to pass the following to the Alertmanager as a single series:

{node="app-server-02",service_name="app-server",status="critical",env="prod"} 1

然后我可以在路由中匹配"env".

I could then match 'env' in my routing.

有没有办法做到这一点?在我看来,没有任何操作或功能可以像我这样分组或加入.据我所知,这些标签已经需要成为consul_health_service_status指标上的标签.

Is there any way to do this? It doesn't look to me like any operations or functions give me the ability to group or join like this. As far as I can see, the tags would already need to be labels on the consul_health_service_status metric.

推荐答案

您可以使用group_left的参数列表包括来自右操作数的其他标签(为清楚起见,括号和缩进):

You can use the argument list of group_left to include extra labels from the right operand (parentheses and indents for clarity):

(
  max(consul_health_service_status{status="critical"}) 
  by (service_name,status,node) == 1
)
   + on(service_name,node) group_left(env)
(
   0 * consul_service_tags
)

这里的重要部分是操作+ on(service_name,node) group_left(env):

The important part here is the operation + on(service_name,node) group_left(env):

  • +作为联接运算符被滥用"(很好,因为0 * consul_service_tags始终具有值0)
  • group_left(env)是修饰符,从右(consul_service_tags)包括额外的标签env
  • the + is "abused" as a join operator (fine since 0 * consul_service_tags always has the value 0)
  • group_left(env) is the modifier that includes the extra label env from the right (consul_service_tags)

这篇关于如何在Prometheus查询中“联接"两个指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆