Prometheus【普罗米修斯】+Grafana部署企业级监控之 promQL语法

一、PromQL介绍

PromQL (Prometheus Query Language) 是 Prometheus 自己开发的数据查询 DSL 语言,语言表现力非常丰富,内置函数很多,在日常数据可视化以及rules 告警中都会使用到它。

1.1 表达式数据类型:

在prometheus的表达式中,一个表达式或子表达式可以分为以下四种类型之一:

即时向量(Instant vector):一组时间序列,每个时间序列包含一个样本,所有样本共享相同的时间戳

范围向量(Range vector):一组时间序列,其中包含每个时间序列随时间变化的一系列数据点

标量(Scalar):一个简单的数字浮点值

字符串(String):一个简单的字符串值,目前未使用

1.2 查询条件:

prometheus 存储的是时序数据,而它的时序是由metric名称和一组标签构成的,其实metric名称也可以写出标签的形式,例如prometheus_http_requests_total等价于{name="prometheus_http_requests_total"}

一个简单的查询相当于是对各种标签的筛选,例如:

prometheus_http_requests_total{code="200"}              #表示查询metric名称为 prometheus_http_requests_total,code 为 "200" 的数据

查询条件支持正则匹配,例如:

prometheus_http_requests_total{code!="200"}                 #表示查询 code 不为 "200" 的数据

prometheus_http_requests_total{code=~"2.."}                #表示查询 code 为 "2xx" 的数据

prometheus_http_requests_total{code!~"2.*"}                #表示查询 code 不为 "2xx" 的数据

1.3 操作符:

prometheus 查询语句中,支持常见的各种表达式操作符,例如:

算术运算符:+、-、*、/、%、^ ,比如 prometheus_http_requests_total * 2 表示将 prometheus_http_requests_total 所有数据乘以2

比较运算符:==、!=、>、<、>=、<= ,比如 prometheus_http_requests_total > 100 表示 prometheus_http_requests_total 结果中大于 100 的数据

逻辑运算符:and、or、unless ,比如 prometheus_http_requests_total == 5 or prometheus_http_requests_total == 2 表示 prometheus_http_requests_total 结果中等于 5 或者 2 的数据

聚合运算符:sum、min、max、avg、stddev、stdvar、count、count_values、bottomk、topk、quantile,比如 max(prometheus_http_requests_total) 表示 prometheus_http_requests_total 结果中最大的数据
注意,运算符也有优先级,它们遵从(^)> (*, /, %) > (+, -) > (==, !=, <=, <, >=, >) > (and, unless) > (or) 的原则。

1.4 内置函数:

prometheus 内置不少函数,方便查询以及数据格式化,例如将结果由浮点数转为整数的 floor 和 ceil

floor(avg(prometheus_http_requests_total{code="200"}))

ceil(avg(prometheus_http_requests_total{code="200"}))

查看 prometheus_http_requests_total 5分钟内平均每秒的数据

rate(prometheus_http_requests_total[5m])

常用内置函数:

abs(v instant-vector)               返回所有样本值均转换为绝对值的输入即时向量v

absent(v instant-vector)                如果传递给它的即时向量v有任何元素,则返回一个空向量;如果传递给它的即时向量v没有元素,则返回值为1的单元素向量

absent_over_time(v range-vector)                如果传递给它的范围向量v有任何元素,则返回一个空向量;如果传递给它的范围向量v没有元素,则返回值为1的单元素向量

avg_over_time(range-vector)             指定时间间隔内范围向量所有元素样本值的平均值

ceil(v instant-vector)              将即时向量v中所有元素的样本值向上取整到最接近的整数

changes(v range-vector)             对于范围向量v中的时间序列,返回其值在提供的时间范围内变化的次数作为一个即时向量

clamp_max(v instant-vector, max scalar)             将即时向量v中所有元素的样本值锁定上限为标量max

clamp_min(v instant-vector, min scalar)             将即时向量v中所有元素的样本值锁定下限为标量min

count_over_time(range-vector)               指定时间间隔内范围向量所有元素样本值的计数

day_of_month(v=vector(time()) instant-vector)               返回UTC中每个给定时间的月份。返回值是1到31

day_of_week(v=vector(time()) instant-vector)                返回UTC中每个给定时间的星期几。返回值是从0到6,其中0表示星期日

days_in_month(v=vector(time()) instant-vector)              返回UTC中每个给定时间的月份中的天数。返回值是28到31

delta(v range-vector)               计算范围向量v中每个时间序列元素的第一个值与最后一个值之间的差,并返回具有给定增量和相同标签的即时向量。delta 应仅与Gauge一起使用

deriv(v range-vector)               使用简单的线性回归来计算范围向量v中时间序列的每秒导数。deriv 应仅与Gauge一起使用

exp(v instant-vector)               计算即时向量v中的所有元素的指数函数。特殊情况是:Exp(+Inf) = +Inf、Exp(NaN) = NaN

floor(v instant-vector)             将即时向量v中所有元素的样本值向下取整到最接近的整数

hour(v=vector(tim
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值