Prometheus查询之Vector match

Vector Match

vector match的含义如下

根据一定的规则,对两个vector的label进行匹配,如果匹配成功,则对两个vector进行运算

vector match有两种类型,一个是one to one,一个是one to many或者many to one

one to one

<vector expr> <bin-op> ignoring(<label list>) <vector expr>
<vector expr> <bin-op> on(<label list>) <vector expr>

bin-op 表示运算操作符,”+,-,/,*”等

ignoring 表示忽略某些label来关联两个vector

on 表示根据某些label来关联两个vector

例如vector1{label1,label2,label3}, vector2{label2,label3}, 则

ignoring(label1) 表示通过label2和label3对vector进行匹配
on(label2) 表示通过label2对vector进行匹配

下面以一个实际的例子来说明one to one 的vector match

原始数据如下

#vector1
method_code:http_errors:rate5m{method="get", code="500"}  24
method_code:http_errors:rate5m{method="get", code="404"}  30
method_code:http_errors:rate5m{method="put", code="501"}  3
method_code:http_errors:rate5m{method="post", code="500"} 6
method_code:http_errors:rate5m{method="post", code="404"} 21

#vector2
method:http_requests:rate5m{method="get"}  600
method:http_requests:rate5m{method="del"}  34
method:http_requests:rate5m{method="post"} 120

查询语句如下

method_code:http_errors:rate5m{code="500"} / ignoring(code) method:http_requests:rate5m

首先vector1的label有method,code,vector2的label有method,根据ignoring(code)可知,两个vector是根据method进行关联,可以得到下表所示的match结果

vector1 vector2
method_code:http_errors:rate5m{method=”get”, code=”500”} 24 method:http_requests:rate5m{method=”get”} 600
method_code:http_errors:rate5m{method=”post”, code=”500”} 6 method:http_requests:rate5m{method=”post”} 120

因此,查询结果如下所示:

{method="get"}  0.04            //  24 / 600
{method="post"} 0.05            //   6 / 120

上述查询语句等价于

method_code:http_errors:rate5m{code="500"} / on(method) method:http_requests:rate5m

one to many/many to one

<vector expr> <bin-op> ignoring(<label list>) group_left(<label list>) <vector expr>
<vector expr> <bin-op> ignoring(<label list>) group_right(<label list>) <vector expr>
<vector expr> <bin-op> on(<label list>) group_left(<label list>) <vector expr>
<vector expr> <bin-op> on(<label list>) group_right(<label list>) <vector expr>    

group_left 表示many to one

group_right 表示one to many

下面还是以实际数据来说明,原始数据如下:

#vector1
method_code:http_errors:rate5m{method="get", code="500"}  24
method_code:http_errors:rate5m{method="get", code="404"}  30
method_code:http_errors:rate5m{method="put", code="501"}  3
method_code:http_errors:rate5m{method="post", code="500"} 6
method_code:http_errors:rate5m{method="post", code="404"} 21

#vector2
method:http_requests:rate5m{method="get"}  600
method:http_requests:rate5m{method="del"}  34
method:http_requests:rate5m{method="post"} 120

查询语句如下:

method_code:http_errors:rate5m / ignoring(code) group_left method:http_requests:rate5m

上述查询语句表示通过method对两个vector进行匹配,vector1存在两个method=get的metric,两个method=post的metric,vector2各存在一个method=get已经method的metric,所以match结果如下

vector1 vector2
method_code:http_errors:rate5m{method=”get”, code=”500”} 24 method:http_requests:rate5m{method=”get”} 600
method_code:http_errors:rate5m{method=”get”, code=”404”} 30 method:http_requests:rate5m{method=”get”} 600
method_code:http_errors:rate5m{method=”post”, code=”500”} 6 method:http_requests:rate5m{method=”post”} 120
method_code:http_errors:rate5m{method=”post”, code=”404”} 21 method:http_requests:rate5m{method=”post”} 120

查询结果如下

{method="get", code="500"}  0.04            //  24 / 600
{method="get", code="404"}  0.05            //  30 / 600
{method="post", code="500"} 0.05            //   6 / 120
{method="post", code="404"} 0.175           //  21 / 120

总结

  1. 对于one to one表达的是,vector1和vector2根据on或者ignoring中的label组成的值是唯一的,也就是说vector1和vector2根据label list,不存在两个相同的label value

  2. 对于group_left(label list)来说,vector1的label list组成的value可以有多个,而vector2的label list组成的value只能有一个,而group_right(label list)则相反,vector1的值只能有一个,而vector2的值可以有多个


Reprint please specify: wbl Prometheus查询之Vector match

Previous
Spring Boot启动流程初探 Spring Boot启动流程初探
刚接触Spring Boot的时候,相信大家都有接触过以下代码,这是Spring Boot的启动类,今天就来简单看下Spring Boot的启动流程 @SpringBootApplication public class Applicati
2019-04-27
Next
Spring-Boot Quick start Spring-Boot Quick start
Create Project构建一个Spring Boot的项目非常方面,可以使用官方提供的Spring Initializr,打开https://start.spring.io/,输入项目基本信息即可构建一个项目 另外你还可以设置需要引
2019-04-21