Skip to main content

Lightweight OpenTelemetry instrumentation for AWS Lambda

Project description

Lambda OTel Lite

PyPI

The lambda-otel-lite library provides a lightweight, efficient OpenTelemetry implementation specifically designed for AWS Lambda environments. It features a custom span processor and internal extension mechanism that optimizes telemetry collection for Lambda's unique execution model.

By leveraging Lambda's execution lifecycle and providing multiple processing modes, this library enables efficient telemetry collection with minimal impact on function latency. By default, it uses the otlp-stdout-span-exporter to export spans to stdout for the serverless-otlp-forwarder project.

[!IMPORTANT] This package is highly experimental and should not be used in production. Contributions are welcome.

Table of Contents

Requirements

  • Python 3.12+
  • OpenTelemetry packages (automatically installed as dependencies)
  • For OTLP HTTP export: Additional dependencies available via pip install "lambda_otel_lite[otlp-http]"

Features

  • Flexible Processing Modes: Support for synchronous, asynchronous, and custom export strategies
  • Automatic Resource Detection: Automatic extraction of Lambda environment attributes
  • Lambda Extension Integration: Built-in extension for efficient telemetry export
  • Efficient Memory Usage: Fixed-size queue to prevent memory growth
  • AWS Event Support: Automatic extraction of attributes from common AWS event types
  • Flexible Context Propagation: Support for W3C Trace Context and custom propagators

Architecture and Modules

  • telemetry: Core initialization and configuration

    • Main entry point via init_telemetry
    • Configures global tracer and span processors
    • Returns a TelemetryCompletionHandler for span lifecycle management
  • processor: Lambda-optimized span processor

    • Fixed-size queue implementation
    • Multiple processing modes
    • Coordinates with extension for async export
  • extension: Lambda Extension implementation

    • Manages extension lifecycle and registration
    • Handles span export coordination
    • Implements graceful shutdown
  • extractors: Event processing

    • Built-in support for API Gateway and ALB events
    • Extensible interface for custom events
    • W3C Trace Context propagation
  • handler: Handler decorator

    • Provides create_traced_handler function to create tracing decorators
    • Automatically tracks cold starts using the faas.cold_start attribute
    • Extracts and propagates context from request headers
    • Manages span lifecycle with automatic status handling for HTTP responses
    • Records exceptions in spans with appropriate status codes
    • Properly completes telemetry processing on handler completion

Installation

# Requires Python 3.12+
pip install lambda_otel_lite

# Optional: For OTLP HTTP export support
pip install "lambda_otel_lite[otlp-http]"

Quick Start

from opentelemetry import trace
from lambda_otel_lite import init_telemetry, create_traced_handler
from lambda_otel_lite.extractors import api_gateway_v2_extractor
import json

# Initialize telemetry once, outside the handler
tracer, completion_handler = init_telemetry()

# Define business logic separately
def process_user(user_id):
    # Your business logic here
    return {"name": "User Name", "id": user_id}

# Create traced handler with specific extractor
traced = create_traced_handler(
    name="my-api-handler",
    completion_handler=completion_handler,
    attributes_extractor=api_gateway_v2_extractor
)

@traced
def handler(event, context):
    try:
        # Get current span to add custom attributes
        current_span = trace.get_current_span()
        current_span.set_attribute("handler.version", "1.0")
        
        # Extract userId from event path parameters
        path_parameters = event.get("pathParameters", {}) or {}
        user_id = path_parameters.get("userId", "unknown")
        current_span.set_attribute("user.id", user_id)
        
        # Process business logic
        user = process_user(user_id)
        
        # Return formatted HTTP response
        return {
            "statusCode": 200,
            "headers": {"Content-Type": "application/json"},
            "body": json.dumps({"success": True, "data": user})
        }
    except Exception as error:
        # Simple error handling (see Error Handling section for more details)
        return {
            "statusCode": 500,
            "headers": {"Content-Type": "application/json"},
            "body": json.dumps({
                "success": False,
                "error": "Internal server error"
            })
        }

Processing Modes

The library supports three processing modes for span export:

  1. Sync Mode (default):

    • Direct, synchronous export in handler thread
    • Recommended for:
      • low-volume telemetry
      • limited resources (memory, cpu)
      • when latency is not critical
    • Set via LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE=sync
  2. Async Mode:

    • Export via Lambda extension using AWS Lambda Extensions API
    • Spans are queued and exported after handler completion
    • Uses event-based communication between handler and extension
    • Registers specifically for Lambda INVOKE events
    • Implements graceful shutdown with SIGTERM handling
    • Error handling for:
      • Event communication failures
      • Export failures
      • Extension registration issues
    • Best for production use with high telemetry volume
    • Set via LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE=async
  3. Finalize Mode:

    • Registers extension with no events
    • Maintains SIGTERM handler for graceful shutdown
    • Ensures all spans are flushed during shutdown
    • Compatible with BatchSpanProcessor for custom export strategies
    • Best for specialized export requirements where you need full control
    • Set via LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE=finalize

Async Processing Mode Architecture

sequenceDiagram
    participant Lambda Runtime
    participant Extension Thread
    participant Handler
    participant LambdaSpanProcessor
    participant OTLPStdoutSpanExporter

    Note over Extension Thread: Initialization
    Extension Thread->>Lambda Runtime: Register extension (POST /register)
    Lambda Runtime-->>Extension Thread: Extension ID
    Extension Thread->>Lambda Runtime: Get next event (GET /next)

    Note over Handler: Function Invocation
    Handler->>LambdaSpanProcessor: Create & queue spans
    Note over LambdaSpanProcessor: Spans stored in fixed-size queue

    Handler->>Extension Thread: Set handler_complete_event
    Note over Handler: Handler returns response

    Extension Thread->>LambdaSpanProcessor: process_spans()
    LambdaSpanProcessor->>OTLPStdoutSpanExporter: export() batched spans
    Extension Thread->>Lambda Runtime: Get next event (GET /next)

    Note over Extension Thread: On SIGTERM
    Lambda Runtime->>Extension Thread: SHUTDOWN event
    Extension Thread->>LambdaSpanProcessor: force_flush()
    LambdaSpanProcessor->>OTLPStdoutSpanExporter: export() remaining spans

The async mode leverages Lambda's extension API to optimize perceived latency by deferring span export until after the response is sent to the user. The diagram above shows the core coordination between components:

  1. Extension thread registers and waits for events from Runtime
  2. Handler queues spans during execution via LambdaSpanProcessor
  3. Handler signals completion via event before returning
  4. Extension processes and exports queued spans after handler completes
  5. Extension returns to waiting for next event
  6. On shutdown, remaining spans are flushed and exported

Telemetry Configuration

The library provides several ways to configure the OpenTelemetry tracing pipeline, which is a required first step to instrument your Lambda function:

Custom configuration with custom resource attributes

from opentelemetry.sdk.resources import Resource
from lambda_otel_lite import init_telemetry

# Create a custom resource with additional attributes
resource = Resource.create({
    "service.version": "1.0.0",
    "deployment.environment": "production",
    "custom.attribute": "value"
})

# Initialize with custom resource
tracer, completion_handler = init_telemetry(resource=resource)

# Use the tracer and completion handler as usual

Custom configuration with custom span processors

from opentelemetry.sdk.trace import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from lambda_otel_lite import init_telemetry

# First install the optional dependency:
# pip install "lambda_otel_lite[otlp-http]"

# Create a custom processor with OTLP HTTP exporter
processor = BatchSpanProcessor(
    OTLPSpanExporter(
        endpoint="https://your-otlp-endpoint/v1/traces"
    )
)

# Initialize with custom processor
tracer, completion_handler = init_telemetry(span_processors=[processor])

You can provide multiple span processors, and they will all be used to process spans. This allows you to send telemetry to multiple destinations or use different processing strategies for different types of spans.

Custom configuration with context propagators

from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3Format
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from lambda_otel_lite import init_telemetry

# Create a custom configuration with specific propagators
tracer, completion_handler = init_telemetry(
    propagators=[
        TraceContextTextMapPropagator(),  # W3C Trace Context
        B3Format(),  # B3 format for Zipkin compatibility
    ]
)

By default, OpenTelemetry Python uses W3C Trace Context and W3C Baggage propagators. The propagators parameter allows you to customize which propagators are used for context extraction and injection. This is useful when you need to integrate with systems that use different context propagation formats.

You can provide multiple propagators, and they will be combined into a composite propagator. The order matters - propagators are applied in the order they are provided.

Note: The OpenTelemetry SDK also supports configuring propagators via the OTEL_PROPAGATORS environment variable. If set, this environment variable takes precedence over programmatic configuration. See the OpenTelemetry Python documentation for more details.

Library specific Resource Attributes

The library adds several resource attributes under the lambda_otel_lite namespace to provide configuration visibility:

  • lambda_otel_lite.extension.span_processor_mode: Current processing mode (sync, async, or finalize)
  • lambda_otel_lite.lambda_span_processor.queue_size: Maximum number of spans that can be queued
  • lambda_otel_lite.lambda_span_processor.batch_size: Maximum batch size for span export
  • lambda_otel_lite.otlp_stdout_span_exporter.compression_level: GZIP compression level used for span export

These attributes are automatically added to the resource and can be used to understand the telemetry configuration in your observability backend.

Event Extractors

Event extractors are responsible for extracting span attributes and context from Lambda event and context objects. The library provides built-in extractors for common Lambda triggers.

Automatic Attributes extraction

The library automatically sets relevant FAAS attributes based on the Lambda context and event. Both event and context parameters must be passed to tracedHandler to enable all automatic attributes:

  • Resource Attributes (set at initialization):

    • cloud.provider: "aws"
    • cloud.region: from AWS_REGION
    • faas.name: from AWS_LAMBDA_FUNCTION_NAME
    • faas.version: from AWS_LAMBDA_FUNCTION_VERSION
    • faas.instance: from AWS_LAMBDA_LOG_STREAM_NAME
    • faas.max_memory: from AWS_LAMBDA_FUNCTION_MEMORY_SIZE
    • service.name: from OTEL_SERVICE_NAME (defaults to function name)
    • Additional attributes from OTEL_RESOURCE_ATTRIBUTES (URL-decoded)
  • Span Attributes (set per invocation when passing context):

    • faas.cold_start: true on first invocation
    • cloud.account.id: extracted from context's invokedFunctionArn
    • faas.invocation_id: from awsRequestId
    • cloud.resource_id: from context's invokedFunctionArn
  • HTTP Attributes (set for API Gateway events):

    • faas.trigger: "http"
    • http.status_code: from handler response
    • http.route: from routeKey (v2) or resource (v1)
    • http.method: from requestContext (v2) or httpMethod (v1)
    • http.target: from path
    • http.scheme: from protocol

The library automatically detects API Gateway v1 and v2 events and sets the appropriate HTTP attributes. For HTTP responses, the status code is automatically extracted from the handler's response and set as http.status_code. For 5xx responses, the span status is set to ERROR.

Built-in Extractors

from lambda_otel_lite.extractors import (
    api_gateway_v1_extractor,  # API Gateway REST API
    api_gateway_v2_extractor,  # API Gateway HTTP API
    alb_extractor,            # Application Load Balancer
    default_extractor,        # Basic Lambda attributes
)

Each extractor is designed to handle a specific event type and extract relevant attributes:

  • api_gateway_v1_extractor: Extracts HTTP attributes from API Gateway REST API events
  • api_gateway_v2_extractor: Extracts HTTP attributes from API Gateway HTTP API events
  • alb_extractor: Extracts HTTP attributes from Application Load Balancer events
  • default_extractor: Extracts basic Lambda attributes from any event type
from lambda_otel_lite.extractors import api_gateway_v1_extractor
import json
# Initialize telemetry with default configuration
tracer, completion_handler = init_telemetry()

traced = create_traced_handler(
    name="api-v1-handler",
    completion_handler=completion_handler,
    attributes_extractor=api_gateway_v1_extractor
)

@traced
def handler(event, context):
    # Your handler code
    return {
        "statusCode": 200,
        "body": json.dumps({"message": "Hello, world!"})
    }

Custom Extractors

You can create custom extractors for event types not directly supported by the library by implementing the extractor interface:

from lambda_otel_lite.extractors import SpanAttributes, TriggerType
from lambda_otel_lite import create_traced_handler, init_telemetry

def custom_extractor(event, context) -> SpanAttributes:
    return SpanAttributes(
        trigger=TriggerType.OTHER,  # Or any custom string
        attributes={
            'custom.attribute': 'value',
            # ... other attributes
        },
        span_name='custom-operation',  # Optional
        carrier=event.get('headers')   # Optional: For context propagation
    )

# Initialize telemetry
tracer, completion_handler = init_telemetry()

# Create traced handler with custom extractor
traced = create_traced_handler(
    name="custom-handler",
    completion_handler=completion_handler,
    attributes_extractor=custom_extractor
)

@traced
def handler(event, context):
    # Your handler code
    return {"statusCode": 200}

The SpanAttributes object returned by the extractor contains:

  • trigger: The type of trigger (HTTP, SQS, etc.) - affects how spans are named
  • attributes: A dictionary of attributes to add to the span
  • span_name: Optional custom name for the span (defaults to handler name)
  • carrier: Optional dictionary containing trace context headers for propagation

Environment Variables

The library can be configured using the following environment variables:

Processing Configuration

  • LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE: Controls span processing strategy
    • sync: Direct export in handler thread (default)
    • async: Deferred export via extension
    • finalize: Custom export strategy
  • LAMBDA_SPAN_PROCESSOR_QUEUE_SIZE: Maximum number of spans to queue (default: 2048)
  • LAMBDA_SPAN_PROCESSOR_BATCH_SIZE: Maximum number of spans to export in each batch (default: 512)

Resource Configuration

  • OTEL_SERVICE_NAME: Override the service name (defaults to function name)
  • OTEL_RESOURCE_ATTRIBUTES: Additional resource attributes in key=value,key2=value2 format

Export Configuration

  • OTLP_STDOUT_SPAN_EXPORTER_COMPRESSION_LEVEL: Gzip compression level for stdout exporter
    • 0: No compression
    • 1: Best speed
    • 6: Good balance between size and speed (default)
    • 9: Best compression

Logging

  • AWS_LAMBDA_LOG_LEVEL or LOG_LEVEL: Configure log level (debug, info, warn, error, none)

AWS Lambda Environment

The following AWS Lambda environment variables are automatically used for resource attributes:

  • AWS_REGION: Region where function runs
  • AWS_LAMBDA_FUNCTION_NAME: Function name
  • AWS_LAMBDA_FUNCTION_VERSION: Function version
  • AWS_LAMBDA_LOG_STREAM_NAME: Log stream name
  • AWS_LAMBDA_FUNCTION_MEMORY_SIZE: Function memory size

License

MIT

See Also

  • GitHub - The main project repository for the Serverless OTLP Forwarder project
  • GitHub | npm - The Node.js version of this library
  • GitHub | crates.io - The Rust version of this library

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

lambda_otel_lite-0.10.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

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

lambda_otel_lite-0.10.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file lambda_otel_lite-0.10.1.tar.gz.

File metadata

  • Download URL: lambda_otel_lite-0.10.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lambda_otel_lite-0.10.1.tar.gz
Algorithm Hash digest
SHA256 5065e0daa678fca09fb8afbc79f60a05e21af1f88879c47453d429d72c26fbcb
MD5 9730b5c13f517882c458e35c86556d65
BLAKE2b-256 7c6f9edb916503d19bccce5bed33dd339b0b58b7b1405565c9dea747d10e42f4

See more details on using hashes here.

File details

Details for the file lambda_otel_lite-0.10.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lambda_otel_lite-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c8ce989ff5928f0bb18e91d99ea08a5a18e29c3ea421129cceb16c9eddab603
MD5 ffb5e5d46cef3fec253dcecb196849b1
BLAKE2b-256 02e3052cf8c2a4a204a8e6e9df889719f2d4b010a720c0c2952a20951bcd7a1b

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