【问题标题】:SerializationException when attempting to insert Log into Cloudwatch via API Gateway尝试通过 API Gateway 将 Log 插入 Cloudwatch 时出现 SerializationException
【发布时间】:2019-12-03 12:16:00
【问题描述】:

我们希望利用现有的 API 网关,并将未托管在 AWS 中的应用程序中的某些错误的目的地从 RDS 更改为 CloudWatch 日志组和流,但在测试时我每次都会收到 SerializationException

进入网关的数据模型是

{
  "Message":"foo",
  "StackTrace":"bar",
  "Category": "example"
  "CustomData":{"foo":"bar","fee","fum"},
  "Timestamp": 1564043651175
}

添加时间戳是因为需要插入 CloudWatch 日志,如下所示:https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html

以下是定义发出请求的 API 网关的一些 CloudFormation yaml

Uri:
  Fn::Join:
  - ''
  - - 'arn:aws:apigateway:'
    - !Ref AWS::Region
    - ":logs:action/PutLogEvents"
RequestTemplates:
  application/json: !Sub |
    #set($inputRoot = $input.path('$'))
    #set($context.requestOverride.header['X-Amz-Target'] = "Logs_20140328.PutLogEvents")
    #set($context.requestOverride.header['Content-Type'] = "application/x-amz-json-1.1")
    {
      "logGroupName": "${Prefix}-err-group"
      "logStreamName": "${Prefix}-app-errors"
      "logEvents": [
        {
          "message": "message: $inputRoot.Message, stackTrace: $inputRoot.StackTrace, category: $inputRoot.Category, customData: $inputRoot.CustomData",
          "timestamp": "$inputRoot.Timestamp"
        }
      ]
    }

API 方法部署成功,在控制台中使用 Method Test 和上面的模型对其进行测试时,我从测试中得到以下输出

Thu Jul 25 08:14:24 UTC 2019 : Starting execution for request: 364e53c8-aeb4-11e9-91f6-ab8c0812e717
Thu Jul 25 08:14:24 UTC 2019 : HTTP Method: POST, Resource Path: /
Thu Jul 25 08:14:24 UTC 2019 : Method request path: {}
Thu Jul 25 08:14:24 UTC 2019 : Method request query string: {}
Thu Jul 25 08:14:24 UTC 2019 : Method request headers: {}
Thu Jul 25 08:14:24 UTC 2019 : Method request body before transformations: {
    "Message": "Example Message",
    "StackTrace": "Example StackTrace",
    "Category": "Category1",
    "CustomData": {"GUID": "17e332e5-9d03-4e0d-83b1-874f62cb33bb","One": "1","Version": "28.1.1.0","ErrorId": "1212"},
    "Timestamp": 1564040369451 
}
Thu Jul 25 08:14:24 UTC 2019 : Request validation succeeded for content type application/json
Thu Jul 25 08:14:24 UTC 2019 : Request parameter overrides:
Add X-Amz-Target: Logs_20140328.PutLogEvents
Add Content-Type: application/x-amz-json-1.1
Thu Jul 25 08:14:24 UTC 2019 : Endpoint request URI: https://logs.eu-west-2.amazonaws.com/?Action=PutLogEvents
Thu Jul 25 08:14:24 UTC 2019 : Endpoint request headers: {Authorization=<Redacted>, X-Amz-Date=20190725T081424Z, x-amzn-apigateway-api-id=<Redacted>, Accept=application/json, User-Agent=AmazonAPIGateway_<Redacted>, X-Amz-Security-Token=<Redacted>[TRUNCATED]
Thu Jul 25 08:14:24 UTC 2019 : Endpoint request body after transformations: {
  "logGroupName": "test-err-group"
  "logStreamName": "test-app-errors"
  "logEvents": [
    {
      "message": "message: Example Message, stackTrace: Example StackTrace, category, Category1, customData: {GUID=17e332e5-9d03-4e0d-83b1-874f62cb33bb,One=1,Version=28.1.1.0,ErrorId=1212}",
      "timestamp": "1564040369451"
    }
  ]
}

Thu Jul 25 08:14:24 UTC 2019 : Sending request to https://logs.eu-west-2.amazonaws.com/?Action=PutLogEvents
Thu Jul 25 08:14:24 UTC 2019 : Received response. Status: 400, Integration latency: 7 ms
Thu Jul 25 08:14:24 UTC 2019 : Endpoint response headers: {x-amzn-RequestId=3655f4fb-aeb4-11e9-8498-591aad10a10c, Content-Type=application/x-amz-json-1.1, Content-Length=35, Date=Thu, 25 Jul 2019 08:14:23 GMT, Connection=close}
Thu Jul 25 08:14:24 UTC 2019 : Endpoint response body before transformations: {"__type":"SerializationException"}
Thu Jul 25 08:14:24 UTC 2019 : Method response body after transformations: {"__type":"SerializationException"}
Thu Jul 25 08:14:24 UTC 2019 : Method response headers: {X-Amzn-Trace-Id=Root=1-5d3964e0-291c033c3040ef301afc81c5, Access-Control-Allow-Origin=*, Content-Type=application/json}
Thu Jul 25 08:14:24 UTC 2019 : Successfully completed execution
Thu Jul 25 08:14:24 UTC 2019 : Method completed with status: 200

当我用一个简单的字符串替换更复杂的 json 请求正文时,仍然会发生这种情况。我发现 [这篇文章]:Getting SerializationException while trying to PutLogEvents on cloudwatch using golang 当有人遇到同样的异常但这是由于他们的 GoLang 问题而不是通过 API 造成的时

【问题讨论】:

    标签: json amazon-web-services aws-api-gateway amazon-cloudwatch


    【解决方案1】:

    原来我在logGroupNamelogStreamName 之后缺少了两个逗号。看久了就瞎了

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      相关资源
      最近更新 更多