Skip to content

Commit 4334e15

Browse files
Merge branch 'develop' into fix/sar-pipeline
2 parents c8a8e34 + 6424f69 commit 4334e15

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

aws_lambda_powertools/logging/logger.py

+6
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,12 @@ def _add_log_record_to_buffer(
11271127
tracer_id = get_tracer_id()
11281128

11291129
if tracer_id and self._buffer_config:
1130+
if not self._buffer_cache.get(tracer_id):
1131+
# Detect new Lambda invocation context and reset buffer to maintain log isolation
1132+
# Ensures logs from previous invocations do not leak into current execution
1133+
# Prevent memory excessive usage
1134+
self._buffer_cache.clear()
1135+
11301136
log_record: dict[str, Any] = _create_buffer_record(
11311137
level=level,
11321138
msg=msg,

docs/core/logger.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,16 @@ sequenceDiagram
681681

682682
4. **What happens if the log buffer reaches its maximum size?** Older logs are removed from the buffer to make room for new logs. This means that if the buffer is full, you may lose some logs if they are not flushed before the buffer reaches its maximum size. When this happens, we emit a warning when flushing the buffer to indicate that some logs have been dropped.
683683

684-
5. **What timestamp is used when I flush the logs?** The timestamp preserves the original time when the log record was created. If you create a log record at 11:00:10 and flush it at 11:00:25, the log line will retain its original timestamp of 11:00:10.
684+
5. **How is the log size of a log line calculated?**
685+
The log size is calculated based on the size of the log line in bytes. This includes the size of the log message, any exception (if present), the log line location, additional keys, and the timestamp.
685686

686-
6. **What happens if I try to add a log line that is bigger than max buffer size?** The log will be emitted directly to standard output and not buffered. When this happens, we emit a warning to indicate that the log line was too big to be buffered.
687+
6. **What timestamp is used when I flush the logs?** The timestamp preserves the original time when the log record was created. If you create a log record at 11:00:10 and flush it at 11:00:25, the log line will retain its original timestamp of 11:00:10.
687688

688-
7. **What happens if Lambda times out without flushing the buffer?** Logs that are still in the buffer will be lost.
689+
7. **What happens if I try to add a log line that is bigger than max buffer size?** The log will be emitted directly to standard output and not buffered. When this happens, we emit a warning to indicate that the log line was too big to be buffered.
689690

690-
8. **Do child loggers inherit the buffer?** No, child loggers do not inherit the buffer from their parent logger but only the buffer configuration. This means that if you create a child logger, it will have its own buffer and will not share the buffer with the parent logger.
691+
8. **What happens if Lambda times out without flushing the buffer?** Logs that are still in the buffer will be lost.
692+
693+
9. **Do child loggers inherit the buffer?** No, child loggers do not inherit the buffer from their parent logger but only the buffer configuration. This means that if you create a child logger, it will have its own buffer and will not share the buffer with the parent logger.
691694

692695
### Built-in Correlation ID expressions
693696

tests/functional/logger/required_dependencies/test_powertools_logger_buffer.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,12 @@ def test_buffer_configuration_propagation_across_child_logger_instances(stdout,
470470
assert primary_logger._buffer_cache != secondary_logger._buffer_cache
471471

472472

473-
def test_logger_buffer_is_cleared_between_lambda_invocations(stdout, service_name, monkeypatch, lambda_context):
473+
def test_logger_buffer_is_cleared_between_lambda_invocations_with_decorator(
474+
stdout,
475+
service_name,
476+
monkeypatch,
477+
lambda_context,
478+
):
474479
# Set initial trace ID for first Lambda invocation
475480
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")
476481

@@ -491,3 +496,30 @@ def handler(event, context):
491496

492497
# THEN Verify buffer for the original trace ID is cleared
493498
assert not logger._buffer_cache.get("1-67c39786-5908a82a246fb67f3089263f")
499+
500+
501+
def test_logger_buffer_is_cleared_between_lambda_invocations_without_decoration(
502+
stdout,
503+
service_name,
504+
monkeypatch,
505+
lambda_context,
506+
):
507+
# Set initial trace ID for first Lambda invocation
508+
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")
509+
510+
# GIVEN A logger configured with specific buffer parameters
511+
logger_buffer_config = LoggerBufferConfig(max_bytes=10240)
512+
logger = Logger(level="DEBUG", service=service_name, stream=stdout, buffer_config=logger_buffer_config)
513+
514+
def handler(event, context):
515+
logger.debug("debug line")
516+
517+
# WHEN First Lambda invocation with initial trace ID
518+
handler({}, lambda_context)
519+
520+
# WHEN New Lambda invocation arrives with different trace ID
521+
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "2-ABC39786-5908a82a246fb67f3089263f")
522+
handler({}, lambda_context)
523+
524+
# THEN Verify buffer for the original trace ID is cleared
525+
assert not logger._buffer_cache.get("1-67c39786-5908a82a246fb67f3089263f")

0 commit comments

Comments
 (0)