Lambda Powertools is a package encapsulating utilities and best practices used to write Python Lambda functions at Spreaker.
To develop the library locally, build the dev container and run it:
docker compose build && docker compose run dev shTo run the tests:
pytestTo install the package locally, for inter-project usage, run:
pip install . --force-reinstallPowerHandler 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)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
The logger supports throttling that works like this:
If the log level of the logger is
DEBUG- Logger will log
100%ofDEBUG - Logger will log
100%ofINFO - Logger will log
100%ofWARN - Logger will log
100%ofERROR
- Logger will log
For all the other log levels:
- Logger will log
0%ofDEBUG - Logger will log
10%ofINFO - Logger will log
20%ofWARN - Logger will log
100%ofERROR
- Logger will log
The throttling, disabled by default, can be enabled by setting environment variable LOG_THROTTLE_ENABLED to true (True, true, 1 are all valid values).
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)