一、PromQL基础入门
1.1 PromQL简介
PromQL(Prometheus Query Language)是Prometheus内置的数据查询语言,支持对时间序列数据进行查询、聚合、逻辑运算等操作。它广泛应用于Prometheus的日常应用中,包括数据查询、可视化、告警处理等场景。
简单来说,所有需要用到数据筛选的地方,都会用到PromQL,例如监控指标的设置、报警指标的设置等。
1.2 基础查询示例
访问Prometheus Web UI,选择Graph,在查询框中输入:prometheus_http_requests_total并执行:
查询结果显示所有指标名称为prometheus_http_requests_total的数据。
二、时间序列与数据类型
2.1 时间序列概念
Prometheus将所有采集到的样本数据以时间序列的方式保存,每条时间序列通过指标名称和标签集命名。
样本组成:
指标(metric):metric name + labelsets
时间戳(timestamp):精确到毫秒的时间戳
样本值(value):float64浮点型数据
示例格式:
text
http_request_total{status="200", method="GET"}@1434417560938 => 943552.2 数据类型分类
PromQL表达式返回的结果有四种类型:
| 类型 | 说明 | 示例 |
|---|---|---|
| 瞬时向量 | 一组时间序列,每个时间序列包含单个样本(最新值) | prometheus_http_requests_total |
| 区间向量 | 一组时间序列,每个包含一段时间范围内的样本数据 | prometheus_http_requests_total[5m] |
| 标量 | 简单的数字浮点值 | 10 |
| 字符串 | 简单的字符串值 | "example" |
2.3 指标类型详解
Prometheus定义了4种指标类型:
1. Counter(计数器)
只增不减的计数器,用于记录累计值,如请求总量。
常用操作:
promql
# 获取HTTP请求量的增长率 rate(http_requests_total[5m]) # 查询访问量前10的HTTP请求 topk(10, http_requests_total)
2. Gauge(仪表盘)
可增可减的计量器,用于反应当前状态,如内存使用量。
常用操作:
promql
# 计算CPU温度在2小时内的差异 delta(cpu_temp_celsius{host="zeus"}[2h]) # 预测系统磁盘空间4小时后的剩余情况 predict_linear(node_filesystem_free_bytes[1h], 4 * 3600)3. Histogram(直方图)与 Summary(摘要)
用于统计和分析样本的分布情况,解决长尾问题。
Histogram示例:
text
prometheus_tsdb_compaction_chunk_range_seconds_bucket{le="100"} 71Summary示例:
text
prometheus_tsdb_wal_fsync_duration_seconds{quantile="0.5"} 0.012352463三、查询语法详解
3.1 完全匹配
使用=和!=进行标签的完全匹配。
示例:查看非200状态的请求
promql
prometheus_http_requests_total{code!="200"}3.2 正则匹配
使用=~和!~进行正则表达式匹配。
示例:查询handler以/api/v1开头的记录
promql
prometheus_http_requests_total{handler=~"/api/v1/.*"}3.3 范围查询
使用时间范围选择器[]定义查询时间范围。
示例:查询最近5分钟内的所有样本数据
promql
prometheus_http_requests_total{}[5m]时间单位:
s- 秒m- 分钟h- 小时d- 天w- 周y- 年
3.4 时间位移操作
使用offset关键字进行时间位移。
示例:
promql
# 查询5分钟前的最新数据 prometheus_http_requests_total{} offset 5m # 查询1天前的数据 prometheus_http_requests_total{}[1d] offset 1d四、聚合操作
4.1 基本聚合函数
PromQL提供多种聚合函数对时间序列进行处理:
| 函数 | 说明 | 示例 |
|---|---|---|
sum() | 求和 | sum(prometheus_http_requests_total) |
min() | 最小值 | min(prometheus_http_requests_total) |
max() | 最大值 | max(prometheus_http_requests_total) |
avg() | 平均值 | avg(prometheus_http_requests_total) |
count() | 计数 | count(prometheus_http_requests_total) |
count函数结果:
sum函数结果:
avg函数结果:
4.2 标准差与方差
用于描述数据的波动情况。
示例:
promql
# 计算标准差 stddev(prometheus_http_requests_total) # 计算标准方差 stdvar(prometheus_http_requests_total)
标准差说明图:
标准差查询结果:
4.3 排名函数
promql
# 获取前5位的请求 topk(5, prometheus_http_requests_total) # 获取后5位的请求 bottomk(5, prometheus_http_requests_total)
五、操作符详解
5.1 数学运算符
支持:+(加法)、-(减法)、*(乘法)、/(除法)、%(求余)、^(幂运算)
示例:将字节单位转换为MB
promql
prometheus_http_response_size_bytes_sum/8/1024
5.2 布尔运算符
支持:==(相等)、!=(不相等)、>(大于)、<(小于)、>=(大于等于)、<=(小于等于)
示例:筛选请求次数超过20次的接口
promql
prometheus_http_requests_total > 20
使用bool修饰符返回0/1值:
promql
prometheus_http_requests_total > bool 20
布尔运算符结果:
bool修饰符结果:
5.3 集合运算符
| 操作符 | 说明 | 示例 |
|---|---|---|
and | 与操作,返回两个向量中都存在的元素 | vector1 and vector2 |
or | 或操作,返回两个向量中的所有元素 | vector1 or vector2 |
unless | 排除操作,返回第一个向量中不在第二个向量中的元素 | vector1 unless vector2 |
5.4 操作符优先级
从高到低依次为:
^*,/,%+,-==,!=,<=,<,>=,>and,unlessor
六、内置函数实战
6.1 增长率计算
rate()函数
计算区间向量在时间窗口内的平均增长速率。
示例:
promql
rate(node_cpu[2m])
irate()函数
计算区间向量的瞬时增长率,灵敏度更高。
示例:
promql
irate(node_cpu[2m])
对比:
rate():适合长期趋势分析和告警规则irate():适合短期波动分析和瞬时状态监控
6.2 增长预测
predict_linear()函数基于简单线性回归预测时间序列的未来值。
示例:预测磁盘空间4小时后是否被占满
promql
predict_linear(node_filesystem_free{job="node"}[2h], 4 * 3600) < 06.3 其他常用函数
scalar()函数
将单个瞬时向量转换为标量。
字符串操作
直接使用字符串作为表达式会返回字符串。
七、实战技巧与最佳实践
7.1 查询优化建议
避免全量查询:尽量使用标签过滤减少数据量
合理使用范围:根据需求选择合适的时间范围
利用聚合:在查询时进行预聚合,减少后续处理压力
7.2 常见场景示例
场景1:监控错误率
promql
# 计算HTTP 5xx错误率 sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m]))场景2:CPU使用率计算
promql
# 节点CPU使用率 100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) * 100)场景3:内存使用率
promql
# 节点内存使用率 (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100
总结
本文全面介绍了PromQL的核心概念、语法规则、操作符和内置函数,通过丰富的示例演示了各种查询场景。掌握PromQL是有效使用Prometheus进行监控告警的关键,建议读者:
理解数据模型:熟悉时间序列、指标类型等基础概念
掌握查询语法:熟练使用标签匹配、范围查询、聚合操作
善用内置函数:合理运用rate、irate、predict_linear等函数
优化查询性能:避免低效查询,提高监控系统效率