向 Prometheus 收集器添加一些 Prometheus 查询函数¶
https://storyboard.openstack.org/#!/story/2006427
问题描述¶
Prometheus 收集器无法处理 Counter 和 Gauge 数据类型。
一个 Counter 是一个累积指标,表示一个单调递增的计数器,其值只能增加或在重启时重置为零。例如,您可以使用计数器来表示服务的请求数、完成的任务数或错误数。
一个 Gauge 是一个指标,表示一个可以任意上升和下降的单个数值。
提议的变更¶
如果使用 delta 函数,就可以获得两个收集周期之间的差异,从而对 Counter 和 Gauge 类型的指标进行计费。
在 metrics.yml 文件的 extra_args 部分添加两个字段(range_function 和 query_function)可以解决此问题。
Prometheus 函数可以分为两类,每一类都与新的字段之一匹配
一些函数将 Range Vector 作为参数,而另一些函数将 Instant Vector 作为参数。两种类别都返回一个 Instant Vector。
由于预期的结果格式,新字段将仅允许有限的一组函数。
将允许以下 Prometheus 查询函数
接受 Range Vector 的函数
changes()delta()deriv()idelta()irange()irate()rate()
接受 Instant Vector 的函数
abs()ceil()exp()floor()ln()log2()log10()round()sqrt()
接受 Range Vector 的函数将通过新的 range_function 字段设置。它们将替换收集器应用于获取 Instant Vector 的当前隐式 {aggregation_method}_over_time 函数。该字段将是可选的,并且默认值为 {aggregation_method}_over_time。
接受 Instant Vector 的函数将通过新的 query_function 字段设置。这将允许在对数据进行速率计算之前应用额外的转换。
这两个字段可以组合使用。
示例¶
以下配置
metrics:
gateway_function_invocation_total:
unit: total
groupby:
- function_name
- code
extra_args:
aggregation_method: max
range_function: delta
将产生此查询:max(delta(gateway_function_invocation_total{}[3600s])) by (function_name,code)
另一个例子
metrics:
gateway_function_invocation_total:
unit: total
groupby:
- function_name
- code
extra_args:
aggregation_method: max
range_function: delta
query_function: abs
将产生:max(abs(delta(gateway_function_invocation_total{}[3600s]))) by (function_name,code)
还有这个
metrics:
gateway_function_invocation_total:
unit: total
groupby:
- function_name
- code
extra_args:
aggregation_method: max
query_function: abs
将产生:max(abs(max_over_time(gateway_function_invocation_total{}[3600s]))) by (function_name,code)
备选方案¶
在某些情况下可以使用 PyScript 模块,但对于一些简单的操作来说有点复杂。它还需要保存指标的最新状态(在 delta 函数的情况下)。
数据模型影响¶
无
REST API 影响¶
无
安全影响¶
无
通知影响¶
无
其他最终用户影响¶
最终用户将能够对 Prometheus 收集器检索到的指标执行更多操作,尤其是在 Gauge 和 Counter 指标上。
性能影响¶
无
其他部署者影响¶
无
开发人员影响¶
无
实现¶
负责人¶
- 主要负责人
<aimbot31>
工作项¶
为 Prometheus 收集器添加对
query_function字段的支持为 Prometheus 收集器添加对
range_function字段的支持
依赖项¶
无
测试¶
提议的更改将使用单元测试进行测试。
文档影响¶
将在 : Admin/Configuration/Collector 中添加一个条目,详细介绍新字段的配置。
参考资料¶
Prometheus 函数文档:https://prometheus.ac.cn/docs/prometheus/latest/querying/functions/
Prometheus 类型文档:https://prometheus.ac.cn/docs/prometheus/latest/querying/basics/