Skip to content

Conversation

@svozza
Copy link
Contributor

@svozza svozza commented Oct 20, 2025

Summary

This PR adds support for using an async context (specifically from the InvokeStore package) in the Logger utility. This allows users to emit log statements that are isolated specifically to the current lambda invocation, isolated from any other executions.

Changes

  • Added a specific storage class for log attributes: LogAttributesStore
  • The Logger class only accesses the data in this store through this interface and never touches the stored objects directly.
  • Updated all methods that stored log attributes and log keys to use the new storage class.
  • The storage class checks whether it is running in the InvokeStore context: if they are then the log keys are stored in the current async context, otherwise we fallback to a plain instance wide object as per the current implementation.
  • Moved the sequence testing function to the common testing package.
  • Added specific concurrency tests to ensure isolation is working as intended for the LogAttributesStore and theLogger class as a whole.

Issue number: closes #4667


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@svozza svozza requested a review from sdangol October 20, 2025 20:25
@svozza svozza self-assigned this Oct 20, 2025
@pull-request-size pull-request-size bot added the size/XXL PRs with 1K+ LOC, largely documentation related label Oct 20, 2025
@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Oct 20, 2025
@svozza svozza force-pushed the async-local-storage-logger branch from d8f7864 to 2073078 Compare October 20, 2025 20:55
@svozza svozza force-pushed the async-local-storage-logger branch from 2073078 to c128331 Compare October 20, 2025 22:51
sdangol
sdangol previously approved these changes Oct 22, 2025
Copy link
Contributor

@sdangol sdangol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small question. Rest looks good from my side.

}

return attributes;
return this.#attributesStore.getAllAttributes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check #checkReservedKeyAndWarn here as it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch!

Copy link
Contributor Author

@svozza svozza Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looking into this a bit more, I was surprised that we didn't have a failing test case in light of this removal.

Upon investigation, I have found that #appendKeys already does this check but the deprecated setPersistentLogAttributes does not. However, we do not have a unit test that uses setPersistentLogAttributes to set a reserved keyword. Upon adding a test case that does this I now had a failing unit test.

Initially I thought that rather than adding reserved key filtering logic to setPersistentLogAttributes that I would instead centralise it in #createAdditionalAttributes as this method is called whenever any log statement is emitted in the createAndPopulateLogItem method. This centralisation would allow us to remove the check from #appendKeys too.

However, when I implemented this, the tests that check for reserved keys started failing with stack overflow errors. This makes sense because the #checkReservedKeyAndWarn function itself has a log statement in it. If you try to use it to do a check in createAndPopulateLogItem you will trigger an infinite loop of log statements.

What I have done instead is add a new function called filterReservedAttributeKeys and used it in both #appendKeys and setPersistentLogAttributes (even though the function is deprecated, it is still part of the public API for now) so that reserved keys will not be added to the store. I have also removed the #createAdditionalAttributes method entirely and we just call this.#attributesStore.getAllAttributes() directly now.

@boring-cyborg boring-cyborg bot added the dependencies Changes that touch dependencies, e.g. Dependabot, etc. label Oct 23, 2025
@sonarqubecloud
Copy link

@svozza svozza added the do-not-merge This item should not be merged label Oct 23, 2025
@svozza svozza marked this pull request as draft October 23, 2025 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Changes that touch dependencies, e.g. Dependabot, etc. do-not-merge This item should not be merged logger This item relates to the Logger Utility size/XXL PRs with 1K+ LOC, largely documentation related tests PRs that add or change tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Allow use of InvokeStore in Logger

2 participants