正则表达式对系列的普罗米修斯率 [英] prometheus rate on series by regex

查看:85
本文介绍了正则表达式对系列的普罗米修斯率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下查询根据名称获取一些指标:

I am using the following query to get some metrics based on the name:

{__name__=~"bus_listener.+_processed"}

有多个指标与此名称匹配,并且多个应用正在发布这些指标.

There are multiple metrics that match this name and multiple apps are publishing these metrics.

我正在尝试计算一个比率,使用:

I am trying to calculate a rate on this, with:

rate({__name__=~"bus_listener.+_processed"}[5m])

但这给了我以下错误:

vector cannot contain metrics with the same labelset

我不能使用记录指标,我只能访问 grafana,它从普罗米修斯读取指标.

I cannot use recording metrics, I only have access to grafana, which reads metrics from prometheus.

我如何使用正则表达式获得此费率?

How can I get this rate using a regex?

推荐答案

听起来您有多个具有相同标签的指标(__name__ 除外).rate() 保留除 __name__ 之外的所有标签,但它会删除 __name__ 以避免任何混淆.这意味着如果您有两个时间序列,例如:

Sounds like you have multiple metrics with the same labels (except for __name__). rate() keeps all labels except __name__, but it drops __name__ to avoid any confusion. Meaning that if you have two time series like:

bus_listener_foo_processed{job="a_job"} 1
bus_listener_bar_processed{job="a_job"} 2

将它们通过 rate() 将导致两个时间序列都具有相同的标签集:

putting them through rate() will result in two time series both with the same labelset:

{job="a_job"} 0.1
{job="a_job"} 0.2

理论上,您可以通过首先使用 label_replace() 并对结果应用 rate()__name__ 标签复制为其他标签其中,导致每个原始时间序列的不同标签集.但是,由于您只能直接在时间序列上计算 rate()(而不是另一个函数的输出),因此您只能使用 子查询,这既是重量级的,又比其他方式慢:

In theory you could duplicate the __name__ label as some other label by using label_replace() first and applying rate() on the result of that, resulting in different labelsets for each original timeseries. However, since you can only compute rate() directly on a timeseries (not the output of another function) you can only do this by using subqueries, which is both heavyweight and slower than it would otherwise be:

rate(label_replace({__name__=~"bus_listener.+_processed"}, "old_name", "$1", "__name__", "(.+)")[5m:1m])

(可选地将 1m 替换为与您的抓取间隔接近的内容,以便尽可能减少混叠.)

(Optionally replacing 1m with something close to your scrape interval, so there's as little aliasing going on as possible.)

但理想情况下,如果您确实有权访问 Prometheus 配置(这似乎不太可能,因为您说您不能使用记录规则)您应该在摄取时使用度量重新标记来提取度量的各个位命名到单独的标签中,这样您以后就不必跳过箍了.或者让导出原始指标的服务使用标签而不是将它们连接到指标名称中.

But ideally, if you do have access to the Prometheus configuration (which doesn't seem likely, since you say you can't use recording rules) you should use metric relabeling at ingestion time to extract the various bits of the metric name into separate labels, so you won't have to jump through hoops later on. Or have the service that exports the original metrics use labels instead of concatenating them into the metric name.

这篇关于正则表达式对系列的普罗米修斯率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆