首页 > 解决方案 > 访问 JSON 数组中的值

问题描述

我正在按照文档中关于如何访问 CloudWatch Insights 中的 JSON 值的说明进行操作,其中推荐如下

JSON arrays are flattened into a list of field names and values. For example, to specify the value of instanceId for the first item in requestParameters.instancesSet, use requestParameters.instancesSet.items.0.instanceId.

参考 https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData-discoverable-fields.html

我正在尝试以下内容,但没有得到任何回报。智能感知自动填充至processList.0但不再

fields processList.0.vss
| sort @timestamp desc
| limit 1

我正在使用的 JSON 是

"processList": [
        {
            "vss": xxxxx,
            "name": "aurora",
            "tgid": xxxx,
            "vmlimit": "unlimited",
            "parentID": 1,
            "memoryUsedPc": 16.01,
            "cpuUsedPc": 0.01,
            "id": xxxxx,
            "rss": xxxxx
        },
        {
            "vss": xxxx,
            "name": "aurora",
            "tgid": xxxxxx,
            "vmlimit": "unlimited",
            "parentID": 1,
            "memoryUsedPc": 16.01,
            "cpuUsedPc": 0.06,
            "id": xxxxx,
            "rss": xxxxx
        }]

标签: amazon-cloudwatchamazon-cloudwatchlogs

解决方案


您发布的参考链接还说明了以下内容。

CloudWatch Logs Insights 最多可以从 JSON 日志中提取 100 个日志事件字段。对于未提取的额外字段,您可以使用 parse 命令从消息字段中的原始未解析日志事件中解析这些字段。

https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData-discoverable-fields.html

对于非常大的 JSON 消息,Insights intellisense 可能不会将所有字段解析为命名字段。因此,解决方案是在您希望数据字段存在的字段中对完整的 JSON 字符串使用 parse。在您的示例和我的示例中,它是processList

通过使用如下查询,我能够提取 processList 下特定 cpuUsedPc 的值。

fields @timestamp, cpuUtilization.total, processList
| parse processList /"name":"RDS processes","tgid":.*?,"parentID":.*?,"memoryUsedPc":.*?,"cpuUsedPc":(?<RDSProcessesCPUUsedPc>.*?),/
| sort @timestamp asc
| display @timestamp, cpuUtilization.total, RDSProcessesCPUUsedPc

推荐阅读