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

PromQL如何按镜像仓库、标签同时返回CPU用量与容器数指标

Sysdig单条PromQL查询同维度返回多指标实现方案

现有查询逻辑

  • 指标A(记为Value_A):按镜像仓库、镜像标签维度聚合CPU使用总核数
    sum by(container_image_repo,container_image_tag) (sysdig_container_cpu_cores_used)
    
  • 指标B(记为Value_B):按相同维度统计运行中的容器总数量
    count by(container_image_repo,container_image_tag)(sysdig_container_info)
    

此前写法的问题

之前使用*on()关联两个查询的写法属于PromQL算术运算,逻辑是对匹配到相同标签的两个时序值做乘法,最终只会返回A*B的单值结果,无法输出两个独立指标值。

-- 该写法仅返回两个值的乘积,不符合需求
sum by(container_image_repo,container_image_tag) (sysdig_container_cpu_cores_used) *on (container_image_repo,container_image_tag) (count by(
container_image_repo,container_image_tag)(sysdig_container_info))

正确实现方式

PromQL原生单条时间序列仅支持返回一个数值,要在单请求内返回同维度的两个独立指标,需要给两个子查询的结果添加自定义标识标签,再通过or运算符合并结果集:

# 给CPU使用量指标打类型标识标签
label_replace(
  sum by(container_image_repo,container_image_tag) (sysdig_container_cpu_cores_used),
  "metric_type", "cpu_cores_used", "", ""
)
or
# 给运行容器数指标打同规则的类型标识标签
label_replace(
  count by(container_image_repo,container_image_tag)(sysdig_container_info),
  "metric_type", "running_container_count", "", ""
)

结果适配配置

上述查询会返回两组时间序列,两组序列共享container_image_repo、container_image_tag标签,通过metric_type标签区分指标类型。在Sysdig监控面板的表格视图中做如下配置,即可得到「镜像仓库 镜像标签 Value_A Value_B」的四列输出结构:

  • 行分组维度选择container_image_repo、container_image_tag
  • 列拆分维度选择metric_type
  • 展示值选择时序采样值

不推荐将其中一个指标值转为标签挂载到另一条时序上的实现方式:标签值为字符串格式,无法后续参与数值计算、告警阈值判断等操作,实用性极差。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 21:48:10