如何定义 AWS MetricFilter FilterPattern 以匹配 CloudWatch 中的 JSON 格式的日志事件?
How do I define an AWS MetricFilter FilterPattern to match a JSON-formatted log event in CloudWatch?
我正在尝试在 AWS CloudFormation 模板中定义一个指标过滤器,以匹配来自 CloudWatch 的 JSON 格式的日志事件。
这是日志事件的示例:
{
"httpMethod": "GET",
"resourcePath": "/deployment",
"status": "403",
"protocol": "HTTP/1.1",
"responseLength": "42"
}
这是我目前尝试使用此处文档中给出的示例创建 MetricFilter 以匹配状态字段:FilterAndPatternSyntax
"DeploymentApiGatewayMetricFilter": {
"Type": "AWS::Logs::MetricFilter",
"Properties": {
"LogGroupName": "/aws/apigateway/DeploymentApiGatewayLogGroup",
"FilterPattern": "{ $.status = \"403\" }",
"MetricTransformations": [
{
"MetricValue": "1",
"MetricNamespace": "ApiGateway",
"DefaultValue": 0,
"MetricName": "DeploymentApiGatewayUnauthorized"
}
]
}
}
我在 CloudFormation 中收到 "Invalid metric filter pattern" 消息。
我尝试过但无效的其他变体:
"{ $.status = 403 }" <- no escaped characters
{ $.status = 403 } <- using a json object instead of string
我已经能够使用以类似方式定义的括号表示法成功过滤 space 分隔的日志事件,但 json 格式的日志事件不遵循相同的约定.
运行 遇到了同样的问题,并且能够通过使用 aws-cdk 编写几行代码来生成过滤器模式模板以查看它与我所拥有的之间的区别。
似乎它需要将每条条件括在括号中。
- FilterPattern: '{ $.priority = "ERROR" && $.message != "*SomeMessagePattern*" }'
+ FilterPattern: '{ ($.priority = "ERROR") && ($.message != "*SomeMessagePattern*") }'
不幸的是,CloudFormation 中 MetricFilter 的 AWS 文档没有 JSON 模式的示例。
我也一直 运行 陷入这个错误,因为我在外部用双引号格式化了指标过滤器,就像这样。
FilterPattern: "{ ($.errorCode = '*UnauthorizedOperation') || ($.errorCode = 'AccessDenied*') }"
Strings that consist entirely of alphanumeric characters do not need to be quoted. Strings that have unicode and other characters such as ‘@,‘ ‘$,' ‘,' etc. must be enclosed in double quotes to be valid.
它没有明确列出 splat/wildcard * 字符,所以我认为它在单引号内是可以的,但它一直说度量过滤器模式不好,因为单引号中的 *
我本可以在模式外部使用单引号,在内部字符串周围使用双引号,但我选择像这样转义双引号。
FilterPattern: "{ ($.errorCode = \"*UnauthorizedOperation\") || ($.errorCode = \"AccessDenied*\") }"
我正在尝试在 AWS CloudFormation 模板中定义一个指标过滤器,以匹配来自 CloudWatch 的 JSON 格式的日志事件。 这是日志事件的示例:
{
"httpMethod": "GET",
"resourcePath": "/deployment",
"status": "403",
"protocol": "HTTP/1.1",
"responseLength": "42"
}
这是我目前尝试使用此处文档中给出的示例创建 MetricFilter 以匹配状态字段:FilterAndPatternSyntax
"DeploymentApiGatewayMetricFilter": {
"Type": "AWS::Logs::MetricFilter",
"Properties": {
"LogGroupName": "/aws/apigateway/DeploymentApiGatewayLogGroup",
"FilterPattern": "{ $.status = \"403\" }",
"MetricTransformations": [
{
"MetricValue": "1",
"MetricNamespace": "ApiGateway",
"DefaultValue": 0,
"MetricName": "DeploymentApiGatewayUnauthorized"
}
]
}
}
我在 CloudFormation 中收到 "Invalid metric filter pattern" 消息。
我尝试过但无效的其他变体:
"{ $.status = 403 }" <- no escaped characters
{ $.status = 403 } <- using a json object instead of string
我已经能够使用以类似方式定义的括号表示法成功过滤 space 分隔的日志事件,但 json 格式的日志事件不遵循相同的约定.
运行 遇到了同样的问题,并且能够通过使用 aws-cdk 编写几行代码来生成过滤器模式模板以查看它与我所拥有的之间的区别。
似乎它需要将每条条件括在括号中。
- FilterPattern: '{ $.priority = "ERROR" && $.message != "*SomeMessagePattern*" }'
+ FilterPattern: '{ ($.priority = "ERROR") && ($.message != "*SomeMessagePattern*") }'
不幸的是,CloudFormation 中 MetricFilter 的 AWS 文档没有 JSON 模式的示例。
我也一直 运行 陷入这个错误,因为我在外部用双引号格式化了指标过滤器,就像这样。
FilterPattern: "{ ($.errorCode = '*UnauthorizedOperation') || ($.errorCode = 'AccessDenied*') }"
Strings that consist entirely of alphanumeric characters do not need to be quoted. Strings that have unicode and other characters such as ‘@,‘ ‘$,' ‘,' etc. must be enclosed in double quotes to be valid.
它没有明确列出 splat/wildcard * 字符,所以我认为它在单引号内是可以的,但它一直说度量过滤器模式不好,因为单引号中的 *
我本可以在模式外部使用单引号,在内部字符串周围使用双引号,但我选择像这样转义双引号。
FilterPattern: "{ ($.errorCode = \"*UnauthorizedOperation\") || ($.errorCode = \"AccessDenied*\") }"