从 API 网关调用时,Cloudwatch 日志 PutLogEvents 操作失败并显示 com.amazon.coral.service#UnknownOperationException [英] Cloudwatch Logs PutLogEvents action fails with com.amazon.coral.service#UnknownOperationException when called from API Gateway

查看:56
本文介绍了从 API 网关调用时,Cloudwatch 日志 PutLogEvents 操作失败并显示 com.amazon.coral.service#UnknownOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 API Gateway 的 AWS 服务集成类型通过 PutLogEvents 操作将日志添加到 Cloudwatch Logs 服务,如下所述:

这是我正在使用的映射模板:

<代码>{"logGroupName": "FromAPI","logStreamName": "$input.path('$.streamName')",日志事件":[{"时间戳": $input.path('$.ts'),"message": "$input.path('$.message')"}]}

我返回的响应有 200 状态代码,但响应正文如下:

<代码>{输出": {"__type": "com.amazon.coral.service#UnknownOperationException",消息":空},版本":1.0"}

这是经过编辑的 (xxx) 执行日志:

请求xxx的执行日志2019 年 4 月 19 日星期五 02:28:58 UTC:开始执行请求:xxx2019 年 4 月 19 日星期五 02:28:58 UTC:HTTP 方法:POST,资源路径:/log2019 年 4 月 19 日星期五 02:28:58 UTC:方法请求路径:{}2019 年 4 月 19 日星期五 02:28:58 UTC:方法请求查询字符串:{}2019 年 4 月 19 日星期五 02:28:58 UTC:方法请求标头:{}2019 年 4 月 19 日星期五 02:28:58 UTC:转换前的方法请求正文:{"streamName": "12345",ts":1555641510000,消息":帮助!"}2019 年 4 月 19 日星期五 02:28:58 UTC:端点请求 URI:https://logs.xxx.amazonaws.com/?Action=PutLogEvents2019 年 4 月 19 日星期五 02:28:58 UTC:端点请求标头:{Authorization=xxx、X-Amz-Date=20190419T022858Z、x-amzn-apigateway-api-id=xxx、Accept=application/json、User-Agent=AmazonAPIGateway_xxx,X-Amz-Security-Token=xxx [截断]2019 年 4 月 19 日星期五 02:28:58 UTC:转换后的端点请求正文:{"logGroupName": "FromAPI","logStreamName": "12345",日志事件":[{时间戳":1555641510000,消息":帮助!"}]}2019 年 4 月 19 日星期五 02:28:58 UTC:向 https://logs.xxx.amazonaws.com/?Action=PutLogEvents 发送请求UTC 2019 年 4 月 19 日星期五 02:28:58:收到回复.状态:200,集成延迟:38 毫秒UTC 2019 年 4 月 19 日星期五 02:28:58:端点响应标头:{x-amzn-RequestId=xxx,Content-Type=application/json,Content-Length=105,Date=Fri,2019 年 4 月 19 日 02:28:58格林威治标准时间}2019 年 4 月 19 日星期五 02:28:58 UTC:转换前的端点响应正文:{"Output":{"__type":"com.amazon.coral.service#UnknownOperationException","message":null},"Version":1.0"}2019 年 4 月 19 日星期五 02:28:58 UTC:转换后的方法响应主体:{"Output":{"__type":"com.amazon.coral.service#UnknownOperationException","message":null},"Version":1.0"}2019 年 4 月 19 日星期五 02:28:58 UTC:方法响应标头:{X-Amzn-Trace-Id=Root=xxx, Content-Type=application/json}2019 年 4 月 19 日星期五 02:28:58 UTC:成功完成执行2019 年 4 月 19 日星期五 02:28:58 UTC:方法已完成,状态:200

没有任何内容记录到我的 Cloudwatch 日志流中 - API Gateway 集成请求响应正文包含 UnknownOperationException 错误.

我最好的猜测是这个请求由于某种原因没有映射到 PutLogEvents 操作.奇怪的是,在这种情况下,状态码是 200.

我猜这只是一些拼写错误 - 还是我需要发送的附加标头?有什么想法吗?

解决方案

如果您在映射模板的顶部添加以下几行,它应该可以工作:

#set($context.requestOverride.header['X-Amz-Target'] = "Logs_20140328.PutLogEvents")#set($context.requestOverride.header['Content-Type'] = "application/x-amz-json-1.1")

这非常棘手,而且没有很好的文档记录.您可以在PutLogEvents 示例请求中找到这些标头.

I'm using API Gateway's AWS Service integration type to add logs to the Cloudwatch Logs service using the PutLogEvents action, as described here: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html

I've successfully setup a similar type of method to add items into a DynamoDB table using the PutItem action and that worked just fine, so I'm not sure what I'm missing with this one.

I've double checked for typos and problems with my template. Have successfully used PutLogEvents using the CLI tools, so the setup seems all OK.

Here's some screenshots of my setup from the AWS dash:

Here's the mapping template I'm using:

{
  "logGroupName": "FromAPI",
  "logStreamName": "$input.path('$.streamName')",
  "logEvents": [
    {
      "timestamp": $input.path('$.ts'), 
      "message": "$input.path('$.message')"
    }
  ]
}

The response I get back has a 200 status code, but the following response body:

{
  "Output": {
    "__type": "com.amazon.coral.service#UnknownOperationException",
    "message": null
  },
  "Version": "1.0"
}

Here's a redacted (xxx) execution log:

Execution log for request xxx
Fri Apr 19 02:28:58 UTC 2019 : Starting execution for request: xxx
Fri Apr 19 02:28:58 UTC 2019 : HTTP Method: POST, Resource Path: /log
Fri Apr 19 02:28:58 UTC 2019 : Method request path: {}
Fri Apr 19 02:28:58 UTC 2019 : Method request query string: {}
Fri Apr 19 02:28:58 UTC 2019 : Method request headers: {}
Fri Apr 19 02:28:58 UTC 2019 : Method request body before transformations: {
    "streamName": "12345",
    "ts": 1555641510000,
    "message": "help!"
}
Fri Apr 19 02:28:58 UTC 2019 : Endpoint request URI: https://logs.xxx.amazonaws.com/?Action=PutLogEvents
Fri Apr 19 02:28:58 UTC 2019 : Endpoint request headers: {Authorization=xxx, X-Amz-Date=20190419T022858Z, x-amzn-apigateway-api-id=xxx, Accept=application/json, User-Agent=AmazonAPIGateway_xxx, X-Amz-Security-Token=xxx [TRUNCATED]
Fri Apr 19 02:28:58 UTC 2019 : Endpoint request body after transformations: {
  "logGroupName": "FromAPI",
  "logStreamName": "12345",
  "logEvents": [
    {
      "timestamp": 1555641510000, 
      "message": "help!"
    }
  ]
}
Fri Apr 19 02:28:58 UTC 2019 : Sending request to https://logs.xxx.amazonaws.com/?Action=PutLogEvents
Fri Apr 19 02:28:58 UTC 2019 : Received response. Status: 200, Integration latency: 38 ms
Fri Apr 19 02:28:58 UTC 2019 : Endpoint response headers: {x-amzn-RequestId=xxx, Content-Type=application/json, Content-Length=105, Date=Fri, 19 Apr 2019 02:28:58 GMT}
Fri Apr 19 02:28:58 UTC 2019 : Endpoint response body before transformations: {"Output":{"__type":"com.amazon.coral.service#UnknownOperationException","message":null},"Version":"1.0"}
Fri Apr 19 02:28:58 UTC 2019 : Method response body after transformations: {"Output":{"__type":"com.amazon.coral.service#UnknownOperationException","message":null},"Version":"1.0"}
Fri Apr 19 02:28:58 UTC 2019 : Method response headers: {X-Amzn-Trace-Id=Root=xxx, Content-Type=application/json}
Fri Apr 19 02:28:58 UTC 2019 : Successfully completed execution
Fri Apr 19 02:28:58 UTC 2019 : Method completed with status: 200

Nothing gets logged into my Cloudwatch Logs stream - the API Gateway integration request response body contains an UnknownOperationException error.

My best guess is that this request is not mapping to the PutLogEvents action for some reason. Strange that the status code is 200 in this situation.

I'm guessing it's just some typo - or an additional header that I need to send through? Any ideas?

解决方案

It should work if you add the following lines at the top of your Mapping Template:

#set($context.requestOverride.header['X-Amz-Target'] = "Logs_20140328.PutLogEvents")
#set($context.requestOverride.header['Content-Type'] = "application/x-amz-json-1.1")

This is very tricky and not well documented. You can find those headers in the sample request for PutLogEvents.

这篇关于从 API 网关调用时,Cloudwatch 日志 PutLogEvents 操作失败并显示 com.amazon.coral.service#UnknownOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆