PromQL
PromQL (Prometheus Query Language) 是 Prometheus 自己开发的数据查询 DSL 语言,语言表现力非常丰富,内置函数很多,在日常数据可视化以及rule 告警中都会使用到它。
在页面 http://localhost:9090/graph 中,输入下面的查询语句,查看结果,例如:
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)。
分类:
运维监控
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?