Skip to content

Repository files navigation

Spreaker Lambda Powertools

Lambda Powertools is a package encapsulating utilities and best practices used to write Python Lambda functions at Spreaker.

Local development

To develop the library locally, build the dev container and run it:

docker compose build && docker compose run dev sh

To run the tests:

pytest

Releasing

To install the package locally, for inter-project usage, run:

pip install . --force-reinstall

Components

PowerHandler

PowerHandler wraps a traditional Lambda handler and makes it smarter.

  • Automatic intercept prometheus metrics generated during the lambda execution and serialize them into the logs so they can be ingested at a later stage. At the beginning of each execution the content of the Prometheus global registry is reset to avoid values from previous executions ending up summing with the newly generated ones.

Example:

fromlambda_powertools.runtimeimportpower_handlerfromprometheus_clientimportCountercounter=Counter(
name="lambda_invocations_total",
documentation="Total number of lambda invocations"
)
deflambda_body(event, context):
counter.inc(1)
deflambda_handler(event, context):
returnpower_handler(lambda_body)(event, context)

Logger

This logger provides an out-of-the-box logging experience compliant to Spreaker best practices:

  • It uses structured logging adhering to the Spreaker logging policies
  • When used in conjunction with the PowerHandler it's automatically initialized to capture useful information from the execution environment, like the function version and memory, and the various ids injected by AWS based on the execution context
  • It automatically enables debug logging for the incoming requests when specified
  • Embedded throttling policy

Throttling (optional)

The logger supports throttling that works like this:

  • If the log level of the logger is DEBUG

    • Logger will log 100% of DEBUG
    • Logger will log 100% of INFO
    • Logger will log 100% of WARN
    • Logger will log 100% of ERROR
  • For all the other log levels:

    • Logger will log 0% of DEBUG
    • Logger will log 10% of INFO
    • Logger will log 20% of WARN
    • Logger will log 100% of ERROR

The throttling, disabled by default, can be enabled by setting environment variable LOG_THROTTLE_ENABLED to true (True, true, 1 are all valid values).

Example

fromlambda_powertools.loggerimportLoggerlogger=Logger()
# Levelslogger.debug("This is debug")
logger.info("This is info")
logger.warn("This is warn")
logger.error("This is error")
# Additional contextlogger.info("This is info", { "foo": "bar" })
# Log errorstry:
raiseException("Error message")
exceptExceptionaserr:
logger.error("This is error", err)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages