PromQL全解析 (4)

label_join(v instant-vector, dst_label string, separator string, src_label_1 string, src_label_2 string, ...)为每个时间序列添加一个label,值为指定旧label的value连接

label_join(up{instance="localhost:9100", job="node"},"new_label","-","instance","job")

结果:

up{instance="localhost:9100", job="node", new_label="localhost:9100-node"} 1

label_replace()

label_replace(v instant-vector, dst_label string, replacement string, src_label string, regex string)从源label中获取value元素用于添加新的label

$1 获取正则匹配,匹配值添加到hello标签中

label_replace(up{instance="localhost:9100", job="node"},"hello","$1","job","(.*)")

结果:

up{hello="node", instance="localhost:9100", job="node"} 1 预测

predict_linear()

predict_linear(v range-vector, t scalar) 通过简单线性回归预测t秒后的样本值,与gauge一起使用。

根据过去1小时的文件系统剩余空间量,预测1小时之后的剩余空间

predict_linear(node_filesystem_free_bytes[1h],3600) 转换

absent()

absent(v instant-vector)如果向量有元素,则返回一个空向量;如果向量没有元素,则返回值为 1。

设置如下告警表达式:

absent(up{job="node"} == 1)

由于up{job="node"} 不存在或值不为1则告警表达式的值为1 产生告警

absent_over_time()

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

如果up{job="node1"}在某段时间不存在则返回1

absent_over_time(up{job="node1"}[1h])

scalar()

scalar(v instant-vector)以标量形式返回该单元素的样本值,如果输入向量不是正好一个元素,scalar将返回NaN.

vector()

vector(s scalar)将标量作为没有标签的向量返回。

sgn()

sgn(v instant-vector)返回一个向量,其中所有样本值都转换为1或-1或0

定义如下:

如果 v 为正,则为 1

如果 v 为负,则为 -1

如果 v 等于 0,则为 0。

排序

sort()

sort(v instant-vector)返回按样本值升序排序的向量元素。

sort_desc()

与sort()相反,按降序排序。

_over_time()

下面的函数列表允许传入一个范围向量,返回一个带有聚合的瞬时向量:

avg_over_time(range-vector): 区间向量内每个度量指标的平均值。

min_over_time(range-vector): 区间向量内每个度量指标的最小值。

max_over_time(range-vector): 区间向量内每个度量指标的最大值。

sum_over_time(range-vector): 区间向量内每个度量指标的求和值。

count_over_time(range-vector): 区间向量内每个度量指标的样本数据个数。

quantile_over_time(scalar, range-vector): 区间向量内每个度量指标的样本数据值分位数,φ-quantile (0 ≤ φ ≤ 1)

stddev_over_time(range-vector): 区间向量内每个度量指标的总体标准偏差。

stdvar_over_time(range-vector): 区间向量内每个度量指标的总体标准方差

数学函数

abs()

abs(v instant-vector) 返回样本的绝对值。

sqrt()

sqrt(v instant-vector)计算样本值的平方根。

deriv()

deriv(v range-vector) 使用简单线性回归计算时间序列在范围向量中的每秒导数。与指标类型gauge一起使用

exp()

exp(v instant-vector)计算样本值的指数函数。

特殊情况:

Exp(+Inf) = +Inf

Exp(NaN) = NaN

ln()、log2()、log10()

ln/log2/log10(v instant-vector) 计算样本值对数

特殊情况(同适用于log2/log10):

ln(+Inf) = +Inf

ln(0) = -Inf

ln(x < 0) = NaN

ln(NaN) = NaN

holt_winters()

holt_winters(v range-vector, sf scalar, tf scalar)基于访问向量v,生成时间序列数据平滑数据值。平滑因子sf越低, 对旧数据越重要。趋势因子tf越高,更关心趋势数据。0<sf,tf<=1。 与gauge一起使用

三角函数、弧度

acos(v instant-vector)

acosh(v instant-vector)

asin(v instant-vector)

asinh(v instant-vector)

atan(v instant-vector)

atanh(v instant-vector)

cos(v instant-vector)

cosh(v instant-vector)

sin(v instant-vector)

sinh(v instant-vector)

tan(v instant-vector)

tanh(v instant-vector)

角度、弧度转化

deg(v instant-vector)

pi()

rad(v instant-vector)

如果内容有误请指正。通过博客阅读:iqsing.github.io

参考

[1] Understanding Prometheus Range Vectors: https://satyanash.net/software/2021/01/04/understanding-prometheus-range-vectors.html

[2] promethues: https://prometheus.io/docs/prometheus/latest/querying/basics/

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjdys.html