如何调整AWS CloudWatch Logs Insights查询以生成设备维度成功率双折线图
AWS CloudWatch Logs Insights 双折线图查询调整方法
问题背景
要在CloudWatch Logs Insights里生成双折线图,分别展示移动、桌面用户的成功率。使用以下查询后:
| fields properties.device as device, properties.success as success | stats avg(success) by device, bin(1hour)
返回的数据格式如下:
# device bin(1hour) avg(success) 1 desktop 2023-02-01T10:00:00.000 0.6129 2 mobile 2023-02-01T10:00:00.000 0.7453 3 desktop 2023-02-01T09:00:00.000 0.5578 4 mobile 2023-02-01T09:00:00.000 0.6082
可视化标签页却提示错误:
The data is not suitable for a line chart.
Try a bar chart, or group your result by bin function.
核心问题是原查询的分组方式生成了"长格式"数据,CloudWatch无法识别出两条独立的时间序列。
调整后的查询方案
以下两种写法都能将数据转换为折线图所需的"宽格式":
方法一:条件聚合(推荐)
直接在stats语句中按设备类型分别计算成功率,将不同设备的结果转为独立列:
| fields properties.device as device, properties.success as success | stats avg(if(device='desktop', success, null)) as desktop_success, avg(if(device='mobile', success, null)) as mobile_success by bin(1hour)
方法二:全外连接查询
分别查询两种设备的数据,再通过时间戳做全外连接合并:
| fields properties.device as device, properties.success as success | stats avg(success) as desktop_success by bin(1hour) | filter device = 'desktop' | join kind=full outer ( | fields properties.device as device, properties.success as success | stats avg(success) as mobile_success by bin(1hour) | filter device = 'mobile' ) on bin(1hour)
原理说明
CloudWatch Logs Insights的折线图要求时间字段为唯一分组维度,其他数值字段对应独立的折线系列。调整后的数据会变为每行对应一个时间点,desktop_success和mobile_success作为两个独立列,系统就能自动识别出两条时间序列,生成目标双折线图。
内容的提问来源于stack exchange,提问作者John Skiles Skinner
相关产品推荐
相关产品推荐

