Crate opentelemetry_lambda_tower

Crate opentelemetry_lambda_tower 

Source
Expand description

OpenTelemetry Tower middleware for AWS Lambda.

This crate provides a Tower middleware layer that automatically instruments AWS Lambda handlers with OpenTelemetry tracing. It extracts trace context from various event sources (HTTP, SQS, SNS, etc.), creates properly attributed spans following OpenTelemetry semantic conventions, and handles span lifecycle management including flushing before Lambda freezes.

§Architecture

The middleware uses the tracing crate as its primary API, with tracing-opentelemetry bridging to OpenTelemetry for export. This allows natural use of tracing macros (info!, debug!, etc.) throughout your handler code.

§Usage

use lambda_runtime::{run, service_fn, LambdaEvent, Error};
use opentelemetry_lambda_tower::{OtelTracingLayer, HttpEventExtractor};
use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
use tower::ServiceBuilder;

async fn handler(
    event: LambdaEvent<ApiGatewayV2httpRequest>,
) -> Result<serde_json::Value, Error> {
    // Your handler logic - spans are automatically created
    tracing::info!("Processing request");
    Ok(serde_json::json!({"statusCode": 200}))
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Initialise your tracer provider and set up tracing-opentelemetry subscriber
    // ...

    let tracing_layer = OtelTracingLayer::new(HttpEventExtractor::new());

    let service = ServiceBuilder::new()
        .layer(tracing_layer)
        .service(service_fn(handler));

    run(service).await
}

§Trace Context Extraction

Different event sources carry trace context in different locations:

  • HTTP (API Gateway): traceparent header using W3C Trace Context
  • SQS: AWSTraceHeader in message system attributes (creates span links)
  • SNS: Similar to SQS

The middleware automatically detects and extracts context appropriately.

§Features

  • http - API Gateway v1/v2 extractor (enabled by default)
  • sqs - SQS event extractor (enabled by default)
  • sns - SNS event extractor
  • lambda-http - Integration with the lambda_http crate
  • full - All extractors

Re-exports§

pub use extractors::http::ApiGatewayV1Extractor;
pub use extractors::http::ApiGatewayV2Extractor;
pub use extractors::http::HttpEventExtractor;
pub use extractors::sqs::SqsEventExtractor;
pub use extractors::sns::SnsEventExtractor;
pub use extractors::lambda_http::LambdaHttpExtractor;

Modules§

extractors
Event-specific trace context extractors.

Structs§

OtelTracingFuture
Future that wraps an instrumented handler and manages span lifecycle.
OtelTracingLayer
Tower layer that adds OpenTelemetry tracing to Lambda handlers.
OtelTracingLayerBuilder
Builder for configuring an OtelTracingLayer.
OtelTracingService
Tower service that instruments Lambda handlers with OpenTelemetry tracing.

Traits§

TraceContextExtractor
Extracts trace context from Lambda event payloads.

Functions§

check_cold_start
Checks if this is a cold start and clears the flag.