PromQL操作符 #

一、操作符概述 #

text
PromQL操作符分类:

┌─────────────────────────────────────────────┐
│ 1. 算术操作符                               │
├─────────────────────────────────────────────┤
│ + - * / % ^                                 │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 2. 比较操作符                               │
├─────────────────────────────────────────────┤
│ == != > < >= <=                             │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 3. 逻辑操作符                               │
├─────────────────────────────────────────────┤
│ and or unless                               │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 4. 向量匹配操作符                           │
├─────────────────────────────────────────────┤
│ on ignoring group_left group_right          │
└─────────────────────────────────────────────┘

二、算术操作符 #

2.1 基本算术操作符 #

text
算术操作符:

┌─────────────────────────────────────────────┐
│ 操作符 │ 说明       │ 示例                 │
├────────┼────────────┼──────────────────────┤
│ +      │ 加法       │ 10 + 5 = 15          │
│ -      │ 减法       │ 10 - 5 = 5           │
│ *      │ 乘法       │ 10 * 5 = 50          │
│ /      │ 除法       │ 10 / 5 = 2           │
│ %      │ 取模       │ 10 % 3 = 1           │
│ ^      │ 幂运算     │ 2 ^ 3 = 8            │
└─────────────────────────────────────────────┘

2.2 标量与标量运算 #

promql
# 加法
10 + 5

# 减法
10 - 5

# 乘法
10 * 5

# 除法
10 / 5

# 取模
10 % 3

# 幂运算
2 ^ 10

2.3 向量与标量运算 #

promql
# 向量加标量
http_requests_total + 100

# 向量减标量
http_requests_total - 100

# 向量乘标量
http_requests_total * 2

# 向量除标量
http_requests_total / 100

# 转换单位(字节转MB)
node_memory_MemTotal_bytes / 1024 / 1024

# 计算百分比
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100

2.4 向量与向量运算 #

promql
# 向量相加
http_requests_total + http_errors_total

# 向量相减
node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes

# 向量相乘
rate(http_requests_total[5m]) * 60

# 向量相除
http_errors_total / http_requests_total

# 计算使用率
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100

三、比较操作符 #

3.1 基本比较操作符 #

text
比较操作符:

┌─────────────────────────────────────────────┐
│ 操作符 │ 说明       │ 示例                 │
├────────┼────────────┼──────────────────────┤
│ ==    │ 等于       │ 10 == 10 = true      │
│ !=    │ 不等于     │ 10 != 5 = true       │
│ >     │ 大于       │ 10 > 5 = true        │
│ <     │ 小于       │ 10 < 5 = false       │
│ >=    │ 大于等于   │ 10 >= 10 = true      │
│ <=    │ 小于等于   │ 10 <= 5 = false      │
└─────────────────────────────────────────────┘

3.2 标量比较 #

promql
# 等于
10 == 10

# 不等于
10 != 5

# 大于
10 > 5

# 小于
10 < 5

# 大于等于
10 >= 10

# 小于等于
10 <= 5

3.3 向量与标量比较 #

promql
# 等于
up == 1

# 不等于
up != 1

# 大于
http_requests_total > 1000

# 小于
http_requests_total < 100

# 大于等于
http_requests_total >= 1000

# 小于等于
http_requests_total <= 100

3.4 向量与向量比较 #

promql
# 向量比较
http_requests_total > http_errors_total

# 过滤条件
http_requests_total > bool 1000

# 比较并返回布尔值
http_requests_total == bool http_requests_total

3.5 bool修饰符 #

promql
# 返回布尔值(0或1)
http_requests_total > bool 1000

# 检查是否在线
up == bool 1

# 检查是否超过阈值
http_requests_total > bool 10000

# 用于条件判断
(http_requests_total > bool 1000) * 100

四、逻辑操作符 #

4.1 and操作符 #

promql
# and:两个向量都存在时返回
http_requests_total and http_errors_total

# 组合条件
up == 1 and http_requests_total > 100

# 多条件组合
up == 1 and http_requests_total > 100 and http_errors_total > 0

# 过滤在线且有错误的实例
up == 1 and rate(http_errors_total[5m]) > 0

4.2 or操作符 #

promql
# or:任一向量存在时返回
http_requests_total or http_errors_total

# 组合多个指标
node_memory_MemAvailable_bytes or node_memory_MemFree_bytes

# 多条件组合
up == 0 or http_requests_total == 0

# 检查任一条件
http_errors_total > 100 or http_requests_total > 10000

4.3 unless操作符 #

promql
# unless:左向量存在但右向量不存在时返回
http_requests_total unless http_errors_total

# 排除特定条件
up unless (up == 0)

# 排除离线实例
http_requests_total unless (up == 0)

# 找出没有错误的请求
http_requests_total unless http_errors_total

五、向量匹配 #

5.1 向量匹配概念 #

text
向量匹配:

┌─────────────────────────────────────────────┐
│ 默认匹配                                    │
├─────────────────────────────────────────────┤
│ • 相同标签集的样本才能匹配                  │
│ • 不同标签集的样本无法匹配                  │
└─────────────────────────────────────────────┘

示例:
向量A:http_requests_total{method="GET", status="200"}
向量B:http_errors_total{method="GET", status="500"}

无法匹配,因为status标签值不同

5.2 ignoring修饰符 #

promql
# 忽略特定标签进行匹配
http_requests_total ignoring(status) / http_errors_total ignoring(status)

# 忽略多个标签
http_requests_total ignoring(status, instance) / http_errors_total ignoring(status, instance)

# 计算错误率(忽略status标签)
sum by (method) (http_errors_total) ignoring(status) 
/ 
sum by (method) (http_requests_total) ignoring(status)

5.3 on修饰符 #

promql
# 只匹配特定标签
http_requests_total on(method) / http_errors_total on(method)

# 只匹配多个标签
http_requests_total on(method, instance) / http_errors_total on(method, instance)

# 计算错误率(只匹配method标签)
sum by (method) (http_errors_total) on(method) 
/ 
sum by (method) (http_requests_total) on(method)

5.4 group_left修饰符 #

promql
# group_left:多对一匹配
# 左侧可以有多个匹配右侧一个

# 示例:每个请求的错误率
http_requests_total 
/ on(instance) 
group_left 
http_errors_total

# 带标签
http_requests_total 
/ on(instance) 
group_left(service) 
http_errors_total

# 计算每个实例的内存使用率
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) 
/ on(instance) 
group_left 
node_memory_MemTotal_bytes

5.5 group_right修饰符 #

promql
# group_right:一对多匹配
# 右侧可以有多个匹配左侧一个

# 示例
http_errors_total 
/ on(instance) 
group_right 
http_requests_total

# 带标签
http_errors_total 
/ on(instance) 
group_right(service) 
http_requests_total

六、操作符优先级 #

text
操作符优先级(从高到低):

┌─────────────────────────────────────────────┐
│ 优先级 │ 操作符                             │
├────────┼────────────────────────────────────┤
│ 1      │ ^                                  │
│ 2      │ * / %                              │
│ 3      │ + -                                │
│ 4      │ == != > < >= <=                    │
│ 5      │ and unless                         │
│ 6      │ or                                 │
└─────────────────────────────────────────────┘

使用括号改变优先级:
(10 + 5) * 2 = 30
10 + 5 * 2 = 20

七、聚合操作符 #

7.1 聚合语法 #

promql
# 基本语法
<聚合操作符>(<向量表达式>)

# 分组聚合
<聚合操作符> by (<标签列表>) (<向量表达式>)

# 排除标签聚合
<聚合操作符> without (<标签列表>) (<向量表达式>)

7.2 常用聚合操作符 #

promql
# sum:求和
sum(http_requests_total)
sum by (method) (http_requests_total)
sum without (instance) (http_requests_total)

# avg:平均值
avg(http_requests_total)
avg by (instance) (http_requests_total)

# min:最小值
min(http_requests_total)
min by (instance) (http_requests_total)

# max:最大值
max(http_requests_total)
max by (instance) (http_requests_total)

# count:计数
count(http_requests_total)
count by (instance) (http_requests_total)

# stddev:标准差
stddev(http_requests_total)
stddev by (instance) (http_requests_total)

# stdvar:方差
stdvar(http_requests_total)
stdvar by (instance) (http_requests_total)

# topk:前K个最大值
topk(5, http_requests_total)

# bottomk:前K个最小值
bottomk(5, http_requests_total)

# quantile:分位数
quantile(0.9, http_requests_total)

八、实际应用示例 #

8.1 计算使用率 #

promql
# CPU使用率
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

# 内存使用率
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100

# 磁盘使用率
(1 - node_filesystem_avail_bytes / node_filesystem_size_bytes) * 100

# 网络使用率
rate(node_network_receive_bytes_total[5m]) / 1024 / 1024

8.2 计算错误率 #

promql
# HTTP错误率
sum(rate(http_errors_total[5m])) / sum(rate(http_requests_total[5m])) * 100

# 按服务分组的错误率
sum by (service) (rate(http_errors_total[5m])) 
/ 
sum by (service) (rate(http_requests_total[5m])) * 100

# 5xx错误率
sum(rate(http_requests_total{status=~"5.."}[5m])) 
/ 
sum(rate(http_requests_total[5m])) * 100

8.3 计算延迟 #

promql
# 平均延迟
rate(http_request_duration_seconds_sum[5m]) 
/ 
rate(http_request_duration_seconds_count[5m])

# P99延迟
histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))

# 按服务分组的P99延迟
histogram_quantile(0.99, 
  sum by (le, service) (rate(http_request_duration_seconds_bucket[5m]))
)

8.4 计算增长率 #

promql
# 请求增长率
(
  sum(rate(http_requests_total[5m])) 
  - 
  sum(rate(http_requests_total[5m] offset 1h))
) 
/ 
sum(rate(http_requests_total[5m] offset 1h)) * 100

# 内存增长趋势
deriv(node_memory_MemAvailable_bytes[1h])

# 预测内存何时耗尽
predict_linear(node_memory_MemAvailable_bytes[1h], 3600)

九、总结 #

操作符要点:

类型 操作符
算术 + - * / % ^
比较 == != > < >= <=
逻辑 and or unless
向量匹配 on ignoring group_left group_right

优先级:

优先级 操作符
1(高) ^
2 * / %
3 + -
4 == != > < >= <=
5 and unless
6(低) or

下一步,让我们学习PromQL高级查询!

最后更新:2026-03-27