Grafana配置ClickHouse查询告警报错:input data must be a wide series
解决Grafana+ClickHouse告警报错及配置差值告警
问题重现
查询脚本(ClickHouse):
SELECT d AS day, awg_redirect_id, count(*) AS hits FROM input_data.dil WHERE puid IS NOT NULL AND day >= toDateTime($from) AND day <= toDateTime($to) GROUP BY day, awg_redirect_id ORDER BY day ASC, awg_redirect_id
图表展示正常,但告警配置时触发错误:
Error
Failed to evaluate queries and expressions: input data must be a wide series but got type not (input refid)
需求:当当前hits值与过去一周平均值相差50%时触发告警。
报错原因
Grafana告警引擎要求查询返回宽格式时间序列:每个维度(这里是awg_redirect_id)对应一个独立的数值列,时间作为唯一的行标识。而你的查询返回的是长格式数据(时间、维度标签、数值),不符合告警引擎的数据格式要求。
解决方案
1. 改写查询为宽格式输出
根据awg_redirect_id的取值数量,有两种方式:
方式一:使用pivot函数(推荐,适合维度值较多的场景)
要求ClickHouse版本≥21.8:
SELECT day, pivot(awg_redirect_id, hits) FROM ( SELECT d AS day, awg_redirect_id, count(*) AS hits FROM input_data.dil WHERE puid IS NOT NULL AND day >= toDateTime($from) AND day <= toDateTime($to) GROUP BY day, awg_redirect_id ) GROUP BY day ORDER BY day ASC
该查询会自动将每个awg_redirect_id转为单独的列,列名即为awg_redirect_id的取值,值对应该维度的hits数。
方式二:条件聚合手动转列(适合维度值固定且较少的场景)
如果awg_redirect_id的取值是已知的固定值,可直接写死:
SELECT d AS day, countIf(awg_redirect_id = 'redirect_001') AS hits_redirect_001, countIf(awg_redirect_id = 'redirect_002') AS hits_redirect_002, -- 按实际awg_redirect_id取值依次添加 FROM input_data.dil WHERE puid IS NOT NULL AND day >= toDateTime($from) AND day <= toDateTime($to) GROUP BY d ORDER BY d ASC
2. 配置告警规则表达式
查询转为宽格式后,即可在Grafana告警中设置差值判断:
- 添加两个查询:
- 查询A:计算过去一周的平均值,使用
avg_over_time函数,比如avg_over_time(hits_redirect_001[$7d]) - 查询B:获取当前最新的
hits值,直接引用对应列的最新点
- 查询A:计算过去一周的平均值,使用
- 设置告警条件表达式:
该表达式表示:当前值低于平均值的50%,或高于平均值的150%时触发告警。($B < $A * 0.5) OR ($B > $A * 1.5)
3. 关键注意事项
- 确保告警查询的时间范围覆盖过去一周:在Grafana告警规则的“查询选项”中,将时间范围设置为
now()-7d到now(),或者调整$from变量为now()-7d。 - 如果使用
pivot函数,需确认ClickHouse版本支持,若版本过低可升级或改用条件聚合方式。 - 若部分
awg_redirect_id存在数据缺失的情况,可在查询中添加COALESCE函数补0,避免告警计算时出现空值错误。
内容的提问来源于stack exchange,提问作者howtoplay112
相关产品推荐
相关产品推荐

