Skip to content

Bug: logger breaks on nodejs Docker lambdas since 2.4.0 #2851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ralbertazzi opened this issue Jul 29, 2024 · 5 comments · Fixed by #2865
Closed

Bug: logger breaks on nodejs Docker lambdas since 2.4.0 #2851

ralbertazzi opened this issue Jul 29, 2024 · 5 comments · Fixed by #2865
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped good-first-issue Something that is suitable for those who want to start contributing logger This item relates to the Logger Utility

Comments

@ralbertazzi
Copy link
Contributor

ralbertazzi commented Jul 29, 2024

The base image used to build Docker images, public.ecr.aws/lambda/nodejs:20, sets the environment variable TZ=:/etc/localtime. This value is not recognised as valid one and throws an error. I believe this was introduced by this PR that was merged in 2.4.0.

Expected Behavior

Building a Docker Lambda using the logger library without further configuration, i.e. I'd expect the library to print times in UTC when TZ is :/etc/localtime

Current Behavior

RangeError: Invalid time zone specified: :/etc/localtime is raised when logging anything under this condition

Code snippet

import { Logger } from "@aws-lambda-powertools/logger";

const log = new Logger();
log.info("Hello");

Steps to Reproduce

  1. Build a Docker Lambda from public.ecr.aws/lambda/nodejs:20
  2. Install the logger library
  3. Log something

Possible Solution

The library should print times in UTC when TZ is :/etc/localtime

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

RangeError: Invalid time zone specified: :/etc/localtime
    at new DateTimeFormat (<anonymous>)
    at #getDateFormatter (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-powertools/logger/lib/cjs/formatter/LogFormatter.js:85:16)
    at #generateISOTimestampWithOffset (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-powertools/logger/lib/cjs/formatter/LogFormatter.js:103:82)
    at PowertoolsLogFormatter.formatTimestamp (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-powertools/logger/lib/cjs/formatter/LogFormatter.js:53:56)
    at PowertoolsLogFormatter.formatAttributes (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-powertools/logger/lib/cjs/formatter/PowertoolsLogFormatter.js:32:29)
    at Logger.createAndPopulateLogItem (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-powertools/logger/lib/cjs/Logger.js:722:39)
    at Logger.processLogItem (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-powertools/logger/lib/cjs/Logger.js:827:46)
    at Logger.info (/var/task/node_modules/.pnpm/@[email protected]/node_modules/@aws-lambda-power
@ralbertazzi ralbertazzi added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Jul 29, 2024
Copy link

boring-cyborg bot commented Jul 29, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@dreamorosi dreamorosi added logger This item relates to the Logger Utility discussing The issue needs to be discussed, elaborated, or refined and removed triage This item has not been triaged by a maintainer, please wait labels Jul 29, 2024
@dreamorosi dreamorosi self-assigned this Jul 29, 2024
@dreamorosi
Copy link
Contributor

Hi @ralbertazzi - thank you for opening this issue.

I confirm that I am able to reproduce the issue:
image

I agree also with the fix proposed, if the value is :/etc/localtime we should default to :UTC. The fix should be applied in this function, which should be modified to do something like this:

public formatTimestamp(now: Date): string {
  const defaultTimezone = 'UTC';

  /**
   * If a specific timezone is configured and it's not the default `UTC`,
   * format the timestamp with the appropriate timezone offset.
   **/
- const configuredTimezone = this.envVarsService?.getTimezone();
+ const envVartimezone = this.envVarsService?.getTimezone();
+ const configuredTimezone =
+   envVartimezone === ':/etc/localtime' ? defaultTimezone : envVartimezone;
  if (configuredTimezone && !configuredTimezone.includes(defaultTimezone))
    return this.#generateISOTimestampWithOffset(now, configuredTimezone);

  return now.toISOString();
}

Additionally, we should also add a test case at the bottom of this block here to test for this case, similar to:

it('uses the default UTC timezone when the TZ env variable is set to `:/etc/localtime`', () => {
  // Prepare
  process.env.TZ = ':/etc/localtime';
  const formatter = new PowertoolsLogFormatter({
    envVarsService: new EnvironmentVariablesService(),
  });

  // Act
  const timestamp = formatter.formatTimestamp(new Date());

  // Assess
  expect(timestamp).toEqual('2016-06-20T12:08:10.000Z');
});

I'll mark the issue as help-wanted and good-first-issue to signify that contributions are very welcome.

If nobody picks it up, we'll work on it and include the fix in the next release (~next Wednesday).

In the meantime, as a stopgap solution you can set the TZ environment variable to :UTC to continue using this version of Logger.

In parallel, I'll also consult the AWS Lambda team to see if the container image should align its behavior with the managed runtime & set the value to :UTC.

@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Jul 29, 2024
@dreamorosi dreamorosi added good-first-issue Something that is suitable for those who want to start contributing help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation and removed discussing The issue needs to be discussed, elaborated, or refined labels Jul 29, 2024
@dreamorosi dreamorosi removed their assignment Jul 29, 2024
@daschaa
Copy link
Contributor

daschaa commented Jul 30, 2024

I would like to take this issue :)

Copy link
Contributor

github-actions bot commented Aug 1, 2024

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation labels Aug 1, 2024
Copy link
Contributor

github-actions bot commented Aug 8, 2024

This is now released under v2.7.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Aug 8, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped good-first-issue Something that is suitable for those who want to start contributing logger This item relates to the Logger Utility
Projects
3 participants