1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! 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
//!
//! ```no_run
//! 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
pub use check_cold_start;
pub use TraceContextExtractor;
pub use OtelTracingFuture;
pub use ;
pub use OtelTracingService;
// Re-export commonly used extractors at crate root
pub use ;
pub use SqsEventExtractor;
pub use SnsEventExtractor;
pub use LambdaHttpExtractor;