promsql 基本使用

2021/10/10 16:50:16

本文主要是介绍promsql 基本使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

PromQL 基本使用

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

查询结果类型

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

  • 瞬时数据 (Instant vector): 包含一组时序,每个时序只有一个点,例如:http_requests_total
  • 区间数据 (Range vector): 包含一组时序,每个时序有多个点,例如:http_requests_total[5m]
  • 纯量数据 (Scalar): 纯量只有一个数字,没有时序,例如:count(http_requests_total)

查询条件

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

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

http_requests_total{code="200"}// 表示查询名字为 http_requests_total,code 为 "200" 的数据

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

http_requests_total{code!="200"}// 表示查询 code 不为 "200" 的数据http_requests_total{code=~"2.."}// 表示查询 code 为 "2xx" 的数据http_requests_total{code!~"2.."}// 表示查询 code 不为 "2xx" 的数据

操作符

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

算术运算符:

支持的算术运算符有 +,-,*,/,%,^, 例如 http_requests_total * 2 表示将 http_requests_total 所有数据2倍。

比较运算符:

支持的比较运算符有 ==,!=,>,<,>=,<=, 例如 http_requests_total > 100 表示 http_requests_total 结果中大于 100 的数据。

逻辑运算符:

支持的逻辑运算符有 and,or,unless, 例如 http_requests_total == 5 or http_requests_total == 2 表示 http_requests_total 结果中等于 5 或者 2 的数据。

聚合运算符:

支持的聚合运算符有 sum,min,max,avg,stddev,stdvar,count,count_values,bottomk,topk,quantile,, 例如 max(http_requests_total) 表示 http_requests_total 结果中最大的数据。

注意Prometheus 的运算符也有优先级,它们遵从(^)> (*, /, %) > (+, -) > (==, !=, <=, <, >=, >) > (and, unless) > (or) 的原则。

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

  • rate()

    • 此函数计算整个采样周期内每秒的增长率。

      例如:rate(http_requests_total[5m]) 得出的是HTTP在5分钟窗口内,平均每秒的请求率。作为最常见的函数,它以可预测的每秒输出单位产生平滑的rate。

  • irate()

    • 即 "瞬时rate",此函数和rate()一样,计算每秒的增长率,但只对规定采样周期内的最后两个样本进行计算,而忽略前面所有样本
  • increase()

    • increase(http_requests_total[5m]) 得出的是5分钟的采样周期内处理完成的HTTP请求的增长量
示例
increase(flink_jobmanager_job_numberOfFailedCheckpoints{env="prod-exclusive",projectCode="platform-v4",type="Flink"}[1h])  ##flink_jobmanager_job_numberOfFailedCheckpoints 1小时的增长量

参考文档

https://prometheus.io/docs/prometheus/latest/querying/functions/



这篇关于promsql 基本使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程