常用 PQL

PQL Prometheus

Posted by gomyck on October 12, 2022

Prometheus 常用语法总结

一、简单时间查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1.查询metricKey="prometheus_http_requests_total"的所有数据
prometheus_http_requests_total

2.查询metrickey="prometheus_http_requests_total"且tagkey="job",tagvalue="prometheus"的所有数据
prometheus_http_requests_total{job="prometheus"}

3.查询最近2min,metrickey="prometheus_http_requests_total"且tagkey="job",tagvalue="prometheus"的所有数据
prometheus_http_requests_total{job="prometheus"}[2m]

4.查询最近1min的单个值,metrickey="prometheus_http_requests_total"且tagkey="job",tagvalue="prometheus"
prometheus_http_requests_total{job="prometheus"} offset 1m

5.查询 3min前到1min前的多个值,时间范围2m,metrickey="prometheus_http_requests_total"且tagkey="job",tagvalue="prometheus"
prometheus_http_requests_total{job="prometheus"}[2m] offset 1m

二、算数运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1.除法
prometheus_http_requests_total{job="prometheus"} offset 1m /1024

2.乘法
prometheus_http_requests_total{job="prometheus"} offset 1m *1024

3.加法
prometheus_http_requests_total{job="prometheus"} offset 1m +1024

4.减法
prometheus_http_requests_total{job="prometheus"} offset 1m -1024

5.%取模
prometheus_http_requests_total{job="prometheus"} offset 1m %1024

6.^幂
prometheus_http_requests_total{job="prometheus"} offset 1m ^2

7.两个指标运算
prometheus_http_requests_total{job="prometheus"} offset 1m + prometheus_http_requests_total{job="prometheus"}

三、比较和过滤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1. >  # 查询最近1min,metrickey="prometheus_http_requests_total"且tagkey="job",tagvalue="prometheus"
prometheus_http_requests_total{job="prometheus"} offset 1m -1024>1

2. ==
prometheus_http_requests_total{job="prometheus"} offset 1m -1024==1

3. !=
prometheus_http_requests_total{job="prometheus"} offset 1m -1024!=1

4. <
prometheus_http_requests_total{job="prometheus"} offset 1m <1

5. <=
prometheus_http_requests_total{job="prometheus"} offset 1m <=1

6. >=
prometheus_http_requests_total{job="prometheus"} offset 1m >=1

7. 比较 bool值,  >1 返回true 1,否则返回false 0
prometheus_http_requests_total{job="prometheus"} offset 1m >= bool 1

四、逻辑运算

1
2
3
4
5
6
7
8
1. or 合并2个查询结果
prometheus_http_requests_total{job="prometheus"} or prometheus_http_requests_total{job="prometheus1"}

2.and 查询匹配2个查询结果的并集
container_memory_usage_bytes{id="xxx"} and container_memory_max_usage_bytes{pod="vvv"}

3.unless 删除2个查询结果的并集,保留不匹配的结果
container_memory_max_usage_bytes{pod="xxx"} unless container_memory_usage_bytes{id="vvv"}

五、复合运算优先级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
^,*/%,+ -,== !=,<=,<,>=,>,and unless,or

1.匹配固定值整数、小数、负数
prometheus_http_requests_total{job="prometheus"} 10?-语法有问题待确认

2.ignore 获取prometheus_http_requests_total ,job= prometheus,rate=test
除以 忽略tagkey= rate的prometheus_http_requests_total指标值
prometheus_http_requests_total{job="prometheus",rate="test"} / ignoring(rate) prometheus_http_requests_total

3.group_left表示多对一?
prometheus_http_requests_total{job="prometheus",rate="test"} / ignoring(rate)  group_left prometheus_http_requests_total

4.group_right表示一对多?
prometheus_http_requests_total{job="prometheus",rate="test"} / ignoring(rate)  group_right prometheus_http_requests_total

六、聚合运算与分组

1
2
3
4
5
6
根据job做分组求不同聚合方式下的值Min、max、avg、sum、count
1.sum(prometheus_http_requests_total) by (job)
2.Min(prometheus_http_requests_total) by (job)
3.avg(prometheus_http_requests_total) by (job)
4.max(prometheus_http_requests_total) by (job)
5.count(prometheus_http_requests_total) by (job1,job2)

七、标准差和方差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1.方差
stddev(container_memory_usage_bytes{node="ingressnode01",pod="node-exporter-kf2cr"}/1024/1024 )

2.标准差
stdvar(container_memory_usage_bytes{node="ingressnode01",pod="node-exporter-kf2cr"}/1024/1024 )

3.统计value相同的指标个数,统计value相同的元素个数,形成标签为"newlabel"=value的新的向量
count_values ("newlabel",kube_deployment_created)

4.获取最小的2个值
bottomk(2, prometheus_http_requests_total{job="prometheus",rate="test"}/2)

5.获取最大的2个值
topk(2, prometheus_http_requests_total{job="prometheus",rate="test"}/2)

6.获取历史的最大值
获取prometheus_http_requests_total 该metrickey下,tagkey=quantile=0.25
获取quantile=0.25的历史metricvalue的最大值
quantile(0.25,prometheus_http_requests_total{rate="test"}

八、函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1.abs(XX)绝对值
abs(prometheus_http_requests_total{job="prometheus",rate="test"})

2.absent(XX) value存在返回空,value不存在返回1
absent(prometheus_http_requests_total{job="prometheus",rate="test"})

3.ceil(XX) 向上取整,当前值为整数则不变
ceil(prometheus_http_requests_total{job="prometheus",rate="test"})

4.floor(XX) 向下取整,当前值为整数则不变
floor(prometheus_http_requests_total{job="prometheus",rate="test"})

5.sqrt(XX) 返回value的平方根
sqrt(prometheus_http_requests_total{job="prometheus",rate="test"})

6.exp(XX) 返回value中所有元素自然常数e为底的指数函数?
exp(prometheus_http_requests_total{job="prometheus",rate="test"})

7.resets(v range-vector) 对于每个 time series , 它都返回一个 counter resets的次数
resets(prometheus_http_requests_total{job="prometheus",rate="test"}[5m])

8.changes(v)返回value中所有元素更改的次数
changes(prometheus_http_requests_total{job="prometheus",rate="test"})

9.round(v, t) 按四舍五入取接近当前值的t的整数倍的值,t可以是小数和分数

10.返回限制范围内的最大值clamp_max(X,max),其上限为max
clamp_max(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"},1)

11.返回限制范围内的最小值clamp_min(X,min),其上限为min
clamp_min(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"},1)

12.time()获取时间

13.delta(v range-vector) 计算第一个值和最后一个值的差值
delta(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}-mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"})

14.idelta(v range-vector) 计算最新的2个样本值之间的差别,和13类似
idelta(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}-mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"})

15.获取计算指定范围内的增长
increase(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}[5m])

16.计算每秒的平均增长值, 基于的是最新的2个数据点
irate(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}[5m])

17.计算每秒的增长值
rate(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}[5m])

18.sort升序排序
sort(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}[5m])

19.sort_desc降序排序
sort_desc(mysql.innodb.rollback.on.timeout_chris{appId="npcs_pubs_cpsgw"}[5m])

九、多指标查询

1
2
3
4
5
1.查询cpu or memory的数据
{__name__=~"cpu|memory"}

2.正则匹配 查询以pro开头
prometheus_http_requests_total{job=~"pro.*"}

原文地址