opentelemetry-lambda-tower
Tower middleware for automatic OpenTelemetry instrumentation of AWS Lambda handlers.
This crate provides a Tower layer that automatically instruments Lambda handler invocations with OpenTelemetry tracing, following semantic conventions for FaaS and messaging systems.
Features
- Automatic span creation - Creates spans for each Lambda invocation with proper naming
- Trace context propagation - Extracts W3C TraceContext from HTTP headers and X-Ray environment variables
- Span links for messaging - Creates span links for SQS/SNS batch messages
- Cold start detection - Automatically detects and records cold starts
- Configurable flushing - Ensures spans are exported before Lambda freezes
Quick Start
use ;
use ;
use ServiceBuilder;
async
async
Event Type Extractors
The crate provides extractors for common Lambda event types:
| Extractor | Event Type | Feature Flag |
|---|---|---|
ApiGatewayV2Extractor |
API Gateway HTTP API (v2) | http (default) |
ApiGatewayProxyExtractor |
API Gateway REST API (v1) | http (default) |
SqsEventExtractor |
SQS Messages | sqs (default) |
SnsEventExtractor |
SNS Notifications | sns |
HTTP Events (API Gateway)
use ;
let layer = new;
Extracts trace context from:
traceparentHTTP header (W3C TraceContext)_X_AMZN_TRACE_IDenvironment variable (X-Ray format, converted to W3C)
SQS Events
use ;
let layer = new;
For SQS batch processing, span links are created for each message's trace context rather than parent-child relationships, following OpenTelemetry messaging semantic conventions.
SNS Events
use ;
let layer = new;
Configuration
Builder Pattern
use ;
use SdkTracerProvider;
use Arc;
use Duration;
let provider = new;
let layer = new
.tracer_provider
.flush_on_end
.flush_timeout
.build;
Options
| Method | Default | Description |
|---|---|---|
tracer_provider() |
None | Set the tracer provider for flushing |
flush_on_end() |
true |
Flush spans after each invocation |
flush_timeout() |
5s | Timeout for flush operations |
Custom Extractors
Implement the TraceContextExtractor trait for custom event types:
use TraceContextExtractor;
use Context;
use Link;
use Context as LambdaContext;
use Span;
;
Semantic Conventions
The middleware records attributes following OpenTelemetry semantic conventions:
FaaS Attributes
faas.trigger- Trigger type (http, pubsub, other)faas.invocation_id- Lambda request IDfaas.coldstart- Whether this is a cold startfaas.name- Function namefaas.version- Function version
HTTP Attributes (API Gateway)
http.request.method- HTTP methodurl.path- Request pathhttp.route- Route patternclient.address- Client IP
Messaging Attributes (SQS/SNS)
messaging.system-aws_sqsoraws_snsmessaging.operation.type-processmessaging.destination.name- Queue/topic namemessaging.batch.message_count- Batch size
Feature Flags
| Feature | Default | Description |
|---|---|---|
http |
Yes | API Gateway event extractors |
sqs |
Yes | SQS event extractor |
sns |
No | SNS event extractor |
full |
No | All extractors |
Licence
MIT