You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Prometheus指标按指定标签相乘并保留全部标签?

问题描述

现有以下两个Prometheus指标:

metric1{identifier="test", name="first"} 100
metric1{identifier="test", name="second"} 200

metric2{identifier="test", othername="third"} 2

需要编写PromQL语句,将两个指标基于identifier标签相乘,同时保留结果中的所有其他标签(name和othername),预期结果如下:

resultmetric{identifier="test", name="first", othername="third"} 200
resultmetric{identifier="test", name="second", othername="third"} 400

直接相乘因标签集合不同无法实现,需找到可行方法。

实现方法

可以使用PromQL的标签匹配修饰符来实现需求,具体语句如下:

metric1 * on(identifier) group_left(othername) metric2

语句说明

  • on(identifier):指定仅通过identifier标签进行指标匹配,忽略两个指标间其他标签的差异;
  • group_left(othername):表示保留左侧指标(metric1)的所有标签,同时将右侧指标(metric2)的othername标签合并到结果中。由于每个identifier对应的metric2序列唯一,不会出现匹配冲突。

执行该语句后,每个metric1的序列都会与同identifier的metric2序列相乘,结果标签包含identifier、name和othername,数值为两者的乘积,完全符合预期。

内容的提问来源于stack exchange,提问作者serge1peshcoff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:55:20