lambda_simulator/
telemetry.rs

1//! Telemetry API types and event schemas.
2//!
3//! Implements the AWS Lambda Telemetry API event schemas as documented at:
4//! <https://docs.aws.amazon.com/lambda/latest/dg/telemetry-api.html>
5//!
6//! Schema version: 2022-12-13
7
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10
11/// Subscription configuration for the Telemetry API.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct TelemetrySubscription {
15    /// Types of events to subscribe to.
16    pub types: Vec<TelemetryEventType>,
17
18    /// Buffering configuration.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub buffering: Option<BufferingConfig>,
21
22    /// HTTP destination for telemetry events.
23    pub destination: Destination,
24}
25
26/// Types of telemetry events that can be subscribed to.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(rename_all = "lowercase")]
29pub enum TelemetryEventType {
30    /// Platform lifecycle events.
31    Platform,
32
33    /// Function logs.
34    Function,
35
36    /// Extension logs.
37    Extension,
38}
39
40/// Buffering configuration for telemetry events.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct BufferingConfig {
44    /// Maximum number of events to buffer before sending.
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub max_items: Option<u32>,
47
48    /// Maximum bytes to buffer before sending.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub max_bytes: Option<u32>,
51
52    /// Maximum time in milliseconds to buffer events.
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub timeout_ms: Option<u32>,
55}
56
57/// HTTP destination for telemetry events.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct Destination {
60    /// Protocol (must be "HTTP").
61    pub protocol: String,
62
63    /// URI to send events to.
64    #[serde(rename = "URI")]
65    pub uri: String,
66}
67
68/// A telemetry event sent to extensions.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct TelemetryEvent {
71    /// Timestamp of the event in RFC3339 format.
72    pub time: DateTime<Utc>,
73
74    /// Event type.
75    #[serde(rename = "type")]
76    pub event_type: String,
77
78    /// Event-specific record data.
79    pub record: serde_json::Value,
80}
81
82/// Platform event: invocation started.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84#[serde(rename_all = "camelCase")]
85pub struct PlatformStart {
86    /// Request ID for this invocation.
87    pub request_id: String,
88
89    /// Version (optional).
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub version: Option<String>,
92
93    /// Tracing context.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub tracing: Option<TraceContext>,
96}
97
98/// Platform event: runtime finished processing invocation.
99#[derive(Debug, Clone, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101pub struct PlatformRuntimeDone {
102    /// Request ID for this invocation.
103    pub request_id: String,
104
105    /// Status of the invocation.
106    pub status: RuntimeStatus,
107
108    /// Metrics for the runtime execution.
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub metrics: Option<RuntimeDoneMetrics>,
111
112    /// Trace spans.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub spans: Option<Vec<Span>>,
115
116    /// Tracing context.
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub tracing: Option<TraceContext>,
119}
120
121/// Platform event: invocation report with metrics.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123#[serde(rename_all = "camelCase")]
124pub struct PlatformReport {
125    /// Request ID for this invocation.
126    pub request_id: String,
127
128    /// Status of the invocation.
129    pub status: RuntimeStatus,
130
131    /// Metrics for this invocation.
132    pub metrics: ReportMetrics,
133
134    /// Trace spans.
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub spans: Option<Vec<Span>>,
137
138    /// Tracing context.
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub tracing: Option<TraceContext>,
141}
142
143/// Platform event: initialization started.
144#[derive(Debug, Clone, Serialize, Deserialize)]
145#[serde(rename_all = "camelCase")]
146pub struct PlatformInitStart {
147    /// Initialization type.
148    pub initialization_type: InitializationType,
149
150    /// Phase (init or invoke).
151    pub phase: Phase,
152
153    /// Function name.
154    #[serde(skip_serializing_if = "Option::is_none")]
155    pub function_name: Option<String>,
156
157    /// Function version.
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub function_version: Option<String>,
160
161    /// Instance ID.
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub instance_id: Option<String>,
164
165    /// Instance maximum memory in MB.
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub instance_max_memory: Option<u32>,
168
169    /// Runtime version.
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub runtime_version: Option<String>,
172
173    /// Runtime version ARN.
174    #[serde(skip_serializing_if = "Option::is_none")]
175    pub runtime_version_arn: Option<String>,
176
177    /// Tracing context.
178    #[serde(skip_serializing_if = "Option::is_none")]
179    pub tracing: Option<TraceContext>,
180}
181
182/// Platform event: runtime finished initialization.
183#[derive(Debug, Clone, Serialize, Deserialize)]
184#[serde(rename_all = "camelCase")]
185pub struct PlatformInitRuntimeDone {
186    /// Initialization type.
187    pub initialization_type: InitializationType,
188
189    /// Phase (init or invoke).
190    pub phase: Phase,
191
192    /// Status of initialization.
193    pub status: RuntimeStatus,
194
195    /// Trace spans.
196    #[serde(skip_serializing_if = "Option::is_none")]
197    pub spans: Option<Vec<Span>>,
198
199    /// Tracing context.
200    #[serde(skip_serializing_if = "Option::is_none")]
201    pub tracing: Option<TraceContext>,
202}
203
204/// Platform event: initialization report with metrics.
205#[derive(Debug, Clone, Serialize, Deserialize)]
206#[serde(rename_all = "camelCase")]
207pub struct PlatformInitReport {
208    /// Initialization type.
209    pub initialization_type: InitializationType,
210
211    /// Phase (init or invoke).
212    pub phase: Phase,
213
214    /// Status of initialization.
215    pub status: RuntimeStatus,
216
217    /// Metrics for initialization.
218    pub metrics: InitReportMetrics,
219
220    /// Trace spans.
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub spans: Option<Vec<Span>>,
223
224    /// Tracing context.
225    #[serde(skip_serializing_if = "Option::is_none")]
226    pub tracing: Option<TraceContext>,
227}
228
229/// Runtime execution status.
230#[derive(Debug, Clone, Serialize, Deserialize)]
231#[serde(rename_all = "lowercase")]
232pub enum RuntimeStatus {
233    /// Successful execution.
234    Success,
235
236    /// Runtime error.
237    Error,
238
239    /// Runtime failure.
240    Failure,
241
242    /// Execution timeout.
243    Timeout,
244}
245
246/// Initialization type.
247#[derive(Debug, Clone, Serialize, Deserialize)]
248#[serde(rename_all = "kebab-case")]
249pub enum InitializationType {
250    /// On-demand initialization.
251    OnDemand,
252
253    /// Provisioned concurrency.
254    ProvisionedConcurrency,
255
256    /// SnapStart initialization.
257    SnapStart,
258}
259
260/// Lambda execution phase.
261#[derive(Debug, Clone, Serialize, Deserialize)]
262#[serde(rename_all = "lowercase")]
263pub enum Phase {
264    /// Initialization phase.
265    Init,
266
267    /// Invocation phase.
268    Invoke,
269}
270
271/// Metrics for runtime execution.
272#[derive(Debug, Clone, Serialize, Deserialize)]
273#[serde(rename_all = "camelCase")]
274pub struct RuntimeDoneMetrics {
275    /// Duration in milliseconds.
276    pub duration_ms: f64,
277
278    /// Number of bytes produced (optional).
279    #[serde(skip_serializing_if = "Option::is_none")]
280    pub produced_bytes: Option<u64>,
281}
282
283/// Metrics for invocation report.
284#[derive(Debug, Clone, Serialize, Deserialize)]
285#[serde(rename_all = "camelCase")]
286pub struct ReportMetrics {
287    /// Duration in milliseconds.
288    pub duration_ms: f64,
289
290    /// Billed duration in milliseconds.
291    pub billed_duration_ms: u64,
292
293    /// Memory size in MB.
294    #[serde(rename = "memorySizeMB")]
295    pub memory_size_mb: u64,
296
297    /// Maximum memory used in MB.
298    #[serde(rename = "maxMemoryUsedMB")]
299    pub max_memory_used_mb: u64,
300
301    /// Init duration in milliseconds (optional).
302    #[serde(skip_serializing_if = "Option::is_none")]
303    pub init_duration_ms: Option<f64>,
304
305    /// Restore duration in milliseconds (optional, SnapStart).
306    #[serde(skip_serializing_if = "Option::is_none")]
307    pub restore_duration_ms: Option<f64>,
308
309    /// Billed restore duration in milliseconds (optional, SnapStart).
310    #[serde(skip_serializing_if = "Option::is_none")]
311    pub billed_restore_duration_ms: Option<u64>,
312}
313
314/// Metrics for initialization report.
315#[derive(Debug, Clone, Serialize, Deserialize)]
316#[serde(rename_all = "camelCase")]
317pub struct InitReportMetrics {
318    /// Duration in milliseconds.
319    pub duration_ms: f64,
320}
321
322/// Trace context for X-Ray.
323#[derive(Debug, Clone, Serialize, Deserialize)]
324#[serde(rename_all = "camelCase")]
325pub struct TraceContext {
326    /// Tracing type (X-Amzn-Trace-Id).
327    #[serde(rename = "type")]
328    pub trace_type: String,
329
330    /// Trace ID value.
331    pub value: String,
332
333    /// Span ID (optional).
334    #[serde(skip_serializing_if = "Option::is_none")]
335    pub span_id: Option<String>,
336}
337
338/// A trace span.
339#[derive(Debug, Clone, Serialize, Deserialize)]
340#[serde(rename_all = "camelCase")]
341pub struct Span {
342    /// Span name.
343    pub name: String,
344
345    /// Start time in RFC3339 format.
346    pub start: DateTime<Utc>,
347
348    /// Duration in milliseconds.
349    pub duration_ms: f64,
350}
351
352/// Platform event: telemetry subscription registered.
353///
354/// Emitted when an extension subscribes to the Telemetry API.
355#[derive(Debug, Clone, Serialize, Deserialize)]
356#[serde(rename_all = "camelCase")]
357pub struct PlatformTelemetrySubscription {
358    /// Name of the extension that subscribed.
359    pub name: String,
360
361    /// State of the subscription (e.g., "Subscribed").
362    pub state: String,
363
364    /// Types of telemetry the extension subscribed to.
365    pub types: Vec<String>,
366}