如何用Grafana时序面板仅展示Redis Stream的price字段数据?
解决Grafana中Redis Stream仅展示price字段的方法
方法一:直接在Grafana面板里过滤字段(无需编写Lua)
这是最直接的处理方式,不需要修改Redis查询逻辑:
- 打开时序面板的编辑界面,切换到字段标签页
- 在字段列表里找到
amount、id这些不需要的字段,点击每个字段右侧的下拉菜单,选择隐藏字段 - 确认后面板就只会显示
price的曲线了
要是需要更灵活的控制,也可以用转换功能:
- 添加「过滤字段」转换,选择「保留字段」模式,只勾选
price和对应的时间字段(一般是Redis返回的时间戳字段)
方法二:用Lua脚本从Redis端过滤(适合复杂场景)
如果想从源头就只返回price数据,减少不必要的数据传输,可以用Lua脚本实现:
- 在Grafana的Redis数据源查询界面,切换到Lua模式
- 粘贴下面的脚本(Key使用你的变量
$trades,时间范围通过参数传入):
local key = ARGV[1] local start = ARGV[2] local stop = ARGV[3] local streamData = redis.call('XRANGE', key, start, stop) local filteredResult = {} for _, entry in ipairs(streamData) do local timestamp = entry[1] local fields = entry[2] local priceValue = nil -- 遍历字段找到price for i = 1, #fields, 2 do if fields[i] == 'price' then priceValue = fields[i+1] break end end if priceValue then table.insert(filteredResult, {timestamp, {price = priceValue}}) end end return filteredResult
- 在脚本参数里依次填入:
$trades、__from、__to(这两个是Grafana内置的时间变量,对应查询的起止时间戳) - 执行查询后,Redis只会返回时间和price的数据,Grafana面板自然就只展示price曲线了
总结
优先用方法一,操作简单高效;如果需要优化数据传输量,或者有更复杂的字段处理需求,再考虑用Lua脚本的方案。
内容的提问来源于stack exchange,提问作者Femto Trader
相关产品推荐
相关产品推荐

