Request scope context with Logger #1772
Replies: 2 comments 4 replies
-
AWS lambda specific https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#runtimes-lifecycle |
Beta Was this translation helpful? Give feedback.
-
Looking at the lifecycle of an execution environment (docs) we can see that there are three distinct phases: 1/ Init, 2/ Invoke, and 3/ Shutdown: Let's take this code: const name = 'Bob';
export const myHandler = () => {
console.log(`Hello ${name}`);
} The first time an execution environment is provisioned, it goes through the Init phase. In this case, the variable named After that, the function handler (in this case, the function with name By storing a reference of the The Logger utility, as well as all others in Powertools, are based on this concept. We create an object (in the case of Powertools a class instance) outside of the function handler, and then later on for each request that goes to the same execution environment, we use that same object. The main limitation, which things like In the context of Lambda, especially on the Node.js runtime, most functions only process one request at the time and multi-threading is not generally used. |
Beta Was this translation helpful? Give feedback.
-
Hi, I was reading powertools logger about persistentLogAttributes that it can be used to persistent the log context throughout the invocation/request, and then coupled with clearState, this will clear the log context preventing it from being reused for the next invocation/request since they are not relevant. This to me, kind of achieved request scope context tracking.
My question is how is this implemented in powertools logger to achieve that? My research tells me that AsyncLocalStorage is only way to achieve request scope context tracking in NodeJS, since nodejs is single threaded there is no thread level storage for request scope data like we have with other traditional servers. But I don't find the use of AsyncLocalStorage in this repo.
Is it there is another way to achieve request scope data in nodejs that I didn't know about OR am I misunderstanding the use of clearState in powertools logger or lambda nodejs env is different from vanilla nodejs?
Beta Was this translation helpful? Give feedback.
All reactions