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

如何在Prometheus/Grafana中筛选存在于A查询但不在B查询的客户ID

在Prometheus中统计满足条件的客户数量

Prometheus单查询方案

直接使用unless操作符筛选出仅存在于第一个查询结果中的客户,再用count()统计数量:

count(
  sum(usage{usage_type="type_b"}) by (customer_id)
  unless
  count(service_plan{plan_type=~".*plan_b.*"}) by (customer_id)
)
  • sum(usage{usage_type="type_b"}) by (customer_id):获取所有使用过type_b服务的客户及其总使用量
  • unless:保留左侧查询结果中,在右侧查询结果里没有相同customer_id标签的序列
  • 外层count():统计最终剩余的序列数量,也就是符合条件的客户数

Grafana转换技巧方案

如果需要同时查看客户明细或更灵活的处理逻辑,可以用Grafana的Transform功能实现:

  1. 在面板中添加两个查询:
    • 查询1:sum(usage{usage_type="type_b"}) by (customer_id)(命名为「Type B用户」)
    • 查询2:count(service_plan{plan_type=~".*plan_b.*"}) by (customer_id)(命名为「Plan B用户」)
  2. 添加「Join」转换:
    • 选择两个查询作为数据源,关联字段选customer_id,连接类型选「左连接」(保留Type B用户的所有条目)
  3. 添加「Filter」转换:
    • 筛选条件设置为「Plan B用户」字段为空(即该客户不在Plan B用户结果中)
  4. 添加「Reduce」转换:
    • 选择「统计行数」,即可得到符合条件的客户数量

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 17:48:11