
Prometheus 如何用 fill 修饰符为二元运算符结果填充缺失匹配【免费下载链接】prometheusThe Prometheus monitoring system and time series database.项目地址: https://gitcode.com/GitHub_Trending/pr/prometheus当两个瞬时向量通过算术二元运算符组合时在另一侧找不到匹配条目的序列默认不会出现在结果向量中。如果你希望这些“单侧缺失”的序列以一个指定的默认样本值参与运算并出现在结果里PromQL 提供了fill修饰符fill(value)、fill_left(value)、fill_right(value)。fill 修饰符属于实验性功能必须先启用对应的 feature flag 才可用。前置条件启用 promql-binop-fill-modifiers feature flagfill 修饰符是实验性功能文档明确要求通过--enable-featurepromql-binop-fill-modifiers启用在 Prometheus 服务端启动时加上该参数prometheus --enable-featurepromql-binop-fill-modifiers几点依据文档的说明feature flag 通过--enable-feature参数启用多个特性用逗号分隔例如--enable-featurepromql-binop-fill-modifiers,其他特性见 feature flags 文档。在 prometheus 命令行参考 列出的有效选项中包含promql-binop-fill-modifiers。需要注意promtool 命令行参考 中--enable-feature的可选值只有promql-experimental-functions、promql-delayed-name-removal、promql-extended-range-selectors不包含promql-binop-fill-modifiers所以这个特性只能在 Prometheus 服务端启用而不是在查询客户端侧。feature flags 文档说明这类默认关闭的特性“其行为可能在后续版本中变化变化会通过 release changelog 传达”按实验性功能对待。三个修饰符及其取值规则operators 文档 定义了三个修饰符修饰符含义fill(value)用value填充任一侧缺失的匹配fill_left(value)用value填充左侧缺失的匹配fill_right(value)用value填充右侧缺失的匹配取值与组合规则value必须是一个表示 float 样本的数值字面量不支持 histogram 样本。这些修饰符只能填充在操作单侧缺失的序列如果一个序列在两侧都缺失无法通过这些修饰符创建出来。允许的组合只有以下五种fill(default)fill_left(default)fill_right(default)fill_left(default) fill_right(default)fill_right(default) fill_left(default)如果同时使用了bool、on、ignoring、group_left、group_right等其他二元运算符修饰符fill 修饰符必须写在最后。执行包含 fill 修饰符的查询feature flags 文档给出的最简示例rate(successful_requests[5m]) fill(0) rate(failed_requests[5m])服务端默认监听地址为0.0.0.0:9090--web.listen-address的默认值见 prometheus 命令行参考因此本地服务可用http://localhost:9090访问。下面以 operators 文档中的例子为例给出两种执行方式。主路径promtool 执行即时查询promtool query instant http://localhost:9090 method_code:http_errors:rate5m{status500} / ignoring(code) fill(0) method:http_requests:rate5mpromtool query instant接收两个必选参数serverPrometheus 服务地址和 exprPromQL 表达式见 promtool 命令行参考。替代路径HTTP APIHTTP API 文档 定义了即时查询端点GET /api/v1/queryquery参数传入表达式字符串需要 URL 编码表达式较长时文档建议使用POST方法和Content-Type: application/x-www-form-urlencoded请求体来传递以避免超出 URL 长度限制curl -X POST http://localhost:9090/api/v1/query --data-urlencode querymethod_code:http_errors:rate5m{status500} / ignoring(code) fill(0) method:http_requests:rate5m响应为 JSON成功时返回2xx状态码data.resultType为vectordata.result为结果向量。文档示例用 0 填充缺失匹配operators 文档给出的示例输入文档示例数据method_code:http_errors:rate5m{methodget, code500} 24 method_code:http_errors:rate5m{methodget, code404} 30 method_code:http_errors:rate5m{methodput, code501} 3 method_code:http_errors:rate5m{methodpost, code500} 6 method_code:http_errors:rate5m{methodpost, code404} 21 method:http_requests:rate5m{methodget} 600 method:http_requests:rate5m{methoddel} 34 method:http_requests:rate5m{methodpost} 120示例查询用0填充任一侧缺失的序列method_code:http_errors:rate5m{status500} / ignoring(code) fill(0) method:http_requests:rate5m文档示例结果{methodget} 0.04 # 24 / 600 {methodput} Inf # 3 / 0 (missing right side filled in) {methoddel} 0 # 0 / 34 (missing left side filled in) {methodpost} 0.05 # 6 / 120对照理解不带 fill 修饰符时put右侧method:http_requests:rate5m中没有put和del左侧没有del都没有匹配按默认行为不会出现在结果里加上fill(0)后它们以填充值0参与运算出现在结果中。注意示例中3 / 0得到Inf填充值按普通样本参与算术运算而二元算术运算符遵循 IEEE 754 浮点运算包括对NaN、Inf、-Inf的处理——这是文档示例展示的现象选择填充值时应当预期它会被真实代入运算。与 group_left / group_right 组合时的行为fill 修饰符可以和分组修饰符一起使用但文档明确了两种行为如果 fill 修饰符用在匹配的“many”侧它只会为每个匹配组的 many 侧填充一个序列以该组的匹配标签作为序列标识。如果 fill 修饰符用在“one”侧且分组修饰符指定了要包含的 one 侧标签名例如left_vector * on(instance, job) group_left(info_label) fill_right(1) right_vector那些标签对于缺失序列不会被填充因为没有值来源。限制实验性功能行为可能在后续版本变化。集合运算符and、or、unless不支持fill 修饰符因为这类运算符的作用就是按序列在另一向量中的存在或缺失来过滤。填充值不支持 histogram 样本只能是 float 数值字面量。两侧都缺失的序列无法被创建只能填充单侧缺失。与其他修饰符同用时fill 修饰符必须位于最后。验证结果文档没有单独的“健康检查”命令可直接利用文档描述的默认行为做对照验证先运行不带 fill 修饰符的同一表达式例如method_code:http_errors:rate5m{status500} / ignoring(code) method:http_requests:rate5m结果中不应包含单侧缺失的序列文档示例中put和del不会出现在结果里。再运行带fill(0)的表达式单侧缺失的序列应以填充值参与运算后出现在data.result中如文档示例所示put和del出现值分别为Inf和0。如果第 2 步中修饰符未生效表达式报未知修饰符、或结果与第 1 步相同先确认服务端启动参数中已包含--enable-featurepromql-binop-fill-modifiers并重启生效。延伸阅读operators 文档 的向量匹配章节以及 feature flags 文档 中的 fill 修饰符条目。【免费下载链接】prometheusThe Prometheus monitoring system and time series database.项目地址: https://gitcode.com/GitHub_Trending/pr/prometheus创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考