如何定义AWS MetricFilter FilterPattern以匹配CloudWatch中的JSON格式的日志事件?

问题描述 投票:1回答:1

我正在尝试在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中收到“无效的指标过滤器模式”消息。

我试过的其他变种不起作用:

"{ $.status = 403 }" <- no escaped characters
{ $.status = 403 } <- using a json object instead of string

我已经能够使用以类似方式定义的括号表示法成功过滤空格分隔的日志事件,但json格式的日志事件不遵循相同的约定。

amazon-web-services amazon-cloudformation amazon-cloudwatchlogs amazon-cloudwatch-metrics
1个回答
0
投票

遇到同样的问题并且能够通过用aws-cdk编写几行来解​​决这个问题,以生成过滤器模式模板,以查看它与我所拥有的之间的区别。

好像它需要用括号括起来的每一条标准。

- FilterPattern: '{ $.priority = "ERROR" && $.message != "*SomeMessagePattern*" }'
+ FilterPattern: '{ ($.priority = "ERROR") && ($.message != "*SomeMessagePattern*") }'

遗憾的是,CloudFormation中的MetricFilter的AWS文档没有JSON模式的示例。

© www.soinside.com 2019 - 2024. All rights reserved.