Contents

PromQL

Prometheus提供了一种功能强大的表达式语言PromQL(Prometheus QueryLanguage)。PromQL允许用户实时选择和汇聚时间序列数据,是Prometheus自己开发的数据查询DSL语言,使用这个查询语言能够进行各种聚合、分析和计算,使管理员能够根据指标更好地了解系统性能。本章首先对时序数据库进行介绍,了解其应用场景;然后对PromQL内容进行说明,包括PromQL数据类型、时序选择器、聚合操作、各类运算符和函数;最后通过简单的示例对PromQL查询操作进行汇总分析。

Prometheus还提供了下列内置的聚合操作符,这些操作符作用域瞬时向量。可以将瞬时表达式返回的样本数据进行聚合,形成一个新的时间序列。

  • sum (求和)

  • min (最小值)

  • max (最大值)

  • avg (平均值)

  • stddev (标准差)

  • stdvar (标准差异)

  • count (计数)

  • count_values (对value进行计数)

  • bottomk (后n条时序)

  • topk (前n条时序)

  • quantile (分布统计)

使用聚合操作的语法如下:

<aggr-op>([parameter,] <vector expression>) [without|by (<label list>)]

其中只有count_values, quantile, topk, bottomk支持参数(parameter)

without用于从计算结果中移除列举的标签,而保留其它标签。by则正好相反,结果向量中只保留列出的标签,其余标签则移除。通过without和by可以按照样本的问题对数据进行聚合。

例如:

sum(prometheus_http_requests_total) without (instance)

等价于

sum(prometheus_http_requests_total) by (code,handler,job,method)

略有不同是排序

如果只需要计算整个应用的HTTP请求总量,可以直接使用表达式:

sum(prometheus_http_requests_total)

count_values用于时间序列中每一个样本值出现的次数。count_values会为每一个唯一的样本值输出一个时间序列,并且每一个时间序列包含一个额外的标签。

例如:

count_values("count", prometheus_http_requests_total)

topk和bottomk则用于对样本值进行排序,返回当前样本值前n位,或者后n位的时间序列。

获取HTTP请求数前5位的时序样本数据,可以使用表达式:

topk(5, prometheus_http_requests_total)

quantile用于计算当前样本数据值的分布情况quantile(φ, express)其中0 ≤ φ ≤ 1。

例如,当φ为0.5时,即表示找到当前样本数据中的中位数:

quantile(0.5, prometheus_http_requests_total)

prometheus-监控项

https://blog.csdn.net/han949417140/article/details/112462319

使用正则表达式过滤变量

https://grafana.com/docs/grafana/latest/variables/filter-variables-with-regex/#filter-and-modify-using-named-text-and-value-capture-groups

node-exporter常用指标含义

https://blog.csdn.net/yjph83/article/details/84909319

https://www.cnblogs.com/robinunix/p/11276296.html

参考文献:

https://zhuanlan.zhihu.com/p/121104877

https://www.bookstack.cn/read/prometheus-book/promql-README.md

https://www.bookstack.cn/read/prometheus_practice/promql-summary.md