Skip to content

Commit 59d95f6

Browse files
committed
Support Durable Function Context in logger and metric decorators
This commit updates the Logger and Metric decorators to handle DurableContexts. If a DurableContext is present, it is unwrapped to access the Lambda Context
1 parent 9491b48 commit 59d95f6

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,18 @@ def handler(event, context):
520520

521521
@functools.wraps(lambda_handler)
522522
def decorate(event, context, *args, **kwargs):
523-
lambda_context = build_lambda_context_model(context)
523+
unwrapped_context = (
524+
build_lambda_context_model(context.lambda_context)
525+
if hasattr(context, "step")
526+
else build_lambda_context_model(context)
527+
)
528+
524529
cold_start = _is_cold_start()
525530

526531
if clear_state:
527-
self.structure_logs(cold_start=cold_start, **lambda_context.__dict__)
532+
self.structure_logs(cold_start=cold_start, **unwrapped_context.__dict__)
528533
else:
529-
self.append_keys(cold_start=cold_start, **lambda_context.__dict__)
534+
self.append_keys(cold_start=cold_start, **unwrapped_context.__dict__)
530535

531536
if correlation_id_path:
532537
self.set_correlation_id(

aws_lambda_powertools/metrics/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,13 @@ def handler(event, context):
430430

431431
@functools.wraps(lambda_handler)
432432
def decorate(event, context, *args, **kwargs):
433+
unwrapped_context = context.lambda_context if hasattr(context, "step") else context
433434
try:
434435
if default_dimensions:
435436
self.set_default_dimensions(**default_dimensions)
436-
response = lambda_handler(event, context, *args, **kwargs)
437+
response = lambda_handler(event, unwrapped_context, *args, **kwargs)
437438
if capture_cold_start_metric:
438-
self._add_cold_start_metric(context=context)
439+
self._add_cold_start_metric(context=unwrapped_context)
439440
finally:
440441
self.flush_metrics(raise_on_empty_metrics=raise_on_empty_metrics)
441442

aws_lambda_powertools/metrics/provider/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def decorate(event, context, *args, **kwargs):
206206
try:
207207
response = lambda_handler(event, context, *args, **kwargs)
208208
if capture_cold_start_metric:
209-
self._add_cold_start_metric(context=context)
209+
unwrapped_context = context.lambda_context if hasattr(context, "step") else context
210+
self._add_cold_start_metric(context=unwrapped_context)
210211
finally:
211212
self.flush_metrics(raise_on_empty_metrics=raise_on_empty_metrics)
212213

0 commit comments

Comments
 (0)