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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! The [`make_lambda_runtime!`] macro and its `default_flush_tracer` helper.
//!
//! [`make_lambda_runtime!`] generates a complete `#[tokio::main] async fn main()`
//! that wires together telemetry initialisation, optional SDK client singletons,
//! and the Lambda runtime with the [`super::layer::DefaultTracingLayer`] applied.
//!
//! ## Macro syntax
//!
//! ```text
//! make_lambda_runtime!(
//! handler_fn
//! [, trigger = OTelFaasTrigger::Http]
//! [, telemetry_init = my_telemetry_init]
//! [, client_fn() -> SdkClientType]*
//! );
//! ```
//!
//! All parameters after `handler_fn` are optional:
//!
//! - `trigger` — sets the `faas.trigger` attribute (default: `Http`)
//! - `telemetry_init` — custom telemetry init function with signature
//! `fn() -> SdkTracerProvider` (default: [`crate::init::default_telemetry_init`])
//! - `client_fn() -> SdkClientType` — zero or more SDK client declarations;
//! each generates a `OnceLock`-backed accessor with
//! [`crate::interceptor::DefaultInterceptor`] pre-attached
//!
//! ## Prerequisites
//!
//! The macro emits `#[tokio::main]` on the generated `main` function. Because
//! the `tokio` proc-macro references itself by absolute path (`tokio`),
//! you must add `tokio` to your own `Cargo.toml`.
//!
//! ## Example
//!
//! ```no_run
//! use awssdk_instrumentation::lambda::{LambdaError, LambdaEvent};
//! use serde_json::Value;
//!
//! async fn handler(event: LambdaEvent<Value>) -> Result<Value, LambdaError> {
//! Ok(event.payload)
//! }
//!
//! // Minimal: just the handler, defaults to Http trigger.
//! awssdk_instrumentation::make_lambda_runtime!(handler);
//! ```
//!
//! [`make_lambda_runtime!`]: crate::make_lambda_runtime
// make_lambda_runtime! macro — generates main(), tracer init, instrumented
// SDK clients, Tower layer setup, and Lambda runtime execution.
use SdkTracerProvider;
/// Flushes the given [`SdkTracerProvider`], logging the outcome.
///
/// Called by the [`make_lambda_runtime!`]-generated flush closure after each invocation.
/// Generates a complete `#[tokio::main] async fn main()` for a Lambda function.
///
/// This macro wires together telemetry initialisation, optional AWS SDK client
/// singletons, and the Lambda runtime with the [`DefaultTracingLayer`] applied.
/// It is the recommended entry point for Lambda functions using this crate.
///
/// # Syntax
///
/// ```text
/// make_lambda_runtime!(
/// handler_fn
/// [, trigger = OTelFaasTrigger::Variant]
/// [, telemetry_init = my_telemetry_init_fn]
/// [, client_fn() -> SdkClientType]*
/// );
/// ```
///
/// All parameters after `handler_fn` are optional and can appear in any order:
///
/// - **`handler_fn`** *(required)* — path to the async handler function.
/// - **`trigger`** — the [`OTelFaasTrigger`] variant for the `faas.trigger`
/// attribute. Defaults to [`OTelFaasTrigger::Http`].
/// - **`telemetry_init`** — a custom telemetry init function with signature
/// `fn() -> SdkTracerProvider`. Defaults to [`default_telemetry_init`].
/// - **`client_fn() -> SdkClientType`** — zero or more SDK client declarations.
/// Each generates a `OnceLock`-backed accessor with [`DefaultInterceptor`]
/// pre-attached.
///
/// # Prerequisites
///
/// `tokio` must be a direct dependency of your crate. Lambda functions in Rust
/// need `tokio` anyway.
///
/// # Examples
///
/// Minimal usage — just the handler:
///
/// ```no_run
/// use awssdk_instrumentation::lambda::{LambdaError, LambdaEvent};
/// use serde_json::Value;
///
/// async fn handler(event: LambdaEvent<Value>) -> Result<Value, LambdaError> {
/// Ok(event.payload)
/// }
///
/// awssdk_instrumentation::make_lambda_runtime!(handler);
/// ```
///
/// With a DynamoDB client and a datasource trigger:
///
/// ```no_run
/// # mod private {
/// use awssdk_instrumentation::lambda::{LambdaError, LambdaEvent, OTelFaasTrigger};
/// use serde_json::Value;
///
/// async fn handler(event: LambdaEvent<Value>) -> Result<Value, LambdaError> {
/// let _client = dynamodb_client();
/// Ok(event.payload)
/// }
///
/// awssdk_instrumentation::make_lambda_runtime!(
/// handler,
/// trigger = OTelFaasTrigger::Datasource,
/// dynamodb_client() -> aws_sdk_dynamodb::Client
/// );
/// # }
/// # fn dynamodb_client() {}
/// # fn aws_sdk_config() {}
/// # fn main() {}
/// ```
///
/// [`DefaultTracingLayer`]: crate::lambda::layer::DefaultTracingLayer
/// [`OTelFaasTrigger`]: crate::lambda::OTelFaasTrigger
/// [`OTelFaasTrigger::Http`]: crate::lambda::OTelFaasTrigger::Http
/// [`default_telemetry_init`]: crate::init::default_telemetry_init
/// [`DefaultInterceptor`]: crate::interceptor::DefaultInterceptor
/// [`aws_sdk_config_provider!`]: crate::aws_sdk_config_provider
;
=> ;
// tracer_provider and trigger, 2 combinations
=> ;
=> ;
// Only one optional parameter, 2 possibilities
=> ;
=> ;
// No optional parameter
=> ;
}