序列化尝试通过 API 网关插入登录 Cloudwatch 时出现异常



我们希望利用现有的 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

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

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 方法部署成功,当使用方法测试和上面的模型在控制台中对其进行测试时,我从测试中获得以下输出

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 请求正文时,仍然会发生这种情况。我找到了[这篇文章]:当有人遇到相同的异常时,尝试使用golang在cloudwatch上放置LogEvents时获取序列化异常,但这是由于他们的GoLang出现问题而不是通过API

原来我在logGroupNamelogStreamName之后缺少 2 个逗号。看得太久而蒙蔽了双眼

最新更新