Skip to content

Documentation: reuse across code files/modules #777

Closed
@humanzz

Description

@humanzz

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

  1. Should different instances be instantiated as needed?
  2. 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

  1. All log lines - regardless of the originating file - should log some custom attribute (e.g. some field from a request or a header)
  2. All metrics - regardless of the originating file - should log come custom attribute as a property

Additional context

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

Labels

confirmedThe scope is clear, ready for implementationdocumentationImprovements or additions to documentation

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions