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

Grafana变量多属性查询方案咨询:关联隐藏变量或复杂值处理

Grafana变量多属性查询解决方案

方案一:隐藏变量关联选中项

保留原有的item变量(用户可见,静态键值对:键为apple/banana等,供用户选择),新增隐藏变量实现类别关联:

  1. 创建名为category的变量,类型选「查询」,数据源选「Grafana」(用于静态逻辑处理)
  2. 在查询输入框编写条件映射语句,根据$item选中值返回对应类别:
    if $item == 'apple' then 'fruit'
    elif $item == 'banana' then 'fruit'
    elif $item == 'orange' then 'fruit'
    elif $item == 'grape' then 'fruit'
    elif $item == 'pea' then 'vegetable'
    
  3. 在变量设置的「Hide」选项中选择「Variable and value」,将该变量隐藏
  4. 面板查询中直接用${category}即可获取当前选中项的类别

方案二:复合文本值拆分提取

将item变量的静态值改为包含多属性的复合字符串,用统一分隔符(如下划线)分隔,再通过Grafana内置函数拆分:

  1. 修改item变量的静态值为:
    apple : red_fruit, banana : yellow_fruit, orange : orange_fruit, grape : purple_fruit, pea : green_vegetable
    
  2. 在面板查询中通过函数提取属性:
    • 获取颜色:split(${item:text}, "_")[0]
    • 获取类别:split(${item:text}, "_")[1]
      (若属性值可能包含下划线,推荐用正则提取更稳妥)
    • 正则提取颜色:regexReplaceAll(${item:text}, '^(.*)_(.*)$', '$1')
    • 正则提取类别:regexReplaceAll(${item:text}, '^(.*)_(.*)$', '$2')

方案对比

  • 方案一:逻辑独立清晰,支持复杂属性值,适合多属性场景;需维护额外隐藏变量
  • 方案二:无需额外变量,配置简单,适合属性少且值无分隔符冲突的场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 06:42:41