Description
Description of the feature request
Problem statement
Powertools should make it easy to implement logging, metrics and tracing for code bases that are split across multiple files and modules. This is in contrast to simplistic examples that have all the example logic needed in a single handler file.
Currently, logger, tracer, and metrics examples all show instantiation in the handler file. It's not clear, from documentation, how developers should handle logging, metrics and tracing in other files
- Should different instances be instantiated as needed?
- Should the handler-instantiated instances be passed passed around for usage by different pieces of code?
Some example use cases where the current implementation makes them unnecessarily difficult - or not clear - to implement
- All log lines - regardless of the originating file - should log some custom attribute (e.g. some field from a request or a header)
- All metrics - regardless of the originating file - should log come custom attribute as a property
Additional context
- Powertools for Java solve these problems
- For logging, it's built on top of log4j which allows global settings to be applied. It also introduces a
LoggingUtils
class which can be accessed anywhere in code to manipulate some feature of logging e.g. https://awslabs.github.io/aws-lambda-powertools-java/core/logging/#appending-additional-keys - For metrics, they also introduce
MetricsUtils
class which can be accessed anywhere in code.
- For logging, it's built on top of log4j which allows global settings to be applied. It also introduces a
- I found docs(tracer): Allow to reuse Tracer across your code #767 which seems to be trying to solve an issue of the same theme but only for tracing
Solution(s)
I'm not proposing a specific solution, but I'd like for the team to think about it. It seems like Java Powertools and the Tracer PR mentioned above do leverage some variation of singleton so this might be worth exploring more.
Off the top of my head, and based on the logger feature parity intended in #482, I don't feel that a simple singleton is the solution, but maybe a singleton root logger, and then child loggers for other files/modules https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/#using-multiple-logger-instances-across-your-code.
In an example application I've started testing Powertools in, I introduced a powertools.ts
file which instantiates/exports logger
, metrics
, tracer
. My handler file imports those instances and configures the handler with the relevant middy middleware.
Other files import those instances as needed as well (this is essentially me introducing the singletons).
Metadata
Metadata
Assignees
Type
Projects
Status