【Prometheus】PromQL

数据类型

  1. 即时向量(instant vector)

node_cpu_seconds_total{instance="ahoj-dev-ubuntu-virtualbox",mode="idle"}
image.png

  1. 区间向量(range vector)

node_cpu_seconds_total{instance="ahoj-dev-ubuntu-virtualbox",mode="idle"}[1m]
image.png

  1. 标量(scalar)

2 * 4
image.png

  1. 字符串(string)

image.png

时间序列过滤器

  1. 瞬时向量过滤器

过滤器标签支持两种模式:完全匹配和正则匹配,正则匹配使用 Google RE2语法
=:完全相同
!=:不相同
=~:选择与提供的正则表达式相匹配的标签
!~:选择与提供的正则表达式不匹配的标签
例如:node_cpu_seconds_total{mode=~"user|system",cpu="0"}
image.png

  1. 区间向量过滤器

以当前时间为基准,查询时间范围内的数据
时间范围通过数字表示,时间单位有s, m, h, d, w, y
例如:node_cpu_seconds_total{mode=~"user|system",cpu="0"}[30s]查询的是 30s 内的数据
image.png

  1. 时间位移操作

如果想查询昨天一天的区间内的数据,需要使用时间位移关键字offset
例如:node_cpu_seconds_total{mode=~"user|system",cpu="0"} offset 3m
这里的offset关键字要跟在选择器{}后面
正确示例:max(node_cpu_seconds_total{mode=~"user|system",cpu="0"} offset 3m)
错误示例:max(node_cpu_seconds_total{mode=~"user|system",cpu="0"}) offset 3m
时间位移操作适用于区间向量,例如:查询一周之前的 5分钟之内的 cpu用户态使用率
rate(node_cpu_seconds_total{instance="ahoj-dev-ubuntu-virtualbox",mode="user"}[5m] offset 1w)

操作符

官方文档:https://prometheus.io/docs/prometheus/latest/querying/operators/

数学运算符

+, -, *, /, %, ^(幂运算)

布尔运算符

查询请求数大于20的:prometheus_http_requests_total > 20
将请求数大于20的结果置为 True,其他置为 False:prometheus_http_requests_total > bool 20
image.png

集合运算符

and(交集), or(并集), unless(排除)
unless:vector1 和 vector2 进行 unless 操作,会产生一个新的集合,该集合首先取 vector1 集合的所有元素,然后排除掉所有 vector2 中存在的元素。
例如:vector1 = [a, b, c], vector2 = [b, c, d] 那么 vector1 unless vector2 = [a]

操作符优先级

  1. ^
  2. *, /, %, atan2
  3. +, -
  4. ==, !=, <=, <, >=, >
  5. and, unless
  6. or

聚合运算

  • sum (calculate sum over dimensions,求和)
  • min (select minimum over dimensions,最小值)
  • max (select maximum over dimensions, 最大值)
  • avg (calculate the average over dimensions,平均值)
  • group (all values in the resulting vector are 1,)
  • stddev (calculate population standard deviation over dimensions,标准差)
    • 常用来描述数据的波动大小,反应一组数据偏离平均值的程度
    • 例如:统计两个球队队员身高,两只对位平均身高都是 180,看起来似乎差不多,但画图如下

image.png
image.png

  • stdvar (calculate population standard variance over dimensions,标准方差)
  • count (count number of elements in the vector,计数)
  • count_values (count number of elements with the same value,对value进行技术)
  • bottomk (smallest k elements by sample value,后n条时序)
  • topk (largest k elements by sample value,前n条时序)
    • topk(5, prometheus_http_requests_total)
  • quantile (calculate φ-quantile (0 ≤ φ ≤ 1) over dimensions,分位数)

内置函数

https://prometheus.io/docs/prometheus/latest/querying/functions/

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值