用于检查 JSON 键是否存在的 CloudWatch 指标过滤器

Nik*_*hev 4 amazon-web-services amazon-cloudwatch amazon-cloudwatchlogs

我正在尝试提出一个指标过滤器表达式,当存在特殊的 JSON 键属性时,该表达式可以过滤 CloudWatch Logs。

用例如下:应用程序执行各种日志记录(以 JSON 格式),每当它有特殊的 JSON 密钥(来自第三方服务的嵌套 JSON 响应)时,我想过滤它。

日志示例:

{"severity":"INFO","msg":"EVENT","event":{"key1":"value1"}}
{"severity":"INFO","msg":"FooService responded","response":{"response_code":800}}
Run Code Online (Sandbox Code Playgroud)

我尝试过的过滤模式不起作用:

{ $.response }
{ $.response = *}
{ $.response = "*"}
{ $.response EXISTS }
{ $.response IS TRUE }
{ $.response NOT NULL }
{ $.response != NULL }
Run Code Online (Sandbox Code Playgroud)

预期过滤结果:

{"severity":"INFO","msg":"FooService responded","response":{"response_code":800}}
Run Code Online (Sandbox Code Playgroud)

{ $.response EXISTS }与我的预期相反(返回第一行而不是第二行),但我不知道如何否定它。

参考资料:过滤器和模式语法@CloudWatch 用户指南

小智 6

我还没有找到好的解决办法。

但至少我确实找到了一个。

如果您搜索!=某个特定值的键,它似乎会对其进行空检查。

所以如果你说:

{$.response != "something_no_one_should_have_ever_saved_this_response_as"}
Run Code Online (Sandbox Code Playgroud)

然后你会得到 json 中存在的所有条目response,以及不是你的字符串的所有条目(希望是所有有效条目)

绝对不是一个干净的解决方案,但它似乎非常实用