prometheus - promSQL v2.29

启动参数

  • 负偏移量: 启动时增加--enable-feature=promql-negative-offset
  • 时间修饰符: @ 启动时增加 --enable-feature=promql-at-modifier

数据类型分类

  • 即时向量: Instant-vector
  • 范围向量: Range-vector
  • 标量: Scalar
  • 字符串(未启用)

查询

指标名

// 结果为即时向量, 所有该指标的结果, label 不同或 label 结果不同, 会作为不同结果
node_network_receive_bytes_total

指标名+label 条件

// 用 label 作为条件限制, 结果也是即时向量
node_network_receive_bytes_total{device="eth0",instance="10.20.122.117:9796"}

// 条件匹配支持
// = 完全等于
// != 完全不等于
// =~ 正则匹配满足条件的
// !~ 正则匹配不满足条件的

// 等价于上边示例的另一种写法, 当指标名和关键字重复时可用
{__name__="node_network_receive_bytes_total", device="eth0", instance="10.20.122.117:9796"}

指标+范围

// 以当前时间为准, 用时间判断取结果的个数
node_network_receive_bytes_total[10m]

// 范围支持
// ms 毫秒
// s  秒
// m  分钟
// h  小时
// d  天
// w  一周(7 天)
// y  一年(365 天)

// 也支持周期内间隔取值, 这样的写法只支持 instant-vector 类型 
node_network_receive_bytes_total[10m:1m]

// 套用函数示例
rate(node_network_receive_bytes_total[5m])[10m:1m] // rate 结果是个 instant-vector

指标+偏移量

// 偏移关键字 offect, 默认向左偏移
node_network_receive_bytes_total offset 10m

// 可以通过参数开启向右偏移
node_network_receive_bytes_total offset -10m

指标+修饰符

// 修饰符关键字 @, 根据时间戳为基准时间取值
node_network_receive_bytes_total @ 1630253194.254

// 也支持范围向量
node_network_receive_bytes_total[10m] @ 1630253194.254

// 同样支持函数
// start()
// end()

// 如果使用聚合函数, 需要将@放在函数内
sum(node_network_receive_bytes_total[10m] @ 1630253194.254)

运算

算数运算

  • 加: +
  • 减: -
  • 乘: *
  • 除: /
  • 取余: %
  • 幂运算: ^

比较运算

  • 等于: ==
  • 不等于: !=
  • 大于: >
  • 小于: <
  • 大于等于: >=
  • 小于等于: <=

逻辑运算 ???

  • 与: and
  • 或: or
  • 非: unless

优先级

// 由上到下

// ^
// *, /,%
// +, -
// ==, !=, <=, <, >=,>
// and, unless
// or

// 相同级别运算符是左结合
2 * 3 % 2 --> (2 * 3) % 2

// ^ 是右结合
2 ^ 3 ^ 2 --> 2 ^ (3 ^ 2)

聚合运算

aggregation (针对符合条件的所有指标进行聚合)

  • sum (总和), min (最小值), max (最大值), avg (平均值)
  • count (计算向量个数), count_values (计算具有相同值的个数)
    // count_values, 是计算值相同的个数, 参数为新 label 的名称
    
    // 示例: em2, em3, em4 值都为 0, 只有 em1 有值
    count_values("zero", node_network_receive_bytes_total{device=~"em[1234]"})
    
    // 结果
    // {zero="903732593307"} 1
    // {zero="0"} 3
    
  • topk (值最大的 k 个元素), bottomk (值最小的 k 个元素)
  • group (结果向量中的所有值都是 1)
    // group 结果都为 1
    // 最好和 by 连用, 否则结果是 "{} 1", 意义不大
    group by(cpu, mode) (node_cpu_seconds_total)
    
  • stddev (计算维度上的总体标准偏差)
  • stdvar (计算维度上的总体标准方差)
  • quantile (在维度上计算 φ-分位数 (0 ≤ φ ≤ 1)

aggregation_over_time (针对一个指标范围时间的结果内进行聚合)

  • sum_over_time, min_over_time, max_over_time, avg_over_time
  • count_over_time
  • last_over_time (距离基准点最近的结果)
  • present_over_time (在 2.29 后添加) ??? 改为 2.29 后测试
  • quantile_over_time
  • stddev_over_time
  • stdvar_over_time

配合 without 和 by

// by, 根据某一列做聚合
sum by(device) (node_network_receive_bytes_total{device=~"em[1234]"})

// without, 去除某一列做聚合
sum without(device) (node_network_receive_bytes_total{device=~"em[1234]"})

函数运算

类型变换

  • vector
    // 将 scalar 变成没有时间标识的 instant-vector
    vector(0)
    
  • scalar
    // 将单个"流"的值转为 scalar, 如果"流"有多个, 则结果为 NaN
    
    // 用 code 做限制, 只有一个结果
    scalar(promhttp_metric_handler_requests_total{code="200"}) --> 2756
    
    // 如果不做限制
    scalar(promhttp_metric_handler_requests_total) --> NaN
    

增长相关

delta 和 rate
idelta 和 irate
increase

排序

// 正序
sort(changes(up[1d]))

// 倒序
sort_desc(changes(up[1d]))

Label 相关

  • label_join
    // 将多个 label 拼接为一个新的 label, 并定义连接符号
    // label_join(instant-vector, newLabel, stepStr, label1, label2)
    label_join(up, "foo", ",", "instance", "job") --> up{foo="localhost:9100,xxx", instance="localhost:9100", job="xxx"}
    
  • label_replace
    // 将一个 label 根据正则重写为新的 label
    label_replace(instant-vector, newLabel, regxIndex, oldLabel, regx)
    label_replace(up, "foo", "$1", "instance", "(\\w+):.*") --> up{foo="localhost", instance="localhost:9100", job="xxx"}
    

设置上下限

  • clamp
    // 设置上下限
    // clamp(instant-vector, min, max)
    clamp(vector(100), 1, 10) --> 10
    clamp(vector(-100), 1, 10) --> 1
    
  • clamp_max
    // 设置上限
    // clamp_max(instant-vector, max)
    clamp_max(vector(100), 10) --> 10
    
  • clamp_min
    // 设置下限
    // clamp_min(instant-vector, min)
    clamp_min(vector(-1), 10) --> 10
    

日期和时间相关

  • time 和 timestamp
    // time 直接使用, 当前时间的时间戳
    time()
    
    // 返回 instant-vector 的时间戳, timestamp(instant-vector)
    timestamp(up)
    
  • 日期
    // 年, year(unix)
    
    // 月, month(unix)
    
    // 日, day_of_month(unix), 1-31
    
    // 周几, day_of_week(unix), 0-6, 0是星期日
    
    // 时, hour(unix), 是 UTC 时间, 需要进行时区计算(北京+8)
    
    // 分, minute()
    
    // 时间戳所在月多少天, days_in_month(), 28-31
    

取整 和 四舍五入

  • ceil
    // 向上取整
    // ceil(instant-vector)
    ceil(vector(40.1)) --> 41
    
  • floor
    // 向下取整
    // floor(instant-vector)
    floor(vector(40.9)) --> 40
    
  • round
    // 四舍五入
    // round(instant-vector, scalar)
    
    // 一般用法
    round(vector(40.4)) --> 40
    round(vector(40.5)) --> 41
    
    // 补充: 四舍五入后, 找到最接近该整数的且是s公倍数的数, s 默认为 1
    round(vector(40.4), 3) --> 39  // 四舍五入结果为 40, 最接近 40 的 3 的公倍数为 39, 结果为 39
    round(vector(40.4), 4) --> 40
    round(vector(40.4), 5) --> 40
    round(vector(40.4), 6) --> 42  // 只找距离最近的最小公倍数, 不需要小于该结果
    

次数统计

  • 变换次数
    // changes(range-vector)
    changes(up[1d]) 
    
  • Counter 类型重置次数
    // resets(range-vector)
    resets(up[1d]) 
    

计算

// abs, 取绝对值
abs(vector(-1)) --> 1

// sgn, 返回符号正负, 正为1, 负为-1, 0为0
sgn(vector(1)) --> 1
sgn(vector(-1)) --> -1
sgn(vector(0)) --> 0

判断是否为空

// absent, 判断 instant-vector 是否为空, 为空则返回1, 不为空返回空
absent(up{job="prometheus"})  -->absent(up{job="xxxxx"}) --> {job="xxxxx"} 1

// absent_over_time, 判断 range-vector 是否为空, 
absent_over_time(up{job="prometheus"}[1m])  -->absent_over_time(up{job="xxxxx"}[1m]) --> {job="xxxxx"} 1

其他

  • 计算对数
    ln()
    log2()
    log10()

  • 计算平方根
    sqrt()

  • 指数函数
    exp()

  • 直方图分布
    histogram_quantile()

  • 求导数
    deriv()

  • 没看懂
    holt_winters()
    predict_linear()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值