有效地记录AWS lambda
我正在使用无服务器框架来启用我在 aws lambdas 上托管我的功能。为了开发,我正在使用Kotlin。
由于我想通过特定的lambda重复使用资源(例如DB连接),因此我对具有相同处理程序功能的API进行了分组。像所有/posts
相关的API一样,将由poSthandler
处理。在内部,基于RouteKey
,我将请求分配给相关功能。
这意味着对于所有/posts
endpoints(例如get/ports/{id}
,post/post/posts
等),它们都被登录到相同的CloudWatch日志组。这已经成为一个问题。由于我使用了API网关,因此我还可以在API网关级别启用访问记录。当我直接击中API时,这解决了我的问题。
但是,我还拥有一项服务,该服务想直接调用这些lambdas(这可能是该服务的lambda援引我服务的lambdas或EC2/ECS实例调用我服务的lambdas)。在这种情况下,我们将直接使用lambdas,并且不涉及apigateway。在这种情况下,如何维护不同端点的日志记录?
I'm using serverless framework to enable hosting my functions on AWS Lambdas. For development, I'm using Kotlin.
Since I wanted to re-use resources ( like DB connection ) by a particular lambda, I have grouped apis which have the same handler function. Like all /posts
related apis will be handled by PostHandler
. Internally, based on routeKey
, I'm assigning requests to concerned functions.
This means that for all /posts
endpoints ( like GET /posts/{id}
, POST /posts
etc.) they all get logged to the same CloudWatch log group. This was becoming a problem. Since I was using an API Gateway, I also enable access logging at API Gateway level. This resolved my issue when I'm directly hitting an api.
However, I also have a service which would like to call these lambdas directly ( it could be lambda of that service invoking lambdas of my service or an EC2/ECS instance invoking lambdas of my service ). In this case, we would directly be using lambdas and no ApiGateway is involved. How can I maintain logging for different endpoints in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难说出什么是实际的问题,我想您是说很难区分不同的用例和请求?
我建议使用结构化日志(日志记录为JSON),然后使用CloudWatch Logs Insights查询所需的特定信息。例如,您可以在每条消息中记录一个请求ID,然后稍后在请求ID上查询。您也可以使用其他服务之间的共享ID来做同样的事情,这将为您提供端到端的日志可见性。
It's hard to tell what the actual question is, I think you're saying that it's hard to differentiate between the different use cases and requests?
I would recommend using structured logs (logging as JSON) and then using CloudWatch Logs Insights to query for the specific information you need. For example, you could log a request-id with each message and then query on the request-id later. You could also do the same with a shared id between other services which would give you end-to-end log visibility.