小编Thi*_*ata的帖子

如何使用 CloudWatch Insights Regex 在第一次匹配后不返回,但返回集合

我的 CloudWatch 日志中有许多行都是 JSON 对象,如下所示:

{
    "friends": [
        { "name": "bob"},
        { "name": "steve"},
        { "name": "joe" }
    ]
}
Run Code Online (Sandbox Code Playgroud)

使用 CloudWatch Regex 表达式,我想提取所有名称。我已经有一个正则表达式,它返回我想要的值:

{
    "friends": [
        { "name": "bob"},
        { "name": "steve"},
        { "name": "joe" }
    ]
}
Run Code Online (Sandbox Code Playgroud)

正如您可以看到在此链接中运行:https://regex101.com/r/Bb28Pg/2

使用 CloudWatch 语法,该正则表达式将变为以下命令:

fields @message
| filter @message like /"friends":/
| parse @message /"name":[ ]*"(?<@name>[^"]*)"/
Run Code Online (Sandbox Code Playgroud)

但此表达式仅返回名字,在示例中为“bob”。我想把它们全部得到。我尝试/g在表达式末尾添加 the ,但这没有帮助。我尝试在官方文档https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html中查找一些信息,但我找不到与此主题相关的任何信息。

在多行日志中的 Cloudwatch Insights 搜索中存在类似的问题,但该问题没有使用 parse 命令,也没有答案。

regex amazon-web-services amazon-cloudwatch amazon-cloudwatchlogs

5
推荐指数
1
解决办法
5456
查看次数

AWS CloudWatch Insights - 简单三元 IF 或一些类似函数

考虑到我的日志中有许多带有时间测量的属性。我想计算它们每个都比我的超时限制大多少次,例如 10 秒。

我目前能够多次运行非常类似的查询,如下所示:

fields context
| filter context.time_to_prepare > 10
| count(*) as count_slow_time_to_prepare by bin(10m)
Run Code Online (Sandbox Code Playgroud)
fields context
| filter context.time_to_shine > 10
| count(*) as count_slow_time_to_shine by bin(10m)
Run Code Online (Sandbox Code Playgroud)

.. 每个属性一次查询

如果我们可以在同一个查询中提取所有这些指标,那么绘制图表会更好、更容易。为此,所缺少的只是一些三元运算符或类似的东西。

我希望这样的事情会起作用:

fields context
| filter context.total_time > 0
| stats sum(if(context.time_to_prepare > 10,1,0)) as slow_time_to_prepare,
|       sum(if(context.time_to_shine   > 10,1,0)) as slow_time_to_shine  ,
# .. one row for each attribute
|       sum(if(context.time_to_move    > 10,1,0)) as slow_time_to_move   by bin(10m)
Run Code Online (Sandbox Code Playgroud)

但这没有用。“if”函数不存在。

那么,有没有办法制作三元if呢?

amazon-cloudwatch aws-cloudwatch-log-insights

5
推荐指数
1
解决办法
5783
查看次数