dvsa / dvsa-lambda-starter

A starting pattern for AWS lambda in Typescript
MIT License
5 stars 4 forks source link

Default Logging #14

Closed Andy-Taylor-92 closed 4 years ago

Andy-Taylor-92 commented 4 years ago

This is a logging approach for #5

It currently uses console.logas you get the timestamp, lambdaRequestId and log level printed with every console.log by default.

By using a utility wrapper surrounding console.log, theawsRequestId and a "correlation ID" can be logged with every debug/info/warn/error message.

For this pattern to work, every service/lambda must forward a correlation ID to subsequent services via a header e.g. X-Correlation-Id.

In practice, the first lambda invoked by an initial request will have received no X-Correlation-Id header, so it will default its correlationId to equal its lambdaRequestId and then traverse that ID to subsequent lambdas via the X-Correlation-Id header. Every lambda called subsequently will then receive that X-Correlation-Id header and inject it into their logs.

This shows an example of what the log looks like from the first invoked lambda:

2020-09-10T17:03:04.891Z    5ff37fce-5ace-114c-9120-a1406cc8d11d    INFO    {"apiRequestId":"c6af9ac6-7b61-11e6-9a41-93e8deadbeef","correlationId":"5ff37fce-5ace-114c-9120-a1406cc8d11d","message":"Here's a gnarly info message from lambda 1 - notice how my correlationId has been set to my lambdaRequestId?"}

This shows an example of what the logs look like from the second invoked lambda (called via the first lambda):

2020-09-10T17:05:31.627Z    32ff455b-057d-1dd7-98b8-7034bf182dc8    INFO    {"apiRequestId":"d9222e0a-6bd9-49e0-84dd-ffe0680bd141","correlationId":"5ff37fce-5ace-114c-9120-a1406cc8d11d","message":"Here's a gnarly info message from lambda 2 - notice how my correlationId is the same as the lambda 1"}
Andy-Taylor-92 commented 4 years ago

Please add a test to check the correlationId is the same correlationId that's passed in through the header (as in your second example) otherwise all looks great!

Done :)