prometheus查询语法(PromQL)
Prometheus底层监控数据存储是时序数据库,它 提供了一种名为 PromQL(Prometheus Query Language)的函数式查询语言,可以让用户实时查询和聚合时序数据库的数据。他是一组组的键值对+标签的组合。
prometheus常见的查询语法
-
字符串
PromQL遵循与Go语言相同的转义规则,单引号和双引号一样,里面有反斜杠
\
就是转义。反引号`里面的内容全部是字符串,不会做任何转义。"this is a string" 'these are unescaped: \n \\ \t' `these are not unescaped: \n ' " \t`
-
浮点数
23 -2.43 3.4e-9 0x8f -Inf NaN
-
查询某个指标的所用时序数据
http_requests_total #指标名 #可以通过{}号筛选里面的标签。 http_requests_otal{job="prometheus",group="canary"}
=
:选择与提供的字符串完全相同的标签。!=
:选择不等于提供的字符串的标签。=~
:选择与提供的字符串正则表达式匹配的标签。!~
:选择与提供的字符串不匹配的标签。
匹配正则,多组数据
http_requests_total{environment=~"staging|testing|development",method!="GET"}
指标名字也可以另外一种方式
__name__=
,后面也可以跟=,!=,=~,!~
{__name__=~"job:.*"} #查询名称以job开头的所有指标: {__name__="http_requests_total"}
-
某个指标的范围查询
http_requests_total{job="prometheus"}[5m] #查询最近5分钟的所用时序数据
ms
- 毫秒s
- 秒m
- 分钟h
- 小时d
- days - 假设一天总是 24 小时w
- 周 - 假设一周总是 7dy
- 年 - 假设一年总是 365d- 1h30m - 也可以是组合模式,代表最近1小时30分
-
查询某个指标的时许偏移量
此功能需要在prometheus server启动时候加上
--enable-feature=promql-negative-offset
标志启用。很多监控指标,在绘制图像过程中,需要同步或者环比,所以需要偏移量。
http_requests_total offset 5m #偏移量为5分钟 http_requests_total offset 1d #偏移量为1天 sum(http_requests_total{method="GET"} offset 5m) # rate(http_requests_total[5m] offset -1d) #还可以指定负偏移量
-
修饰符
返回特定时间点的指标监控值,可以通过设置
--enable-feature=promql-at-modifier
标志来启用。http_requests_total @ 160974600 #160974600是一个时间戳 sum(http_requests_total{method="GET"} @ 1609746000) rate(http_requests_total[5m] @ 1609746000)
-
子查询
子查询一般用不到。子查询允许您对给定的范围和分辨率运行即时查询。子查询的结果是一个范围向量。
句法:
<instant_query> '[' <range> ':' [<resolution>] ']' [ @ <float_literal> ] [ offset <duration> ]
<resolution>
是可选的。默认为全局评估区间。
参考链接
官网:https://prometheus.io/docs/prometheus/latest/querying/basics/