Skip to content

Feature request: support passing durable context in Logging and Metrics decorators #7763

@ConnorKirk

Description

@ConnorKirk

Expected Behaviour

Logging and Metric decorators support Durable Context.

Current Behaviour

  • The logger.inject_lambda_context() and metrics.log_metrics() decorators raise an error in some configurations when used with a Durable Execution.
  • The decorators access the Lambda Context. In durable executions, the Lambda Context is wrapped with the Durable Context. The Durable Context is now passed to the handler and thus the decorators.
  • Affects the @logger.inject_lambda_context(log_event=True) and @metrics.log_metrics(capture_cold_start_metric=True) decorators (when included params are True). These decorators work without these parameters set.

Code snippet

from aws_durable_execution_sdk_python import (
    DurableContext,
    StepContext,
    durable_execution,
    durable_step,
)
from aws_durable_execution_sdk_python.config import Duration
from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig

logger = Logger(
    service="durable",
    level="INFO",
    serialize_stacktrace=False,
    sampling_rate=0.1,
)

@durable_execution
@logger.inject_lambda_context(log_event=True)
def lambda_handler(event: dict, context: DurableContext) -> str:
    """Simple durable function."""


    logger.info("an info log")


    return "Hello world"

Possible Solution

Check if the provided context is a Durable Context. If so, unwrap the Lambda Context from the Durable Context.

Steps to Reproduce

  1. Create lambda function with durable config specified
  2. Invoke handler above
  3. Observe error

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.14

Packaging format used

PyPi

Debugging logs

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Working on it

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions