API网关日志记录-代理Lambda-请求路径

时间:2019-04-16 11:30:45

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

我正在尝试将我的API网关日志推送到Elasticsearch。除了一个令人烦恼的缺陷之外,我已经进行了这项工作。我似乎无法获得原始资源路径,因为我正在使用代理lambda函数。我将API日志记录格式设置如下;

{
    "requestId": "$context.requestId",
    "ip": "$context.identity.sourceIp",
    "caller": "$context.identity.caller",
    "user": "$context.identity.user",
    "requestTime": "$context.requestTime",
    "httpMethod": "$context.httpMethod",
    "resourcePath": "$context.resourcePath",
    "status": "$context.status",
    "protocol": "$context.protocol",
    "responseLength": "$context.responseLength"
}

请给我以下内容;

{
    "requestId": "xxxxxxxxxxxxxx",
    "ip": "xxx.xxx.xxx.xxx",
    "caller": "-",
    "user": "-",
    "requestTime": "16/Apr/2019:11:03:49 +0000",
    "httpMethod": "GET",
    "resourcePath": "/{proxy+}",
    "status": "304",
    "protocol": "HTTP/1.1",
    "responseLength": "0"
}

如何获取实际的资源路径而不是/{proxy+}?该文档似乎并不清楚;

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference?cmpid=docs_apigateway_console

1 个答案:

答案 0 :(得分:0)

原来我应该使用它作为我的API日志记录配置;

{
    "requestId": "$context.requestId",
    "ip": "$context.identity.sourceIp",
    "caller": "$context.identity.caller",
    "user": "$context.identity.user",
    "requestTime": "$context.requestTime",
    "httpMethod": "$context.httpMethod",
    "resourcePath": "$context.path",
    "status": "$context.status",
    "protocol": "$context.protocol",
    "responseLength": "$context.responseLength"
}