Skip to main content

Python SDK for structured, correlated logging from AWS Lambda functions

Project description

logsentinel-sdk

Python SDK for structured, correlated logging from AWS Lambda functions.

Generates a sentinel_id correlation ID at the execution entry point and propagates it through every downstream Lambda call, so all events from a single workflow can be retrieved as a unified timeline — without manually correlating CloudWatch log groups.


Installation

pip install logsentinel-sdk

Requires Python 3.13+.


Quick start

from logsentinel_sdk import Logger, generate_sentinel_id

def handler(event, context):
    sentinel_id = event.get("sentinel_id") or generate_sentinel_id()
    with Logger(service="battle-service", sentinel_id=sentinel_id) as logger:
        logger.info("Battle started", pokemon="Pikachu", opponent="Mewtwo")
        # pass sentinel_id to every downstream call:
        return {"sentinel_id": sentinel_id}

The with block guarantees flush() is called even if the handler raises an exception.


Propagation patterns

Different Lambda trigger types expose the incoming payload differently.

Direct invoke / API Gateway (entry point)

Generate a new sentinel_id if none is present in the event.

def handler(event, context):
    sentinel_id = event.get("sentinel_id") or generate_sentinel_id()
    with Logger(service="entry-service", sentinel_id=sentinel_id) as logger:
        ...
        return {"sentinel_id": sentinel_id, ...}

Step Functions

Step Functions passes the full state input as the event dict.

def handler(event, context):
    # sentinel_id was set by the entry Lambda and passed through the state machine input
    sentinel_id = event["sentinel_id"]
    with Logger(service="step-service", sentinel_id=sentinel_id) as logger:
        ...

SQS trigger

Each record's body is a JSON string. Parse it to access the sentinel_id.

import json

def handler(event, context):
    for record in event["Records"]:
        body = json.loads(record["body"])
        sentinel_id = body["sentinel_id"]
        with Logger(service="worker", sentinel_id=sentinel_id) as logger:
            ...

EventBridge trigger

The sentinel_id lives inside event["detail"].

def handler(event, context):
    sentinel_id = event["detail"]["sentinel_id"]
    with Logger(service="notifier", sentinel_id=sentinel_id) as logger:
        ...

Kinesis trigger (fanout)

Each shard record's data is base64-encoded. Decode and parse to extract the sentinel_id.

import base64, json

def handler(event, context):
    for record in event["Records"]:
        payload = json.loads(base64.b64decode(record["kinesis"]["data"]))
        sentinel_id = payload["sentinel_id"]
        with Logger(service="processor", sentinel_id=sentinel_id) as logger:
            ...

Nested service (parent tracking)

When a Lambda is called by another LogSentinel-instrumented service, pass parent_service to preserve the call graph.

def handler(event, context):
    sentinel_id = event["sentinel_id"]
    with Logger(
        service="child-service",
        sentinel_id=sentinel_id,
        parent_service=event.get("source_service"),
    ) as logger:
        ...

Configuration

The SDK reads its configuration from SSM Parameter Store at init:

Parameter Description
/logsentinel/stream-name Kinesis Data Stream name
/logsentinel/dlq-url SQS DLQ URL (fallback on persistent Kinesis failure)

These parameters are provisioned automatically by logsentinel deploy (see logsentinel-cli).


Retry and fallback

On transient Kinesis failures, the SDK retries up to 5 times with exponential backoff (100 ms → 200 ms → 400 ms → 800 ms → 1 600 ms). If all retries are exhausted, failed records are:

  1. Printed as JSON to stdout — captured by CloudWatch Logs automatically
  2. Sent to the SQS Dead Letter Queue for replay

Override defaults via environment variables:

Variable Default Description
LOGSENTINEL_RETRY_BASE_MS 100 Base delay in milliseconds
LOGSENTINEL_MAX_RETRIES 5 Maximum number of retry attempts

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

logsentinel_sdk-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logsentinel_sdk-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file logsentinel_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: logsentinel_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.4 Darwin/25.3.0

File hashes

Hashes for logsentinel_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ff52a869ab5e2aca2f07eafb992a445c4f2ad051b4b26c9b8c268ee5a60d822a
MD5 2205febf874a7404b595f8d2548fa5ee
BLAKE2b-256 54d8604dd86e099c421525e4ef0527e319b7e59ac475ccf43cc1c97b5457875a

See more details on using hashes here.

File details

Details for the file logsentinel_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: logsentinel_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.4 Darwin/25.3.0

File hashes

Hashes for logsentinel_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b56d9f775130a8fa374875dd99daca113cfb2b8973ab8b772959a6a380c7390b
MD5 085ff8e6696e2a61266ce26876517956
BLAKE2b-256 d4a9ce51d28f3ff96ff62a296e92dc00b156903a39e212cb6b6622467652bc9a

See more details on using hashes here.

Supported by

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