prometheus语法学习

目录

prometheus简介

1、指标内容

2、Prometheus中类型介绍

PromQL

时间序列选择器

即时矢量选择器

范围向量选择器

偏移修改器

@修饰符

子查询

函数

abs()

absent()

HTTP API


prometheus简介

1、指标内容

在 Prometheus 中,一个指标是指用于描述系统状态的特定数据流。一个指标通常包含以下内容:

  1. 指标名称(Metric Name):指标的唯一标识,用于区分不同的数据流。示例:http_requests_total

  2. 标签集(Label Set):用于区分同一指标不同数据流的关键标识信息。标签集是由键值对组成的元数据,通常包含有关监控对象的附加信息。示例:method="GET", status="200"

  3. 时间戳(Timestamp):数据点记录的时间戳,表示观测数据的时间。通常以 UNIX 时间戳的形式表示。

  4. 数值(Value):每个数据点对应的具体观测数值。示例:42.0

以下是一个指标内容

# HELP prometheus_http_requests_total Counter of HTTP requests.
# TYPE prometheus_http_requests_total counter
prometheus_http_requests_total{code="200",handler="/"} 0
prometheus_http_requests_total{code="200",handler="/-/healthy"} 0

表示指标帮助文档,解释一下指标的作用
# HELP prometheus_http_requests_total Counter of HTTP requests.

表示指标类型,counter是计数器类型,值只增不减

# TYPE prometheus_http_requests_total counter

这个指标包含了

指标名称:prometheus_http_requests_total,累计计算通常需要加后缀total,指标命名规则,可以参考:https://prometheus.io/docs/practices/naming/

abels(标签):code、handler

指标值:0

prometheus_http_requests_total{code="200",handler="/"} 0

2、Prometheus中类型介绍

指标数据类型

  • counter(计数器)
  • gauge (仪表类型)
  • histogram(直方图类型)
  • summary (摘要类型)

表达式语言数据类型

在 Prometheus 的表达式语言中,表达式或子表达式可以计算为以下四种类型之一:

  • 即时向量- 一组时间序列,每个时间序列包含一个样本,所有时间序列共享相同的时间戳
  • 范围向量- 一组时间序列,其中包含每个时间序列随时间变化的一系列数据点
  • 标量- 一个简单的浮点值
  • String - 一个简单的字符串值;目前未使用

PromQL

官方文档:https://prometheus.io/docs/prometheus/latest/querying/basics/#instant-vector-selectors

时间序列选择器

即时矢量选择器

即时向量选择器允许在给定时间戳(时间点)选择一组时间序列和每个时间序列的单个样本值。在最简单的形式中,仅指定指标名称,这会生成一个即时向量,其中包含具有该指标名称的所有时间序列的元素。

可以简单理解为查询当前瞬时值

此示例选择具有http_requests_total指标名称的所有时间序列:

http_requests_total

可以通过在大括号 ( {}) 中附加以逗号分隔的标签匹配器列表来进一步过滤这些时间序列。

标签匹配运算符:

  • =:与字符串匹配
  • !=:与字符串不匹配
  • =~:与正则匹配
  • !~:与正则不匹配

仅选择那些具有http_requests_total 指标名称且job标签设置为prometheus且 group标签设置为 的时间序列canary

http_requests_total{job="prometheus",group="canary"}

标签匹配器还可以通过与内部标签匹配来应用于指标名称 __name__。例如,该表达式http_requests_total相当于 {__name__="http_requests_total"}.也可以使用=!==~, )以外的匹配器。!~以下表达式选择名称以 开头的所有指标job:

{__name__=~"job:.*"}

指标名称不能是关键字boolonignoring和之一group_left,解决此限制的方法是使用标签__name__

{__name__="on"} # Good!
on{} # Bad!

范围向量选择器

即时向量选择器允许在给定时间戳(时间点)选择一组时间序列和每个时间序列的单个样本值。在最简单的形式中,仅指定指标名称,这会生成一个即时向量,其中包含具有该指标名称的所有时间序列的元素。

可以简单理解为查询这一段时间中的所有值

选择过去 5 分钟内为指标名称和job标签设置为 的所有时间序列记录的所有值

http_requests_total{job="prometheus"}[5m]

持续时间指定为数字,后跟以下单位之一:

  • ms- 毫秒
  • s- 秒
  • m- 分钟
  • h- 小时
  • d- 天 - 假设一天总是有 24 小时
  • w- 周 - 假设一周总是有 7d
  • y- 年 - 假设一年总是 365d

偏移修改器

offset修饰符允许更改查询中各个瞬时向量和范围向量的时间偏移。

例如,以下表达式返回 http_requests_total相对于当前查询评估时间过去 5 分钟的值:

http_requests_total offset 5m //GOOD
sum(http_requests_total{method="GET"} offset 5m) // GOOD.
sum(http_requests_total{method="GET"}) offset 5m // BAD.
rate(http_requests_total[5m] offset -1w) //GOOD

@修饰符

@修饰符允许更改查询中各个瞬时向量和范围向量的评估时间。提供给修饰符的时间@是 unix 时间戳,并用浮点文字描述。

例如,以下表达式返回 http_requests_totalat的值2021-01-04T07:40:00+00:00@修饰符始终需要立即跟随选择器

http_requests_total @ 1609746000
sum(http_requests_total{method="GET"} @ 1609746000) // GOOD.
sum(http_requests_total{method="GET"}) @ 1609746000 // INVALID.

子查询

子查询允许您针对给定范围和分辨率运行即时查询。子查询的结果是范围向量。

句法:<instant_query> '[' <range> ':' [<resolution>] ']' [ @ <float_literal> ] [ offset <duration> ]

  • <resolution>是可选的。默认为全局评估间隔。

sum(rate(http_requests_total[5m:1m])) 

  • <instant_query> 是 sum(rate(http_requests_total)),即时查询的评估表达式是计算每秒的HTTP请求数量,并将其求和。

  • <range> 是 5m,表示在过去5分钟的时间范围内执行子查询。

  • <resolution> 是 1m,指定子查询结果的分辨率为1分钟。

函数

abs()

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

absent()

absent(v instant-vector)如果传递给它的向量有任何元素(浮点数或本机直方图),则返回一个空向量;如果传递给它的向量没有元素,则返回值为 1 的 1 元素向量。

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

更多功能参考文档:https://prometheus.io/docs/prometheus/latest/querying/functions/

HTTP API

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