Skip to content

powertools

AWS lambda powertools shortcut.

Parameters:

Name Type Description Default
decorated Optional[Callable]

the function being decorated

None
log_event bool

passed to aws_lambda_powertools.Logger

True
capture_response bool

passed to aws_lambda_powertools.Tracer

True
capture_error bool

passed to aws_lambda_powertools.Tracer

True
raise_on_empty_metrics bool

passed to aws_lambda_powertools.Metrics

False
capture_cold_start_metric bool

passed to aws_lambda_powertools.Metrics

True
default_dimensions Optional[dict]

passed to aws_lambda_powertools.Metrics

None
model Optional[pydantic.BaseModel]

passed to aws_lambda_powertools.utilities.parser.event_parser

None
envelope Optional[pydantic.BaseModel]

passed to aws_lambda_powertools.utilities.parser.event_parser

None

For further descriptions, see awslabs.github.io/aws-lambda-powertools-python/latest/

Source code in orkestra/decorators.py
def powertools(
    decorated: Optional[Callable] = None,
    log_event: bool = True,
    capture_response: bool = True,
    capture_error: bool = True,
    raise_on_empty_metrics: bool = False,
    capture_cold_start_metric: bool = True,
    default_dimensions: Optional[dict] = None,
    model: Optional["pydantic.BaseModel"] = None,
    envelope: Optional["pydantic.BaseModel"] = None,
):
    """
    AWS lambda powertools shortcut.

    Args:
        decorated: the function being decorated
        log_event: passed to aws_lambda_powertools.Logger
        capture_response: passed to aws_lambda_powertools.Tracer
        capture_error: passed to aws_lambda_powertools.Tracer
        raise_on_empty_metrics: passed to aws_lambda_powertools.Metrics
        capture_cold_start_metric: passed to aws_lambda_powertools.Metrics
        default_dimensions: passed to aws_lambda_powertools.Metrics
        model: passed to aws_lambda_powertools.utilities.parser.event_parser
        envelope: passed to aws_lambda_powertools.utilities.parser.event_parser

    For further descriptions, see https://awslabs.github.io/aws-lambda-powertools-python/latest/
    """

    from aws_lambda_powertools import Logger, Tracer, Metrics
    from aws_lambda_powertools.utilities.parser import parse

    def decorator(func):

        if isinstance(func, Compose):

            raise TypeError(
                f"@powertools decorator must be used BELOW the @compose decorator."
            )

        logger, tracer, metrics = (
            func.__globals__.get(x)
            for x in (
                "logger",
                "tracer",
                "metrics",
            )
        )

        if isinstance(logger, Logger):

            func = logger.inject_lambda_context(
                lambda_handler=func,
                log_event=log_event,
            )

        if isinstance(tracer, Tracer):

            func = tracer.capture_lambda_handler(
                lambda_handler=func,
                capture_response=capture_response,
                capture_error=capture_error,
            )

        if isinstance(metrics, Metrics):

            func = metrics.log_metrics(
                lambda_handler=func,
                capture_cold_start_metric=capture_cold_start_metric,
                raise_on_empty_metrics=raise_on_empty_metrics,
                default_dimensions=default_dimensions,
            )

        if model is not None:

            @functools.wraps(func)
            def mini_decorator(event, context):
                """
                Exists because event_parser expects the function it wraps to be named "handler".
                """

                logger.debug(f"parsing {event = }")

                parsed_event = parse(
                    event=event,
                    model=model,
                    envelope=envelope,
                )

                return func(parsed_event, context)

            return mini_decorator

        return func

    if decorated is not None:
        return decorator(decorated)
    else:
        return decorator