PromQL 基本使用

PromQL是Prometheus的数据查询语言,用于查询和分析时序数据。本文介绍了PromQL的基本使用,包括字符串和数字、查询结果类型、查询条件、操作符及内置函数。通过示例,展示了如何筛选和操作时序数据,如利用正则匹配条件、算术和比较运算符,以及聚合函数进行数据聚合。此外,还探讨了PromQL中的逻辑运算和内置函数,如rate()用于计算速率。
摘要由CSDN通过智能技术生成

PromQL 基本使用

PromQL 基本使用

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

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

http_requests_total{code="200"}

字符串和数字

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

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

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

3-2.4

查询结果类型

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 所有数据 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) 的原则。

内置函数

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])
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Prometheus是一个开源的系统监控和警报工具,它支持多种数据采集方式,包括自主的Prometheus采集器、服务端的exporter插件以及第三方的pushgateway等。以下是Prometheus的基本使用方法: 1. 安装和启动Prometheus Prometheus可以在官网上下载二进制文件,也可以使用容器来部署。安装好后,可以使用命令行启动Prometheus: ``` ./prometheus --config.file=prometheus.yml ``` 其中,prometheus.yml是一个配置文件,用于指定数据采集器和数据存储方式等。 2. 配置数据采集器 Prometheus支持多种数据采集器,包括自主的Prometheus采集器、服务端的exporter插件以及第三方的pushgateway等。可以在prometheus.yml文件中配置数据采集器: ``` scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100'] ``` 以上配置实现了对本地的Prometheus采集器和node_exporter采集器的监控。 3. 查询数据 Prometheus提供了一个查询语言PromQL,用于查询和分析采集到的数据。可以使用Prometheus的WebUI或API查询数据: ``` http://localhost:9090/graph?g0.range_input=1h&g0.expr=node_cpu{mode="idle"}&g0.tab=0 ``` 以上查询语句查询了最近1小时内所有节点的空闲CPU时间。 4. 设置警报规则 Prometheus支持设置警报规则,当某个监控指标超过或低于特定阈值时,会触发警报。可以在prometheus.yml文件中配置警报规则: ``` rules: - alert: HighCPUUsage expr: node_cpu{mode="user"} > 90 for: 5m labels: severity: warning annotations: summary: "High CPU usage detected" description: "CPU usage for {{ $labels.instance }} is {{ $value }}%" ``` 以上规则表示,当某个节点的用户CPU占用率超过90%持续5分钟时,会触发一个警报,并在警报信息中包含节点名称和CPU占用率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王伯爵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值