java如何使用promql_PromQL

本文介绍了Prometheus查询语言PromQL的基础知识,包括查询结果类型、查询条件及内置函数等内容,并对比了irate与rate函数的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

prometheus_http_requests_total{code="200"}

查询结果类型

PromQL 查询结果主要有 3 种类型:

瞬时数据 (Instant vector): 包含一组时序,每个时序只有一个点,例如:prometheus_http_requests_total

区间数据 (Range vector): 包含一组时序,每个时序有多个点,例如:prometheus_http_requests_total[5m]

纯量数据 (Scalar): 纯量只有一个数字,没有时序,例如:count(prometheus_http_requests_total)

查询条件

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

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

http_requests_total{code="200"} // 表示查询名字为 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" 的数据

内置函数

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

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

ceil(avgprometheus_(http_requests_total{code="200"}))

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

rate(prometheus_http_requests_total[5m])

rate 与irate 区别:

irate和rate都会用于计算某个指标在一定时间间隔内的变化速率。但是它们的计算方法有所不同:irate取的是在指定时间范围内的最近两个数据点来算速率,而rate会取指定时间范围内所有数据点,算出一组速率,然后取平均值作为结果。

所以官网文档说:irate适合快速变化的计数器(counter),而rate适合缓慢变化的计数器(counter)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值