Skip to main content

Collection of helpers for working with AWS

Project description

okdata-aws

Collection of helpers for working with AWS.

Logging for Lambda

Based on Structlog.

Structured and enriched logging for AWS Lambda functions.

TL;DR:

  • Decorate handler with logging_wrapper
  • Encrich logs with key/value pairs using log_add
  • Time functions with log_duration
  • Log exceptions with log_exception

Usage

Wrap your Lambda handler with logging_wrapper. Badabing badabom, you're good to go!

You can set the service name using the logging.init method, or configure it using the SERVICE_NAME environment variable.

from okdata.aws import logging
from okdata.aws.logging import logging_wrapper

logging.init("my_fantastic_lambda")

@logging_wrapper
def handler(event, context):
    if error:
        return {
            "statusCode": 500,
            "body": "Automatically logs bodies from error responses even!",
        }

For asynchronous handlers, set async_wrapper to True:

@logging_wrapper("my-service", async_wrapper=True)
async def handler(event, context):
    return await foo()

Encriching logs

By automagic logs will be enriched with git revisions, cold start y/n call duration and much more, but to add even more magic you can use log_add and log_duration.

from okdata.aws.logging import logging_wrapper, log_add, log_duration

@logging_wrapper
def handler(event, context):
    log_add(dataset=event["dataset"], foo=context["foo"])
    log_duration(
        lambda: slow_thinger(event["dataset"]),
        "my_slow_thinger"
    )
    ... and so on

def slow_thinger():
    sleep(9999999999999999)

Exceptions

Struct log can extract exception info if we log the exception to the special exc_info key.

For convenience we catch and log uncaught exceptions using this already.

If you need to process an exception you can use log_exception to log it to the exc_info key.

from okdata.aws.logging import logging_wrapper, log_exception

@logging_wrapper
def handler(event, context):
    try:
        thing()
    except MyException as e:
        log_exception(e)
        return { ... }

Status wrapper

The status wrapper logs details about a Lambda function execution and sends it to the status API.

The first component that touches the data (typically on upload) sets a "trace ID", which is then inherited by the following processing steps. This allows the status API to track what has happened to the data, from upload through the various processing steps until the data is ready for consumption.

For pipeline components, the status wrapper picks up the trace ID from the Lambda event automatically.

The status wrapper expects the SERVICE_NAME of the Lambda component to be set in an environment variable, along with GIT_REV and GIT_BRANCH.

Usage

Tag the Lambda handler function with @status_wrapper.

The handler function should set the domain and domain_id values using the status_add method:

from okdata.aws.status import status_wrapper, status_add

@status_wrapper
def my_lambda_handler(event, context):
    status_add(domain="dataset", domain_id=f"{dataset_id}/{version}")

    # Regular handler logic here ...

    # The handler can also add a body object containing component-specific information
    status_body = {
        "input": "/tmp/file.txt",
        "output": "/tmp/file.csv",
        "transformation": "text-to-csv",
    }
    status_add(status_body=status_body)

By default, this will send a status event with event status OK and trace status CONTINUE, meaning that the data pipeline is still running. If the handler function fails, e.g. throws an exception, it will send event status FAILED and trace status FINISHED, in addition to the failure details (exception).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

okdata-aws-0.4.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

okdata_aws-0.4.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file okdata-aws-0.4.1.tar.gz.

File metadata

  • Download URL: okdata-aws-0.4.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.26.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.7

File hashes

Hashes for okdata-aws-0.4.1.tar.gz
Algorithm Hash digest
SHA256 5797d297c2af75017d600ac7d08a89b6a4adf2678fc453e532e1fb483d3242d7
MD5 049cf19602af694482a1323abfc61a58
BLAKE2b-256 1764002dc79393740dbfd02f141fd5368ca4b84f621d443b01c0a106af5be9df

See more details on using hashes here.

File details

Details for the file okdata_aws-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: okdata_aws-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.26.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.7

File hashes

Hashes for okdata_aws-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 edb8f6702d4b904dc6a216d4ecdceb992db39b54bbf6da01d1a6333c12214b34
MD5 81ba060b89779dd82af44a14d8cc24b4
BLAKE2b-256 c0a7ce07a477d56563e10d71c41b1fdbde7a741c689502a88ec2e6af1e3d54d5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page