Skip to content

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

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
coughlmao:fix/8822-log-without-trace-id
Nov 20, 2026
Merged

leandrodamascena merged 2 commits 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
@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 20, 2026

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @coughlmao! Emitting right away when there is no trace id is the call I'd have made too, and the early return makes the buffered path easier to read than before. The flush-after test is a good one, it pins that nothing gets written twice. Goes out in 3.42.0, no need to rebase, you'll be in the release notes.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.14%. Comparing base (17eca32) to head (dba9589).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
aws_lambda_powertools/logging/logger.py 84.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8865      +/-   ##
===========================================
- Coverage    97.15%   97.14%   -0.01%     
===========================================
  Files          327      327              
  Lines        16781    16785       +4     
  Branches      1531     1532       +1     
===========================================
+ Hits         16303    16306       +3     
- Misses         335      336       +1     
  Partials       143      143              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit a904747 into aws-powertools:develop Nov 20, 2026
13 of 15 checks passed
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

2 participants