普罗米修斯 returns "ranges only allowed for vector selectors"

Prometheus returns "ranges only allowed for vector selectors"

这是我的查询,它应该显示 taskcnt.*:

形式的每个计数器的增量
delta(label_replace({__name__=~"taskcnt.*"}, "old_name", "", "__name__", "(.+)")[1w])

我得到:

Error executing query: 1:83: parse error: ranges only allowed for vector selectors

基本上,没有 label_replace 我会得到:

vector cannot contain metrics with the same labelset

我怎样才能让这个查询起作用?

子查询确实是您所需要的(感谢 M. Doubez 上面的评论者)。这应该对您有用 - 它通过每天计算子查询来计算每周增量(参见 [1w:1d]

delta(label_replace({__name__=~"desired_metric_prefix_.+_suffix"}, "metric_name", "", "__name__", "desired_metric_prefix_(.+)_suffix")[1w:1d])

确保与您的正则表达式匹配的所有指标都与 label_replacedelta 函数兼容。如果您在 Grafana 中显示它,请使用 Legend 表达式 {{ metric_name }} 显示每个系列的提取 metric_name

{__name__=~"taskcnt.*"} 可能 return 具有相同标签集但具有不同指标名称的多个时间序列。例如,它可能 return 以下时间序列:

taskcnt_foo{job="x",instance="y"}
taskcnt_bar{job="x",instance="y"}

如您所见,这些时间序列具有相同的标签集 - {job="x",instance="y"},但它们具有不同的指标名称:taskcnt_footaskcnt_bar

delta() function strips metric names from results, so it strips taskcnt_foo and taskcnt_bar names from the original time series and returns two time series with identical labelset - {job="x",instance="y"} when {__name__=~"taskcnt.*"} series selector传递给它。这会导致 vector cannot contain metrics with the same labelset 错误,因为 returned 时间序列无法相互区分。

不幸的是,Prometheus 不允许在类似 delta() 的函数中使用 label_replace() 或任何其他函数,这些函数接受例如range vector - 以方括号结尾的 series selector(例如,{__name__=~"taskcnt.*"}[1w])。

使用 subqueries, which is recommended in 有一个 half-working 的解决方案。不幸的是,这个解决方案有以下缺点:

  • Prometheus 子查询在执行期间可能会占用大量额外的 CPU、内存和磁盘 IO,尤其是当 interval 值与方括号中的后向 window 相比太小时: [window:interval].
  • 当方括号中的interval太大时,Prometheus子查询在计算过程中可能会丢失原始原始数据。
  • Prometheus 子查询很容易 mis-use 如果 their behavior 理解不清楚。

这些问题由 VictoriaMetrics in MetricsQLkeep_metric_names 修饰符解决,可应用于任何函数。例如,以下 MetricsQL 查询不会从结果中去除指标名称,因此它不会 return 既不会 vector cannot contain metrics with the same labelset 也不会 ranges only allowed for vector selectors 错误:

increase({__name__=~"taskcnt.*"}[1w]) keep_metric_names