elasticq 0.3.0

Thread-safe, dynamically resizable queues with lock-based and lock-free implementations for high-throughput scenarios
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
//! Metrics and observability integration for ElasticQ.
//!
//! This module provides integration with the `metrics` crate for production
//! monitoring and observability. It exposes key metrics as counters, gauges,
//! and histograms that can be collected by various backends (Prometheus, etc.).

use metrics_crate::{counter, gauge, histogram, describe_counter, describe_gauge, describe_histogram, Unit};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Instant;

/// Metric names as constants
pub mod names {
    /// Counter: Total messages enqueued
    pub const MESSAGES_ENQUEUED: &str = "elasticq_messages_enqueued_total";
    /// Counter: Total messages dequeued
    pub const MESSAGES_DEQUEUED: &str = "elasticq_messages_dequeued_total";
    /// Counter: Total messages dropped (due to capacity limits)
    pub const MESSAGES_DROPPED: &str = "elasticq_messages_dropped_total";
    /// Gauge: Current queue depth
    pub const QUEUE_DEPTH: &str = "elasticq_queue_depth";
    /// Gauge: Current queue capacity
    pub const QUEUE_CAPACITY: &str = "elasticq_queue_capacity";
    /// Counter: Total resize operations
    pub const RESIZE_OPERATIONS: &str = "elasticq_resize_operations_total";
    /// Histogram: Enqueue latency in seconds
    pub const ENQUEUE_LATENCY: &str = "elasticq_enqueue_latency_seconds";
    /// Histogram: Dequeue latency in seconds
    pub const DEQUEUE_LATENCY: &str = "elasticq_dequeue_latency_seconds";
    /// Counter: Backpressure events (when buffer is full)
    pub const BACKPRESSURE_EVENTS: &str = "elasticq_backpressure_events_total";
    /// Gauge: Queue utilization ratio (0.0 - 1.0)
    pub const QUEUE_UTILIZATION: &str = "elasticq_queue_utilization_ratio";
}

/// Register metric descriptions with the metrics registry.
///
/// Call this once at application startup to provide descriptions for all metrics.
///
/// # Example
///
/// ```ignore
/// use elasticq::metrics::register_metrics;
///
/// fn main() {
///     // Initialize your metrics backend (e.g., prometheus)
///     // ...
///
///     // Register ElasticQ metric descriptions
///     register_metrics();
/// }
/// ```
pub fn register_metrics() {
    describe_counter!(
        names::MESSAGES_ENQUEUED,
        Unit::Count,
        "Total number of messages successfully enqueued"
    );
    describe_counter!(
        names::MESSAGES_DEQUEUED,
        Unit::Count,
        "Total number of messages successfully dequeued"
    );
    describe_counter!(
        names::MESSAGES_DROPPED,
        Unit::Count,
        "Total number of messages dropped due to capacity limits"
    );
    describe_gauge!(
        names::QUEUE_DEPTH,
        Unit::Count,
        "Current number of messages in the queue"
    );
    describe_gauge!(
        names::QUEUE_CAPACITY,
        Unit::Count,
        "Current capacity of the queue"
    );
    describe_counter!(
        names::RESIZE_OPERATIONS,
        Unit::Count,
        "Total number of queue resize operations"
    );
    describe_histogram!(
        names::ENQUEUE_LATENCY,
        Unit::Seconds,
        "Time taken to enqueue a message"
    );
    describe_histogram!(
        names::DEQUEUE_LATENCY,
        Unit::Seconds,
        "Time taken to dequeue a message"
    );
    describe_counter!(
        names::BACKPRESSURE_EVENTS,
        Unit::Count,
        "Number of times enqueue was rejected due to full queue"
    );
    describe_gauge!(
        names::QUEUE_UTILIZATION,
        Unit::Count,
        "Queue utilization ratio (depth / capacity)"
    );
}

/// A metrics recorder for a specific buffer instance.
///
/// This struct wraps a buffer and automatically records metrics for all operations.
/// Uses static queue name labels for efficient metric recording.
///
/// # Example
///
/// ```ignore
/// use elasticq::{DynamicCircularBuffer, Config};
/// use elasticq::metrics::MetricsRecorder;
///
/// let buffer = DynamicCircularBuffer::<i32>::new(Config::default()).unwrap();
/// let recorder = MetricsRecorder::new("my_queue");
///
/// // Record a push operation
/// let start = std::time::Instant::now();
/// buffer.push(42).unwrap();
/// recorder.record_enqueue(start);
///
/// // Or use the instrumented wrapper
/// let instrumented = recorder.wrap(buffer);
/// instrumented.push(42).unwrap(); // Metrics recorded automatically
/// ```
#[derive(Clone)]
pub struct MetricsRecorder {
    /// Queue name for labeling
    name: &'static str,
    /// Whether metrics are enabled
    enabled: Arc<AtomicBool>,
}

impl MetricsRecorder {
    /// Create a new metrics recorder with the given queue name.
    ///
    /// Note: The name must be a static string for efficient metric recording.
    pub fn new(name: &'static str) -> Self {
        Self {
            name,
            enabled: Arc::new(AtomicBool::new(true)),
        }
    }

    /// Enable or disable metrics recording.
    pub fn set_enabled(&self, enabled: bool) {
        self.enabled.store(enabled, Ordering::Relaxed);
    }

    /// Check if metrics recording is enabled.
    pub fn is_enabled(&self) -> bool {
        self.enabled.load(Ordering::Relaxed)
    }

    /// Get the queue name.
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// Record a successful enqueue operation.
    pub fn record_enqueue(&self, start: Instant) {
        if !self.is_enabled() {
            return;
        }

        let duration = start.elapsed();
        counter!(names::MESSAGES_ENQUEUED, "queue" => self.name).increment(1);
        histogram!(names::ENQUEUE_LATENCY, "queue" => self.name).record(duration.as_secs_f64());
    }

    /// Record a successful dequeue operation.
    pub fn record_dequeue(&self, start: Instant) {
        if !self.is_enabled() {
            return;
        }

        let duration = start.elapsed();
        counter!(names::MESSAGES_DEQUEUED, "queue" => self.name).increment(1);
        histogram!(names::DEQUEUE_LATENCY, "queue" => self.name).record(duration.as_secs_f64());
    }

    /// Record a dropped message (due to capacity limits).
    pub fn record_dropped(&self) {
        if !self.is_enabled() {
            return;
        }

        counter!(names::MESSAGES_DROPPED, "queue" => self.name).increment(1);
    }

    /// Record a backpressure event (enqueue rejected due to full queue).
    pub fn record_backpressure(&self) {
        if !self.is_enabled() {
            return;
        }

        counter!(names::BACKPRESSURE_EVENTS, "queue" => self.name).increment(1);
    }

    /// Record a resize operation.
    pub fn record_resize(&self) {
        if !self.is_enabled() {
            return;
        }

        counter!(names::RESIZE_OPERATIONS, "queue" => self.name).increment(1);
    }

    /// Update queue depth gauge.
    pub fn set_queue_depth(&self, depth: usize) {
        if !self.is_enabled() {
            return;
        }

        gauge!(names::QUEUE_DEPTH, "queue" => self.name).set(depth as f64);
    }

    /// Update queue capacity gauge.
    pub fn set_queue_capacity(&self, capacity: usize) {
        if !self.is_enabled() {
            return;
        }

        gauge!(names::QUEUE_CAPACITY, "queue" => self.name).set(capacity as f64);
    }

    /// Update queue utilization ratio.
    pub fn set_utilization(&self, depth: usize, capacity: usize) {
        if !self.is_enabled() {
            return;
        }

        let utilization = if capacity > 0 {
            depth as f64 / capacity as f64
        } else {
            0.0
        };
        gauge!(names::QUEUE_UTILIZATION, "queue" => self.name).set(utilization);
    }

    /// Record a batch of enqueue operations.
    pub fn record_batch_enqueue(&self, count: usize, start: Instant) {
        if !self.is_enabled() {
            return;
        }

        let duration = start.elapsed();
        counter!(names::MESSAGES_ENQUEUED, "queue" => self.name).increment(count as u64);
        // Record average latency per item
        let per_item_duration = duration.as_secs_f64() / count as f64;
        histogram!(names::ENQUEUE_LATENCY, "queue" => self.name).record(per_item_duration);
    }

    /// Record a batch of dequeue operations.
    pub fn record_batch_dequeue(&self, count: usize, start: Instant) {
        if !self.is_enabled() {
            return;
        }

        let duration = start.elapsed();
        counter!(names::MESSAGES_DEQUEUED, "queue" => self.name).increment(count as u64);
        let per_item_duration = duration.as_secs_f64() / count as f64;
        histogram!(names::DEQUEUE_LATENCY, "queue" => self.name).record(per_item_duration);
    }

    /// Create an instrumented buffer wrapper.
    pub fn wrap<T>(&self, buffer: crate::DynamicCircularBuffer<T>) -> InstrumentedBuffer<T> {
        InstrumentedBuffer {
            buffer,
            recorder: self.clone(),
        }
    }

    /// Create an instrumented buffer wrapper from an Arc.
    pub fn wrap_arc<T>(
        &self,
        buffer: std::sync::Arc<crate::DynamicCircularBuffer<T>>,
    ) -> InstrumentedBufferRef<T> {
        InstrumentedBufferRef {
            buffer,
            recorder: self.clone(),
        }
    }
}

/// A buffer wrapper that automatically records metrics.
pub struct InstrumentedBuffer<T> {
    buffer: crate::DynamicCircularBuffer<T>,
    recorder: MetricsRecorder,
}

impl<T: Send + Sync + 'static> InstrumentedBuffer<T> {
    /// Push an item with automatic metrics recording.
    pub fn push(&self, item: T) -> crate::BufferResult<()> {
        let start = Instant::now();
        let result = self.buffer.push(item);

        match &result {
            Ok(()) => {
                self.recorder.record_enqueue(start);
                self.update_gauges();
            }
            Err(crate::BufferError::Full) | Err(crate::BufferError::MaxCapacityReached(_)) => {
                self.recorder.record_backpressure();
            }
            _ => {}
        }

        result
    }

    /// Pop an item with automatic metrics recording.
    pub fn pop(&self) -> crate::BufferResult<T> {
        let start = Instant::now();
        let result = self.buffer.pop();

        if result.is_ok() {
            self.recorder.record_dequeue(start);
            self.update_gauges();
        }

        result
    }

    /// Push a batch with automatic metrics recording.
    pub fn push_batch(&self, items: Vec<T>) -> crate::BufferResult<()> {
        let count = items.len();
        let start = Instant::now();
        let result = self.buffer.push_batch(items);

        match &result {
            Ok(()) => {
                self.recorder.record_batch_enqueue(count, start);
                self.update_gauges();
            }
            Err(crate::BufferError::Full) | Err(crate::BufferError::MaxCapacityReached(_)) => {
                self.recorder.record_backpressure();
            }
            _ => {}
        }

        result
    }

    /// Pop a batch with automatic metrics recording.
    pub fn pop_batch(&self, max_items: usize) -> crate::BufferResult<Vec<T>> {
        let start = Instant::now();
        let result = self.buffer.pop_batch(max_items);

        if let Ok(ref items) = result {
            if !items.is_empty() {
                self.recorder.record_batch_dequeue(items.len(), start);
                self.update_gauges();
            }
        }

        result
    }

    /// Get the current queue length.
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    /// Check if the queue is empty.
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Get the current capacity.
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Get a reference to the underlying buffer.
    pub fn inner(&self) -> &crate::DynamicCircularBuffer<T> {
        &self.buffer
    }

    /// Get the metrics recorder.
    pub fn recorder(&self) -> &MetricsRecorder {
        &self.recorder
    }

    fn update_gauges(&self) {
        let depth = self.buffer.len();
        let capacity = self.buffer.capacity();
        self.recorder.set_queue_depth(depth);
        self.recorder.set_queue_capacity(capacity);
        self.recorder.set_utilization(depth, capacity);
    }
}

/// A buffer reference wrapper that automatically records metrics.
pub struct InstrumentedBufferRef<T> {
    buffer: std::sync::Arc<crate::DynamicCircularBuffer<T>>,
    recorder: MetricsRecorder,
}

impl<T> Clone for InstrumentedBufferRef<T> {
    fn clone(&self) -> Self {
        Self {
            buffer: self.buffer.clone(),
            recorder: self.recorder.clone(),
        }
    }
}

impl<T: Send + Sync + 'static> InstrumentedBufferRef<T> {
    /// Push an item with automatic metrics recording.
    pub fn push(&self, item: T) -> crate::BufferResult<()> {
        let start = Instant::now();
        let result = self.buffer.push(item);

        match &result {
            Ok(()) => {
                self.recorder.record_enqueue(start);
                self.update_gauges();
            }
            Err(crate::BufferError::Full) | Err(crate::BufferError::MaxCapacityReached(_)) => {
                self.recorder.record_backpressure();
            }
            _ => {}
        }

        result
    }

    /// Pop an item with automatic metrics recording.
    pub fn pop(&self) -> crate::BufferResult<T> {
        let start = Instant::now();
        let result = self.buffer.pop();

        if result.is_ok() {
            self.recorder.record_dequeue(start);
            self.update_gauges();
        }

        result
    }

    /// Push a batch with automatic metrics recording.
    pub fn push_batch(&self, items: Vec<T>) -> crate::BufferResult<()> {
        let count = items.len();
        let start = Instant::now();
        let result = self.buffer.push_batch(items);

        match &result {
            Ok(()) => {
                self.recorder.record_batch_enqueue(count, start);
                self.update_gauges();
            }
            Err(crate::BufferError::Full) | Err(crate::BufferError::MaxCapacityReached(_)) => {
                self.recorder.record_backpressure();
            }
            _ => {}
        }

        result
    }

    /// Pop a batch with automatic metrics recording.
    pub fn pop_batch(&self, max_items: usize) -> crate::BufferResult<Vec<T>> {
        let start = Instant::now();
        let result = self.buffer.pop_batch(max_items);

        if let Ok(ref items) = result {
            if !items.is_empty() {
                self.recorder.record_batch_dequeue(items.len(), start);
                self.update_gauges();
            }
        }

        result
    }

    /// Get the current queue length.
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    /// Check if the queue is empty.
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Get the current capacity.
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Get a reference to the underlying buffer.
    pub fn inner(&self) -> &std::sync::Arc<crate::DynamicCircularBuffer<T>> {
        &self.buffer
    }

    /// Get the metrics recorder.
    pub fn recorder(&self) -> &MetricsRecorder {
        &self.recorder
    }

    fn update_gauges(&self) {
        let depth = self.buffer.len();
        let capacity = self.buffer.capacity();
        self.recorder.set_queue_depth(depth);
        self.recorder.set_queue_capacity(capacity);
        self.recorder.set_utilization(depth, capacity);
    }
}

/// A timer guard that automatically records duration on drop.
pub struct TimerGuard<'a> {
    recorder: &'a MetricsRecorder,
    start: Instant,
    operation: TimerOperation,
    cancelled: bool,
}

enum TimerOperation {
    Enqueue,
    Dequeue,
}

impl<'a> TimerGuard<'a> {
    /// Create a new timer for enqueue operations.
    pub fn enqueue(recorder: &'a MetricsRecorder) -> Self {
        Self {
            recorder,
            start: Instant::now(),
            operation: TimerOperation::Enqueue,
            cancelled: false,
        }
    }

    /// Create a new timer for dequeue operations.
    pub fn dequeue(recorder: &'a MetricsRecorder) -> Self {
        Self {
            recorder,
            start: Instant::now(),
            operation: TimerOperation::Dequeue,
            cancelled: false,
        }
    }

    /// Complete the timer and record metrics.
    pub fn complete(self) {
        // Drop will handle the recording
    }

    /// Cancel the timer without recording metrics.
    pub fn cancel(mut self) {
        self.cancelled = true;
    }
}

impl<'a> Drop for TimerGuard<'a> {
    fn drop(&mut self) {
        if self.cancelled {
            return;
        }
        match self.operation {
            TimerOperation::Enqueue => self.recorder.record_enqueue(self.start),
            TimerOperation::Dequeue => self.recorder.record_dequeue(self.start),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_metrics_recorder_creation() {
        let recorder = MetricsRecorder::new("test_queue");
        assert_eq!(recorder.name(), "test_queue");
        assert!(recorder.is_enabled());
    }

    #[test]
    fn test_metrics_enable_disable() {
        let recorder = MetricsRecorder::new("test_queue");
        assert!(recorder.is_enabled());

        recorder.set_enabled(false);
        assert!(!recorder.is_enabled());

        recorder.set_enabled(true);
        assert!(recorder.is_enabled());
    }

    #[test]
    fn test_instrumented_buffer() {
        use crate::{Config, DynamicCircularBuffer};

        let buffer = DynamicCircularBuffer::<i32>::new(Config::default()).unwrap();
        let recorder = MetricsRecorder::new("test");
        let instrumented = recorder.wrap(buffer);

        instrumented.push(1).unwrap();
        instrumented.push(2).unwrap();

        assert_eq!(instrumented.len(), 2);
        assert_eq!(instrumented.pop().unwrap(), 1);
        assert_eq!(instrumented.len(), 1);
    }

    #[test]
    fn test_timer_guard() {
        let recorder = MetricsRecorder::new("test");

        // Test enqueue timer
        {
            let _guard = TimerGuard::enqueue(&recorder);
            std::thread::sleep(Duration::from_millis(1));
            // Timer records on drop
        }

        // Test dequeue timer
        {
            let guard = TimerGuard::dequeue(&recorder);
            std::thread::sleep(Duration::from_millis(1));
            guard.complete();
        }

        // Test cancel
        {
            let guard = TimerGuard::enqueue(&recorder);
            guard.cancel(); // Should not record
        }
    }

    #[test]
    fn test_batch_operations() {
        use crate::{Config, DynamicCircularBuffer};

        let buffer = DynamicCircularBuffer::<i32>::new(Config::default()).unwrap();
        let recorder = MetricsRecorder::new("test");
        let instrumented = recorder.wrap(buffer);

        instrumented.push_batch(vec![1, 2, 3, 4, 5]).unwrap();
        assert_eq!(instrumented.len(), 5);

        let items = instrumented.pop_batch(3).unwrap();
        assert_eq!(items, vec![1, 2, 3]);
        assert_eq!(instrumented.len(), 2);
    }
}