Skip to content

fix(logger): emit buffered logs when trace ID is unavailable - #8865

Open
coughlmao wants to merge 1 commit into
aws-powertools:developfrom
coughlmao:fix/8822-log-without-trace-id
Open

coughlmao wants to merge 1 commit into
aws-powertools:developfrom
coughlmao:fix/8822-log-without-trace-id

Conversation

@coughlmao

Copy link
Copy Markdown
Contributor

Issue number: closes #8822

Summary

Changes

  • Updated Logger._add_log_record_to_buffer() to emit log records immediately when no X-Ray trace ID is available, rather than silently dropping them.
  • Preserved the existing invocation-scoped buffering behavior when a trace ID is available, including buffer isolation and overflow handling.
  • Updated the logger buffering documentation and example to explain the behavior during initialization and outside Lambda invocations.
  • Added functional regression tests covering both the Powertools Logger API and external loggers configured with buffering.

Design decision: I chose to emit records immediately when no trace ID is available, rather than buffering them under a fixed key. Without a trace ID, records cannot be associated with a specific invocation. Immediate emission prevents log loss during initialization and outside Lambda without introducing additional fallback-buffer lifecycle or cross-invocation coordination. This also follows the maintainer's recommendation in the issue discussion.

User experience

Before: When buffer_config was enabled, log records emitted while _X_AMZN_TRACE_ID was unavailable were silently dropped. This affected initialization-phase logs and logs produced outside Lambda. Calling flush_buffer() later did not recover those records.

After: Records emitted without an available trace ID are written immediately, so they are not lost. Once a trace ID is available, the existing invocation-scoped buffering behavior continues unchanged.

Verification

Ran the project validation successfully using make pr.

  • Full test suite: 4,300 passed, 6 skipped, 10 deselected.
  • Performance tests: 10 passed.
  • Code coverage: 97.10% (minimum required: 90%).
  • Linting, formatting, documentation checks, type checks, security baseline, and complexity checks completed successfully.

Also ran focused functional tests for the buffering behavior and external logger integration.

No live AWS service validation was performed; the change concerns local logger buffering behavior and was verified through the project's functional and unit test suites.

This contribution was developed with AI assistance. I reviewed the changes and personally ran the tests and checks listed above.


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.

- Emit log records immediately when no X-Ray trace ID is available,
  preventing messages produced during initialization or outside a traced
  Lambda invocation from being silently dropped.
- Preserve existing invocation-scoped buffering when a trace ID is
  available, including buffer isolation and overflow handling.
- Update the logger documentation and example to explain the
  immediate-emission behavior.
- Add functional tests covering immediate emission through the Powertools
  Logger and external loggers configured with buffering, including
  verification that emitted records are not duplicated when the buffer is
  subsequently flushed.
@coughlmao
coughlmao requested a review from a team as a code owner November 17, 2026 06:36
@boring-cyborg boring-cyborg Bot added documentation Improvements or additions to documentation logger tests labels Nov 17, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 17, 2026
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation logger size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: log buffering drops records when _X_AMZN_TRACE_ID is not set, including the whole init phase

1 participant