使用 Amazon Cloudwatch Logs Insights 计算 JSON 消息对象中的数组长度

Ula*_*ach 12 json amazon-web-services amazon-cloudwatch aws-cloudwatch-log-insights

有什么方法可以获取在云监视日志洞察解析的 JSON 对象中找到的数组的长度?

例如,当发送以下结构的 JSON 对象以记录洞察时:

{
  names: ['john', 'doe', 'joe', 'schmoe']
}
Run Code Online (Sandbox Code Playgroud)

它被解析为以下字段:

  names.0: john
  names.1: doe
  names.2: joe
  names.3: schmoe
Run Code Online (Sandbox Code Playgroud)

并且可以通过

fields @timestamp, names.0, names.1, ...
Run Code Online (Sandbox Code Playgroud)

在这个例子中,有没有办法得到一个field被调用的number_of_names

  • 例如, | parse get_length(names) as number_of_names

小智 2

对于已知最大长度的较小数组,这是一个丑陋的解决方法:

fields @timestamp, ispresent(names.0) + ispresent(names.1) + ispresent(names.2) + ... + ispresent(names.10) as names_length
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,一个有效的答案!(畏缩) (3认同)