智能运维-Prometheus时序数据库PromQL使用(基本语法、API调用等)

PromQL是Prometheus的数据查询语言,用于数据可视化和规则告警。本文介绍了PromQL的基本语法,包括字符串和数字、查询结果类型、查询条件、操作符、内置函数,以及使用示例。同时,讨论了PromQL在数据处理中的应用,如理解时间序列、计数器和仪表盘,并展示了Prometheus API的查询方法,包括直接查询指标、按标签过滤和正则匹配。
摘要由CSDN通过智能技术生成

一、PromQL 基本使用

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

在页面 http://localhost:9090/graph 中,输入下面的查询语句,查看结果,例如:

http_requests_total{code="200"}

1、字符串和数字

字符串: 在查询语句中,字符串往往作为查询条件 labels 的值,和 Golang 字符串语法一致,可以使用 "", '', 或者 ````, 格式如:

"this is a string"
'these are unescaped: \n \\ \t'
`these are not unescaped: \n ' " \t`

正数,浮点数: 表达式中可以使用正数或浮点数,例如:

3
-2.4

2、查询结果类型

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

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

3、查询条件

​ 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" 的数据

4、操作符

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

算术运算符:

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

比较运算符:

支持的比较运算符有 ==,!=,>,<,>=,<=, 例如 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) 的原则。

5、内置函数

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

floor(avg(http_requests_total{code="200"}))
ceil(avg(http_requests_total{code="200"}))

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

rate(http_requests_total[5m])
abs()

abs(v instant-vector) 返回所有样本值均转换为绝对值的输入向量。

absent()

absent(v instant-vector) 如果传递给它的向量具有任何元素,则返回一个空向量;如果传递给它的向量没有元素,则返回值为1的1元素向量。

这对于在给定度量标准名称和标签组合不存在任何时间序列时发出警报非常有用。

absent(nonexistent{job="myjob"})
# => {job="myjob"}

absent(nonexistent{job="myjob",instance=~".*"})
# => {job="myjob"}

absent(sum(nonexistent{job="myjob"}))
# => {}

在前两个示例中,absent()尝试聪明地从输入向量中导出1元素输出向量的标签。

absent_over_time()

absent_over_time(v range-vector) 如果传递给它的范围向量有任何元素,则返回一个空向量;如果传递给它的范围向量没有元素,则返回值为1的1元素向量。

这对于在给定的度量标准名称和标签组合在一定时间内没有时间序列时发出警报是很有用的。

absent_over_time(nonexistent{job="myjob"}[1h])
# => {job="myjob"}

absent_over_time(nonexistent{job="myjob",instance=~".*"}[1h])
# => {job="myjob"}

absent_over_time(sum(nonexistent{job="myjob"})[1h:])
# => {}

在前两个示例中,absent_over_time()尝试聪明地从输入向量中导出1元素输出向量的标签。

ceil()

ceil(v instant-vector)将所有元素的样本值四舍五入v到最接近的整数。

changes()

对于每个输入时间序列,changes(v range-vector)返回其值在提供的时间范围内变化的次数作为即时向量。

clamp_max()

clamp_max(v instant-vector, max scalar)将所有元素的样本值钳位为的v上限

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Prometheus时序数据库提供了强大的Promql查询语言来满足我们的查询需求。在查询数据之前,我们需要先插入数据到Prometheus中。在之前的博客中,详细介绍了Prometheus数据的插入过程\[1\]。而在查询数据时,我们可以使用Promql来进行查询。Promql是一种灵活的查询语言,可以根据我们的需求进行千变万化的查询\[2\]。 在进行查询时,Prometheus会根据指定的时间窗口来过滤数据,默认的时间窗口是5分钟,可以通过启动参数来进行设置\[3\]。这样可以确保我们只获取到指定时间范围内的数据,而不会包含过去或未来的数据。 总结来说,Prometheus时序数据库通过Promql查询语言来满足我们的查询需求,并通过时间窗口来过滤数据,确保我们获取到的数据符合我们的要求。 #### 引用[.reference_title] - *1* [Prometheus时序数据库-数据的查询](https://blog.csdn.net/weixin_55416758/article/details/115350199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Prometheus时序数据库-报警的计算 及 Prometheus时序数据库-数据的查询](https://blog.csdn.net/m0_67322837/article/details/125004715)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值