durable-lambda-core 1.2.0

Core replay engine, types, and operation logic for AWS Lambda durable execution in Rust
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Log operation — replay-safe structured logging.
//!
//! Implement FR29-FR31: deduplicated log messages, tracing integration,
//! suppress duplicate output during replay phase.
//!
//! Unlike all other operations in this module, logging is NOT a checkpoint-based
//! durable operation. It does not send checkpoints to AWS, does not generate
//! operation IDs, and does not interact with the replay engine's operations map.
//!
//! Replay suppression is purely client-side: if the context is replaying,
//! log calls are no-ops. This matches the Python SDK's `context.logger` behavior.

use crate::context::DurableContext;

impl DurableContext {
    /// Return the parent operation ID for log enrichment, or empty string for root context.
    fn log_parent_id(&self) -> &str {
        self.parent_op_id().unwrap_or("")
    }

    /// Emit a replay-safe info-level log message.
    ///
    /// During execution mode, emits the message via `tracing::info!` with
    /// execution context enrichment (execution ARN, parent ID for child
    /// contexts). During replay mode, the call is a no-op — no log output
    /// is produced.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log("Order processing started");
    /// // During replay, this produces no output.
    /// # }
    /// ```
    pub fn log(&self, message: &str) {
        if !self.is_replaying() {
            tracing::info!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe info-level log message with structured data.
    ///
    /// During execution mode, emits the message and structured data via
    /// `tracing::info!`. During replay mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    /// * `data` — Structured data to include in the log event
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_with_data("Order processed", &serde_json::json!({"order_id": 42}));
    /// # }
    /// ```
    pub fn log_with_data(&self, message: &str, data: &serde_json::Value) {
        if !self.is_replaying() {
            tracing::info!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                data = %data,
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe debug-level log message.
    ///
    /// During execution mode, emits via `tracing::debug!`. During replay
    /// mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_debug("Validating order fields");
    /// # }
    /// ```
    pub fn log_debug(&self, message: &str) {
        if !self.is_replaying() {
            tracing::debug!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe warn-level log message.
    ///
    /// During execution mode, emits via `tracing::warn!`. During replay
    /// mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_warn("Inventory below threshold");
    /// # }
    /// ```
    pub fn log_warn(&self, message: &str) {
        if !self.is_replaying() {
            tracing::warn!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe error-level log message.
    ///
    /// During execution mode, emits via `tracing::error!`. During replay
    /// mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_error("Payment gateway timeout");
    /// # }
    /// ```
    pub fn log_error(&self, message: &str) {
        if !self.is_replaying() {
            tracing::error!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe debug-level log message with structured data.
    ///
    /// During execution mode, emits via `tracing::debug!` with data field.
    /// During replay mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    /// * `data` — Structured data to include in the log event
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_debug_with_data("Request details", &serde_json::json!({"method": "POST"}));
    /// # }
    /// ```
    pub fn log_debug_with_data(&self, message: &str, data: &serde_json::Value) {
        if !self.is_replaying() {
            tracing::debug!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                data = %data,
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe warn-level log message with structured data.
    ///
    /// During execution mode, emits via `tracing::warn!` with data field.
    /// During replay mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    /// * `data` — Structured data to include in the log event
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_warn_with_data("Retry attempt", &serde_json::json!({"attempt": 3}));
    /// # }
    /// ```
    pub fn log_warn_with_data(&self, message: &str, data: &serde_json::Value) {
        if !self.is_replaying() {
            tracing::warn!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                data = %data,
                message = message,
                "durable_log"
            );
        }
    }

    /// Emit a replay-safe error-level log message with structured data.
    ///
    /// During execution mode, emits via `tracing::error!` with data field.
    /// During replay mode, the call is a no-op.
    ///
    /// # Arguments
    ///
    /// * `message` — The log message to emit
    /// * `data` — Structured data to include in the log event
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(ctx: &durable_lambda_core::context::DurableContext) {
    /// ctx.log_error_with_data("Payment failed", &serde_json::json!({"error": "timeout"}));
    /// # }
    /// ```
    pub fn log_error_with_data(&self, message: &str, data: &serde_json::Value) {
        if !self.is_replaying() {
            tracing::error!(
                execution_arn = %self.arn(),
                parent_id = %self.log_parent_id(),
                data = %data,
                message = message,
                "durable_log"
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use aws_sdk_lambda::operation::checkpoint_durable_execution::CheckpointDurableExecutionOutput;
    use aws_sdk_lambda::operation::get_durable_execution_state::GetDurableExecutionStateOutput;
    use aws_sdk_lambda::types::{Operation, OperationStatus, OperationType, OperationUpdate};
    use tracing_test::traced_test;

    use crate::backend::DurableBackend;
    use crate::context::DurableContext;
    use crate::error::DurableError;

    /// Minimal mock backend for log tests — logging never calls the backend.
    struct LogTestBackend;

    #[async_trait::async_trait]
    impl DurableBackend for LogTestBackend {
        async fn checkpoint(
            &self,
            _arn: &str,
            _checkpoint_token: &str,
            _updates: Vec<OperationUpdate>,
            _client_token: Option<&str>,
        ) -> Result<CheckpointDurableExecutionOutput, DurableError> {
            unimplemented!("logging does not checkpoint")
        }

        async fn get_execution_state(
            &self,
            _arn: &str,
            _checkpoint_token: &str,
            _next_marker: &str,
            _max_items: i32,
        ) -> Result<GetDurableExecutionStateOutput, DurableError> {
            Ok(GetDurableExecutionStateOutput::builder().build().unwrap())
        }
    }

    async fn make_executing_context() -> DurableContext {
        DurableContext::new(
            Arc::new(LogTestBackend),
            "arn:aws:lambda:us-east-1:123456789:durable-execution/test-exec".to_string(),
            "tok".to_string(),
            vec![],
            None,
        )
        .await
        .unwrap()
    }

    async fn make_replaying_context() -> DurableContext {
        let op = Operation::builder()
            .id("op-1")
            .r#type(OperationType::Step)
            .status(OperationStatus::Succeeded)
            .start_timestamp(aws_smithy_types::DateTime::from_secs(0))
            .build()
            .unwrap();

        DurableContext::new(
            Arc::new(LogTestBackend),
            "arn:aws:lambda:us-east-1:123456789:durable-execution/test-exec".to_string(),
            "tok".to_string(),
            vec![op],
            None,
        )
        .await
        .unwrap()
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_emits_during_execution() {
        let ctx = make_executing_context().await;
        assert!(!ctx.is_replaying());

        ctx.log("order processing started");
        assert!(logs_contain("order processing started"));
        assert!(logs_contain("execution_arn"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_suppressed_during_replay() {
        let ctx = make_replaying_context().await;
        assert!(ctx.is_replaying());

        ctx.log("should not appear in logs");
        assert!(!logs_contain("should not appear in logs"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_with_structured_data() {
        let ctx = make_executing_context().await;
        let data = serde_json::json!({"order_id": 42, "amount": 99.99});

        ctx.log_with_data("order processed", &data);
        assert!(logs_contain("order processed"));
        assert!(logs_contain("order_id"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_all_levels() {
        let ctx = make_executing_context().await;

        ctx.log_debug("debug message");
        ctx.log("info message");
        ctx.log_warn("warn message");
        ctx.log_error("error message");

        assert!(logs_contain("debug message"));
        assert!(logs_contain("info message"));
        assert!(logs_contain("warn message"));
        assert!(logs_contain("error message"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_all_levels_suppressed_during_replay() {
        let ctx = make_replaying_context().await;

        ctx.log_debug("replay debug");
        ctx.log("replay info");
        ctx.log_warn("replay warn");
        ctx.log_error("replay error");
        ctx.log_with_data("replay data", &serde_json::json!({"key": "val"}));
        ctx.log_debug_with_data("replay debug data", &serde_json::json!({"k": "v"}));
        ctx.log_warn_with_data("replay warn data", &serde_json::json!({"k": "v"}));
        ctx.log_error_with_data("replay error data", &serde_json::json!({"k": "v"}));

        assert!(!logs_contain("replay debug"));
        assert!(!logs_contain("replay info"));
        assert!(!logs_contain("replay warn"));
        assert!(!logs_contain("replay error"));
        assert!(!logs_contain("replay data"));
        assert!(!logs_contain("replay debug data"));
        assert!(!logs_contain("replay warn data"));
        assert!(!logs_contain("replay error data"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_is_not_durable_operation() {
        let ctx = make_executing_context().await;

        // Logging should NOT generate operation IDs or interact with the replay engine.
        let ops_before = ctx.replay_engine().operations().len();

        ctx.log("test message");
        ctx.log_with_data("test data", &serde_json::json!({"k": "v"}));
        ctx.log_debug("test debug");
        ctx.log_warn("test warn");
        ctx.log_error("test error");

        let ops_after = ctx.replay_engine().operations().len();
        assert_eq!(
            ops_before, ops_after,
            "logging must not add operations to replay engine"
        );
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_with_data_variants() {
        let ctx = make_executing_context().await;

        ctx.log_debug_with_data("debug details", &serde_json::json!({"step": "validate"}));
        ctx.log_warn_with_data("warn details", &serde_json::json!({"retries": 2}));
        ctx.log_error_with_data("error details", &serde_json::json!({"code": 500}));

        assert!(logs_contain("debug details"));
        assert!(logs_contain("warn details"));
        assert!(logs_contain("error details"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_includes_execution_arn() {
        let ctx = make_executing_context().await;

        ctx.log("arn check");
        assert!(logs_contain("durable-execution/test-exec"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_root_context_has_empty_parent_id() {
        let ctx = make_executing_context().await;
        assert!(ctx.parent_op_id().is_none());

        ctx.log("root log");
        assert!(logs_contain("root log"));
        assert!(logs_contain("parent_id"));
    }

    #[traced_test]
    #[tokio::test]
    async fn test_log_child_context_includes_parent_id() {
        let ctx = make_executing_context().await;
        let child = ctx.create_child_context("child-op-123");

        assert_eq!(child.parent_op_id(), Some("child-op-123"));

        child.log("child log message");
        assert!(logs_contain("child log message"));
        assert!(logs_contain("child-op-123"));
    }
}