如何为包含句点字符的 JSON 属性编写带有属性选择器的 CloudWatch 过滤器
"."
?
CloudWatch 文档介绍了使用属性选择器来筛选日志数据 (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html#metric-filters-extract-json):
文档语法:
{ PropertySelector EqualityOperator String }
文档示例:
{ $.eventType = "UpdateTrail" }
如果日志事件包含包含句点字符
"."
的JSON属性,是否可以使用属性选择器来描述它?
我尝试了以下属性选择器:
过滤器遇到错误
{ $.attributes.http\.method = "POST" }
过滤器遇到错误
{ $.attributes["http.method"] = "POST" }
过滤器产生零结果
{ $.attributes.http.method = "POST" }
示例事件
{
"traceId": "75e07edf6f50ddf0d4a8239cbe91d60a",
"parentId": "f8875623e2e77de4",
"name": "request handler - /subscriptions",
"id": "7f8b38dcbbb15983",
"kind": 0,
"timestamp": 1673621283065,
"duration": 3,
"attributes": {
"http.route": "/subscriptions",
"express.name": "/subscriptions",
"express.type": "request_handler",
"http.method": "POST",
"http.url": "/subscriptions",
"http.body": "{\"foo\":\"bar\"}"
},
"status": {
"code": 0
},
"events": [],
"links": []
}
如果属性选择器中有句点,则需要使用方括号表示法来访问它。所以你需要做的是:
{ $.attributes.['http.method'] = "POST" }
您可以在“使用简单表达式匹配 JSON 日志事件中的术语”下的模式过滤器文档中找到此信息,您可以找到类似的示例