slog-cloud 0.1.1

This is an unofficial crate for cloud-based drains for the slog logging infrastructure 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
    time::Duration,
};

use futures::FutureExt;
use slog::{Drain, OwnedKVList, Record};

use crate::{
    batch::BatchBuilder,
    client::Client,
    queue::{BatchMessageQueue, MessageQueue, MessageQueueSender},
    sender::SenderTask,
    serializer::LogMessageSerializer,
};

/// Cloud drain builder.
pub struct CloudDrainBuilder {
    queue_capacity: Option<usize>,
    max_concurrency: usize,
    initial_retry_delay: Duration,
    max_retry_delay: Duration,
    max_retries: Option<u32>,
    debug: bool,
}

impl CloudDrainBuilder {
    /// Create a new cloud drain builder.
    #[inline]
    pub const fn new() -> Self {
        Self {
            queue_capacity: None,
            max_concurrency: 16,
            initial_retry_delay: Duration::from_secs(1),
            max_retry_delay: Duration::from_secs(60),
            max_retries: None,
            debug: false,
        }
    }

    /// Set the log message queue capacity or `None` for unlimited (the default
    /// is `None`).
    #[inline]
    pub fn queue_capacity(mut self, queue_capacity: Option<usize>) -> Self {
        self.queue_capacity = queue_capacity;
        self
    }

    /// Set the maximum number of concurrent message sends (the default is 16).
    #[inline]
    pub fn max_concurrency(mut self, max_concurrency: usize) -> Self {
        assert!(max_concurrency > 0);

        self.max_concurrency = max_concurrency;
        self
    }

    /// Set the initial retry delay (the default is 1 second).
    #[inline]
    pub fn initial_retry_delay(mut self, initial_retry_delay: Duration) -> Self {
        self.initial_retry_delay = initial_retry_delay;
        self
    }

    /// Set the maximum retry delay (the default is 60 seconds).
    #[inline]
    pub fn max_retry_delay(mut self, max_retry_delay: Duration) -> Self {
        self.max_retry_delay = max_retry_delay;
        self
    }

    /// Set the maximum number of retries or `None` for unlimited (the default
    /// is unlimited).
    #[inline]
    pub fn max_retries(mut self, max_retries: Option<u32>) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Enable or disable debug mode (the default is disabled).
    ///
    /// In the debug mode you'll be able to see some runtime info on stderr
    /// that will help you with setting up the drain (e.g. failed requests).
    /// With debug mode disabled, all errors will be silently ignored.
    #[inline]
    pub fn debug(mut self, debug: bool) -> Self {
        self.debug = debug;
        self
    }

    /// Build the cloud drain.
    ///
    /// # Arguments
    /// * `client` - a cloud service client
    /// * `serializer` - log message serializer
    /// * `batch_builder` - log message batch builder
    pub fn build<C, S, M, B, BB>(
        self,
        client: C,
        serializer: S,
        batch_builder: BB,
    ) -> (CloudDrain<S, M>, CloudDrainTask, CloudDrainHandle<M>)
    where
        C: Client<Message = B> + Send + Sync + 'static,
        B: Clone + Send + Sync + 'static,
        BB: BatchBuilder<Item = M, Batch = B> + Send + 'static,
        M: Send + 'static,
    {
        let (messages, message_sender) = MessageQueue::new(self.queue_capacity);

        let messages = messages.batch(batch_builder);

        let sender_task = SenderTask::builder()
            .max_concurrency(self.max_concurrency)
            .initial_retry_delay(self.initial_retry_delay)
            .max_retry_delay(self.max_retry_delay)
            .max_retries(self.max_retries)
            .build(client, messages);

        let task = CloudDrainTask { inner: sender_task };

        let handle = CloudDrainHandle {
            sender: message_sender.clone(),
        };

        let drain = CloudDrain {
            serializer,
            sender: message_sender,
            debug: self.debug,
        };

        (drain, task, handle)
    }
}

impl Default for CloudDrainBuilder {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Generic cloud log drain.
pub struct CloudDrain<S, M> {
    serializer: S,
    sender: MessageQueueSender<M>,
    debug: bool,
}

impl<S, M> Drain for CloudDrain<S, M>
where
    S: LogMessageSerializer<Serialized = M>,
{
    type Ok = ();
    type Err = ();

    fn log(&self, record: &Record, logger_values: &OwnedKVList) -> Result<(), ()> {
        match self.serializer.serialize(record, logger_values) {
            Ok(msg) => {
                let res = self.sender.send(msg);

                if self.debug && res.is_err() {
                    eprintln!("unable to send a log message: the background sender is not running");
                }
            }
            Err(err) if self.debug => {
                eprintln!("unable to serialize a log message: {err}");
            }
            Err(_) => (),
        }

        Ok(())
    }
}

/// Task for a cloud log drain.
///
/// The task needs to be polled to drive the drain. It can be spawned as a
/// background task using [`tokio::spawn`].
pub struct CloudDrainTask {
    inner: SenderTask,
}

impl Future for CloudDrainTask {
    type Output = ();

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.inner.poll_unpin(cx)
    }
}

/// Handle for a cloud log drain.
pub struct CloudDrainHandle<M> {
    sender: MessageQueueSender<M>,
}

impl<M> CloudDrainHandle<M> {
    /// Flush any pending log messages.
    pub async fn flush(&self) {
        self.sender.flush().await
    }

    /// Flush any pending log messages.
    ///
    /// # Panics
    /// The method panics if called from an asynchronous execution context.
    pub fn blocking_flush(&self) {
        self.sender.blocking_flush();
    }
}

#[cfg(test)]
mod tests {
    use std::{future::Future, time::Duration};

    use bytes::Bytes;
    use futures::poll;
    use slog::{Drain, Logger, OwnedKVList, Record, info, o};
    use tokio::sync::{mpsc, oneshot};

    use crate::{
        batch::BatchBuilder,
        client::{Client, HttpClientError, SendError, StatusCode},
        serializer::LogMessageSerializer,
    };

    use super::{CloudDrainBuilder, CloudDrainHandle, CloudDrainTask};

    /// Serializer producing just the log message text.
    struct MessageSerializer;

    impl LogMessageSerializer for MessageSerializer {
        type Serialized = String;

        fn serialize(&self, record: &Record, _: &OwnedKVList) -> slog::Result<String> {
            Ok(record.msg().to_string())
        }
    }

    /// Batch builder grouping up to a given number of messages.
    struct VecBatchBuilder {
        batch: Vec<String>,
        max_len: usize,
    }

    impl BatchBuilder for VecBatchBuilder {
        type Item = String;
        type Batch = Vec<String>;

        fn push(&mut self, item: String) -> Option<Vec<String>> {
            let res = if self.batch.len() < self.max_len {
                None
            } else {
                Some(std::mem::take(&mut self.batch))
            };

            self.batch.push(item);

            res
        }

        fn flush(&mut self) -> Option<Vec<String>> {
            if self.batch.is_empty() {
                None
            } else {
                Some(std::mem::take(&mut self.batch))
            }
        }
    }

    /// Send request intercepted by the mock client.
    struct Request {
        batch: Vec<String>,
        response: oneshot::Sender<Result<(), SendError>>,
    }

    impl Request {
        /// Respond with `Ok(())`.
        fn respond_ok(self) {
            let _ = self.response.send(Ok(()));
        }

        /// Response with unexpected status code.
        fn respond_status(self, status: StatusCode) {
            let err = HttpClientError::UnexpectedStatusCode(status, Bytes::new());

            let _ = self.response.send(Err(err.into()));
        }
    }

    /// Client passing all send requests to the test.
    struct MockClient {
        requests: mpsc::UnboundedSender<Request>,
    }

    impl Client for MockClient {
        type Message = Vec<String>;

        async fn send(&self, batch: Vec<String>) -> Result<(), SendError> {
            let (tx, rx) = oneshot::channel();

            let _ = self.requests.send(Request {
                batch,
                response: tx,
            });

            // treat requests left behind by a finished test as delivered
            rx.await.unwrap_or(Ok(()))
        }
    }

    /// Helper struct.
    struct TestDrain {
        logger: Logger,
        task: CloudDrainTask,
        handle: CloudDrainHandle<String>,
        requests: mpsc::UnboundedReceiver<Request>,
    }

    /// Get a drain builder with negligible retry delays.
    fn builder() -> CloudDrainBuilder {
        CloudDrainBuilder::new()
            .initial_retry_delay(Duration::from_millis(1))
            .max_retry_delay(Duration::from_millis(1))
    }

    /// Build a test drain with a given maximum batch length.
    fn build(builder: CloudDrainBuilder, max_batch_len: usize) -> TestDrain {
        let (tx, rx) = mpsc::unbounded_channel();

        let client = MockClient { requests: tx };

        let batch_builder = VecBatchBuilder {
            batch: Vec::new(),
            max_len: max_batch_len,
        };

        let (drain, task, handle) = builder.build(client, MessageSerializer, batch_builder);

        TestDrain {
            logger: Logger::root(drain.fuse(), o!()),
            task,
            handle,
            requests: rx,
        }
    }

    /// Panic if the future takes more than 5 seconds to complete.
    async fn within<F>(fut: F) -> F::Output
    where
        F: Future,
    {
        tokio::time::timeout(Duration::from_secs(5), fut)
            .await
            .expect("timed out")
    }

    /// Get the next request or panic.
    async fn next_request(requests: &mut mpsc::UnboundedReceiver<Request>) -> Request {
        within(requests.recv())
            .await
            .expect("request channel closed")
    }

    /// Wait 50 milliseconds for a request and panic if there is one.
    async fn assert_no_request(requests: &mut mpsc::UnboundedReceiver<Request>) {
        tokio::time::sleep(Duration::from_millis(50)).await;

        assert!(requests.try_recv().is_err(), "unexpected request");
    }

    #[tokio::test]
    async fn batches_queued_messages() {
        let TestDrain {
            logger,
            task,
            handle: _handle,
            mut requests,
        } = build(builder(), 2);

        for msg in ["a", "b", "c", "d", "e"] {
            info!(logger, "{}", msg);
        }

        tokio::spawn(task);

        let mut batches = Vec::new();

        for _ in 0..3 {
            let req = next_request(&mut requests).await;

            batches.push(req.batch.clone());

            req.respond_ok();
        }

        // the batches are sent concurrently
        batches.sort();

        assert_eq!(batches, [vec!["a", "b"], vec!["c", "d"], vec!["e"]]);
    }

    #[tokio::test]
    async fn sends_partial_batch_without_waiting() {
        let TestDrain {
            logger,
            task,
            handle: _handle,
            mut requests,
        } = build(builder(), 10);

        tokio::spawn(task);

        info!(logger, "a");

        assert_eq!(next_request(&mut requests).await.batch, ["a"]);
    }

    #[tokio::test]
    async fn flush_waits_for_message_delivery() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder(), 10);

        tokio::spawn(task);

        info!(logger, "a");

        let mut flush = std::pin::pin!(handle.flush());

        assert!(poll!(flush.as_mut()).is_pending());

        let req = next_request(&mut requests).await;

        tokio::time::sleep(Duration::from_millis(50)).await;

        assert!(poll!(flush.as_mut()).is_pending());

        req.respond_ok();

        within(flush).await;
    }

    #[tokio::test]
    async fn flush_does_not_wait_for_messages_logged_after_it() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder(), 10);

        tokio::spawn(task);

        info!(logger, "a");

        let mut flush = std::pin::pin!(handle.flush());

        assert!(poll!(flush.as_mut()).is_pending());

        info!(logger, "b");

        let req = next_request(&mut requests).await;

        assert_eq!(req.batch, ["a"]);

        req.respond_ok();

        let req = next_request(&mut requests).await;

        assert_eq!(req.batch, ["b"]);

        // the flush completes while "b" is still in flight
        within(flush).await;
    }

    #[tokio::test]
    async fn retries_failed_sends() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder(), 10);

        tokio::spawn(task);

        info!(logger, "a");

        next_request(&mut requests)
            .await
            .respond_status(StatusCode::SERVICE_UNAVAILABLE);

        next_request(&mut requests)
            .await
            .respond_status(StatusCode::TOO_MANY_REQUESTS);

        let req = next_request(&mut requests).await;

        assert_eq!(req.batch, ["a"]);

        req.respond_ok();

        within(handle.flush()).await;

        assert_no_request(&mut requests).await;
    }

    #[tokio::test]
    async fn gives_up_after_max_retries() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder().max_retries(Some(2)), 10);

        tokio::spawn(task);

        info!(logger, "a");

        for _ in 0..3 {
            next_request(&mut requests)
                .await
                .respond_status(StatusCode::SERVICE_UNAVAILABLE);
        }

        within(handle.flush()).await;

        assert_no_request(&mut requests).await;
    }

    #[tokio::test]
    async fn does_not_retry_non_retryable_errors() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder(), 10);

        tokio::spawn(task);

        info!(logger, "a");

        next_request(&mut requests)
            .await
            .respond_status(StatusCode::BAD_REQUEST);

        within(handle.flush()).await;

        assert_no_request(&mut requests).await;
    }

    #[tokio::test]
    async fn limits_concurrent_sends() {
        let TestDrain {
            logger,
            task,
            handle: _handle,
            mut requests,
        } = build(builder().max_concurrency(2), 1);

        for msg in ["a", "b", "c"] {
            info!(logger, "{}", msg);
        }

        tokio::spawn(task);

        let first = next_request(&mut requests).await;
        let second = next_request(&mut requests).await;

        assert_no_request(&mut requests).await;

        let mut batches = vec![first.batch.clone(), second.batch.clone()];

        first.respond_ok();

        batches.push(next_request(&mut requests).await.batch);
        batches.sort();

        assert_eq!(batches, [["a"], ["b"], ["c"]]);
    }

    #[tokio::test]
    async fn bounded_queue_drops_oldest_messages_but_keeps_flushes() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder().queue_capacity(Some(1)), 10);

        let mut flush = std::pin::pin!(handle.flush());

        assert!(poll!(flush.as_mut()).is_pending());

        // the flush stays in front of "b" even though "a" gets dropped
        info!(logger, "a");
        info!(logger, "b");

        tokio::spawn(task);

        let req = next_request(&mut requests).await;

        assert_eq!(req.batch, ["b"]);

        // the flush completes while "b" is still in flight
        within(flush).await;
    }

    #[tokio::test]
    async fn flush_completes_when_task_is_dropped() {
        let TestDrain {
            logger,
            task,
            handle,
            requests: _requests,
        } = build(builder(), 10);

        info!(logger, "a");

        let mut flush = std::pin::pin!(handle.flush());

        assert!(poll!(flush.as_mut()).is_pending());

        std::mem::drop(task);

        within(flush).await;
        within(handle.flush()).await;
    }

    #[tokio::test]
    async fn task_delivers_pending_messages_before_finishing() {
        let TestDrain {
            logger,
            task,
            handle,
            mut requests,
        } = build(builder(), 10);

        info!(logger, "a");

        std::mem::drop(logger);
        std::mem::drop(handle);

        let task = tokio::spawn(task);

        let req = next_request(&mut requests).await;

        assert_eq!(req.batch, ["a"]);

        tokio::time::sleep(Duration::from_millis(50)).await;

        assert!(!task.is_finished());

        req.respond_ok();

        within(task).await.unwrap();
    }
}