communitas-core 0.12.1

Core business logic for Communitas - PQC collaboration with virtual disks
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Metrics collection and reporting for Communitas.
//!
//! This module provides both in-memory metrics for local monitoring and
//! OpenTelemetry integration for production observability.
//!
//! # Metrics Collected
//!
//! - **Sync latency**: Histogram of sync operation durations
//! - **Error rates**: Counter of errors by category
//! - **Message queue depth**: Gauge of pending messages
//! - **CRDT conflicts**: Counter of merge conflicts
//! - **Storage operations**: Counter of storage operations by type
//!
//! # Example
//!
//! ```rust,ignore
//! use communitas_bindings::metrics::{Metrics, SyncMetrics};
//!
//! let metrics = Metrics::new();
//!
//! // Record sync operation
//! let start = std::time::Instant::now();
//! // ... perform sync ...
//! metrics.record_sync_latency("push", "success", start.elapsed());
//!
//! // Record error
//! metrics.record_error("sync_error", "error");
//!
//! // Update queue depth
//! metrics.set_queue_depth("outbound", 42);
//! ```

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use tokio::sync::RwLock;

#[cfg(feature = "metrics")]
use opentelemetry::metrics::{Counter, Histogram, UpDownCounter};
#[cfg(feature = "metrics")]
use opentelemetry::{KeyValue, global};

/// Central metrics registry for Communitas.
///
/// This struct provides both in-memory metrics (always available) and
/// OpenTelemetry export (when the `metrics` feature is enabled and
/// telemetry is configured).
#[derive(Debug)]
pub struct Metrics {
    /// In-memory storage metrics
    storage: StorageMetrics,
    /// In-memory counters
    counters: Arc<RwLock<HashMap<String, AtomicU64>>>,
    /// In-memory gauges
    gauges: Arc<RwLock<HashMap<String, AtomicU64>>>,
    /// OpenTelemetry sync latency histogram
    #[cfg(feature = "metrics")]
    sync_latency: Histogram<f64>,
    /// OpenTelemetry error counter
    #[cfg(feature = "metrics")]
    error_counter: Counter<u64>,
    /// OpenTelemetry queue depth gauge
    #[cfg(feature = "metrics")]
    queue_depth: UpDownCounter<i64>,
    /// OpenTelemetry CRDT conflict counter
    #[cfg(feature = "metrics")]
    crdt_conflicts: Counter<u64>,
}

impl Metrics {
    /// Creates a new metrics registry.
    ///
    /// When the `metrics` feature is enabled, this initializes OpenTelemetry
    /// meters for export. Otherwise, only in-memory metrics are available.
    pub fn new() -> Self {
        #[cfg(feature = "metrics")]
        {
            let meter = global::meter("communitas");

            let sync_latency = meter
                .f64_histogram("communitas_sync_latency_seconds")
                .with_description("Duration of sync operations in seconds")
                .with_unit(opentelemetry::metrics::Unit::new("s"))
                .init();

            let error_counter = meter
                .u64_counter("communitas_errors_total")
                .with_description("Total number of errors by category")
                .init();

            let queue_depth = meter
                .i64_up_down_counter("communitas_message_queue_depth")
                .with_description("Current depth of message queues")
                .init();

            let crdt_conflicts = meter
                .u64_counter("communitas_crdt_conflicts_total")
                .with_description("Total number of CRDT merge conflicts")
                .init();

            Self {
                storage: StorageMetrics::new(),
                counters: Arc::new(RwLock::new(HashMap::new())),
                gauges: Arc::new(RwLock::new(HashMap::new())),
                sync_latency,
                error_counter,
                queue_depth,
                crdt_conflicts,
            }
        }

        #[cfg(not(feature = "metrics"))]
        Self {
            storage: StorageMetrics::new(),
            counters: Arc::new(RwLock::new(HashMap::new())),
            gauges: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Records a sync operation latency.
    ///
    /// # Arguments
    ///
    /// * `operation` - The sync operation type ("push", "pull", "merge")
    /// * `status` - The operation status ("success", "error")
    /// * `duration` - The operation duration
    pub fn record_sync_latency(&self, operation: &str, status: &str, duration: Duration) {
        // Always record to in-memory metrics
        let key = format!("sync_latency_{}_{}", operation, status);
        if let Ok(mut counters) = self.counters.try_write() {
            counters
                .entry(key)
                .or_insert_with(|| AtomicU64::new(0))
                .fetch_add(1, Ordering::Relaxed);
        }

        // Record to OpenTelemetry when enabled
        #[cfg(feature = "metrics")]
        {
            let seconds = duration.as_secs_f64();
            let attributes = [
                KeyValue::new("operation", operation.to_string()),
                KeyValue::new("status", status.to_string()),
            ];
            self.sync_latency.record(seconds, &attributes);
        }

        tracing::trace!(
            operation = operation,
            status = status,
            duration_ms = duration.as_millis() as u64,
            "Sync latency recorded"
        );
    }

    /// Records an error occurrence.
    ///
    /// # Arguments
    ///
    /// * `category` - The error category ("sync_error", "storage_error", "network_error", "crypto_error")
    /// * `severity` - The error severity ("error", "warning")
    pub fn record_error(&self, category: &str, severity: &str) {
        // Always record to in-memory metrics
        let key = format!("errors_{}_{}", category, severity);
        if let Ok(mut counters) = self.counters.try_write() {
            counters
                .entry(key)
                .or_insert_with(|| AtomicU64::new(0))
                .fetch_add(1, Ordering::Relaxed);
        }

        // Record to OpenTelemetry when enabled
        #[cfg(feature = "metrics")]
        {
            let attributes = [
                KeyValue::new("category", category.to_string()),
                KeyValue::new("severity", severity.to_string()),
            ];
            self.error_counter.add(1, &attributes);
        }

        tracing::debug!(
            category = category,
            severity = severity,
            "Error recorded in metrics"
        );
    }

    /// Updates the message queue depth gauge.
    ///
    /// # Arguments
    ///
    /// * `queue_type` - The queue type ("outbound", "inbound", "retry")
    /// * `depth` - The current queue depth
    pub fn set_queue_depth(&self, queue_type: &str, depth: i64) {
        // Store in in-memory gauges
        let key = format!("queue_depth_{}", queue_type);
        if let Ok(mut gauges) = self.gauges.try_write() {
            gauges
                .entry(key)
                .or_insert_with(|| AtomicU64::new(0))
                .store(depth as u64, Ordering::Relaxed);
        }

        // Record delta to OpenTelemetry when enabled
        // Note: UpDownCounter tracks cumulative changes, so we record the absolute value
        // In a real implementation, we'd track the previous value to calculate delta
        #[cfg(feature = "metrics")]
        {
            let attributes = [KeyValue::new("queue_type", queue_type.to_string())];
            // For UpDownCounter, we need to track delta
            // This simplified version just records the absolute value
            self.queue_depth.add(depth, &attributes);
        }

        tracing::trace!(
            queue_type = queue_type,
            depth = depth,
            "Queue depth updated"
        );
    }

    /// Increments the queue depth by a delta.
    ///
    /// # Arguments
    ///
    /// * `queue_type` - The queue type ("outbound", "inbound", "retry")
    /// * `delta` - The change in depth (positive for enqueue, negative for dequeue)
    pub fn increment_queue_depth(&self, queue_type: &str, delta: i64) {
        // Update in-memory gauges
        let key = format!("queue_depth_{}", queue_type);
        if let Ok(mut gauges) = self.gauges.try_write() {
            let counter = gauges.entry(key).or_insert_with(|| AtomicU64::new(0));
            if delta >= 0 {
                counter.fetch_add(delta as u64, Ordering::Relaxed);
            } else {
                counter.fetch_sub((-delta) as u64, Ordering::Relaxed);
            }
        }

        // Record to OpenTelemetry when enabled
        #[cfg(feature = "metrics")]
        {
            let attributes = [KeyValue::new("queue_type", queue_type.to_string())];
            self.queue_depth.add(delta, &attributes);
        }
    }

    /// Records a CRDT merge conflict.
    ///
    /// # Arguments
    ///
    /// * `doc_type` - The document type ("message", "kanban", "canvas")
    /// * `resolution` - The resolution strategy ("automatic", "manual")
    pub fn record_crdt_conflict(&self, doc_type: &str, resolution: &str) {
        // Always record to in-memory metrics
        let key = format!("crdt_conflicts_{}_{}", doc_type, resolution);
        if let Ok(mut counters) = self.counters.try_write() {
            counters
                .entry(key)
                .or_insert_with(|| AtomicU64::new(0))
                .fetch_add(1, Ordering::Relaxed);
        }

        // Record to OpenTelemetry when enabled
        #[cfg(feature = "metrics")]
        {
            let attributes = [
                KeyValue::new("doc_type", doc_type.to_string()),
                KeyValue::new("resolution", resolution.to_string()),
            ];
            self.crdt_conflicts.add(1, &attributes);
        }

        tracing::debug!(
            doc_type = doc_type,
            resolution = resolution,
            "CRDT conflict recorded"
        );
    }

    /// Returns a reference to the storage metrics.
    pub fn storage(&self) -> &StorageMetrics {
        &self.storage
    }

    /// Gets the current value of all in-memory metrics.
    pub async fn get_all_metrics(&self) -> HashMap<String, u64> {
        let mut result = self.storage.get_current_metrics().await;

        let counters = self.counters.read().await;
        for (key, counter) in counters.iter() {
            result.insert(key.clone(), counter.load(Ordering::Relaxed));
        }

        let gauges = self.gauges.read().await;
        for (key, gauge) in gauges.iter() {
            result.insert(key.clone(), gauge.load(Ordering::Relaxed));
        }

        result
    }
}

impl Default for Metrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Collects and manages storage system metrics.
#[derive(Debug)]
pub struct StorageMetrics {
    personal_storage_ops: Arc<RwLock<u64>>,
    group_storage_ops: Arc<RwLock<u64>>,
    dht_storage_ops: Arc<RwLock<u64>>,
}

impl StorageMetrics {
    /// Creates a new storage metrics collector.
    pub fn new() -> Self {
        Self {
            personal_storage_ops: Arc::new(RwLock::new(0)),
            group_storage_ops: Arc::new(RwLock::new(0)),
            dht_storage_ops: Arc::new(RwLock::new(0)),
        }
    }

    /// Records a personal storage operation.
    pub async fn record_personal_storage(&self, _size: usize) {
        let mut ops = self.personal_storage_ops.write().await;
        *ops += 1;
    }

    /// Records a group storage operation.
    pub async fn record_group_storage(&self, _group_id: &str, _size: usize, _shards: usize) {
        let mut ops = self.group_storage_ops.write().await;
        *ops += 1;
    }

    /// Records a local cache hit.
    pub async fn record_local_hit(&self) {
        // Stub implementation
    }

    /// Records a DHT fallback.
    pub async fn record_dht_fallback(&self) {
        // Stub implementation
    }

    /// Records a successful Reed-Solomon recovery.
    pub async fn record_reed_solomon_success(&self) {
        // Stub implementation
    }

    /// Records a DHT backup used.
    pub async fn record_dht_backup_used(&self) {
        // Stub implementation
    }

    /// Records a DHT storage acceptance.
    pub async fn record_dht_storage_accepted(&self, _size: usize, _requester: &str) {
        let mut ops = self.dht_storage_ops.write().await;
        *ops += 1;
    }

    /// Gets all current storage metrics.
    pub async fn get_current_metrics(&self) -> HashMap<String, u64> {
        let personal_ops = *self.personal_storage_ops.read().await;
        let group_ops = *self.group_storage_ops.read().await;
        let dht_ops = *self.dht_storage_ops.read().await;

        let mut metrics = HashMap::new();
        metrics.insert("personal_storage_ops".to_string(), personal_ops);
        metrics.insert("group_storage_ops".to_string(), group_ops);
        metrics.insert("dht_storage_ops".to_string(), dht_ops);

        metrics
    }
}

impl Default for StorageMetrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Global metrics instance for convenience.
///
/// This provides a singleton-like interface for recording metrics
/// from anywhere in the codebase without passing the Metrics struct around.
static GLOBAL_METRICS: once_cell::sync::Lazy<Metrics> = once_cell::sync::Lazy::new(Metrics::new);

/// Returns the global metrics instance.
pub fn global_metrics() -> &'static Metrics {
    &GLOBAL_METRICS
}

/// Helper to time and record a sync operation.
///
/// # Example
///
/// ```rust,ignore
/// use communitas_bindings::metrics::timed_sync;
///
/// let result = timed_sync("push", || async {
///     // perform sync operation
///     Ok(())
/// }).await;
/// ```
pub async fn timed_sync<F, Fut, T, E>(operation: &str, f: F) -> std::result::Result<T, E>
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = std::result::Result<T, E>>,
    E: std::fmt::Display,
{
    let start = std::time::Instant::now();
    let result = f().await;
    let duration = start.elapsed();

    let status = if result.is_ok() { "success" } else { "error" };
    global_metrics().record_sync_latency(operation, status, duration);

    if result.is_err() {
        global_metrics().record_error("sync_error", "error");
    }

    result
}

/// Records an error with automatic category detection.
///
/// # Arguments
///
/// * `error` - The error to record
/// * `category` - Optional category override (defaults to auto-detection)
pub fn record_app_error<E: std::fmt::Display>(error: &E, category: Option<&str>) {
    let error_str = error.to_string().to_lowercase();
    let detected_category = category.unwrap_or_else(|| {
        if error_str.contains("network") || error_str.contains("connection") {
            "network_error"
        } else if error_str.contains("storage") || error_str.contains("io") {
            "storage_error"
        } else if error_str.contains("crypto") || error_str.contains("signature") {
            "crypto_error"
        } else if error_str.contains("sync") {
            "sync_error"
        } else {
            "other_error"
        }
    });

    global_metrics().record_error(detected_category, "error");
    tracing::debug!(
        category = detected_category,
        error = %error,
        "Application error recorded"
    );
}

/// Tracks message queue operations.
pub struct QueueTracker {
    queue_type: &'static str,
}

impl QueueTracker {
    /// Creates a new queue tracker.
    pub fn new(queue_type: &'static str) -> Self {
        Self { queue_type }
    }

    /// Records an enqueue operation.
    pub fn enqueue(&self, count: u32) {
        global_metrics().increment_queue_depth(self.queue_type, count as i64);
    }

    /// Records a dequeue operation.
    pub fn dequeue(&self, count: u32) {
        global_metrics().increment_queue_depth(self.queue_type, -(count as i64));
    }

    /// Sets the absolute queue depth.
    pub fn set_depth(&self, depth: u32) {
        global_metrics().set_queue_depth(self.queue_type, depth as i64);
    }
}

/// Records a CRDT conflict with automatic resolution tracking.
///
/// # Arguments
///
/// * `doc_type` - The document type ("message", "kanban", "canvas")
/// * `manual` - Whether manual resolution was required
pub fn record_conflict(doc_type: &str, manual: bool) {
    let resolution = if manual { "manual" } else { "automatic" };
    global_metrics().record_crdt_conflict(doc_type, resolution);
}

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

    #[test]
    fn test_metrics_creation() {
        let metrics = Metrics::new();
        assert!(metrics.counters.try_read().is_ok());
    }

    #[test]
    fn test_record_sync_latency() {
        let metrics = Metrics::new();
        metrics.record_sync_latency("push", "success", Duration::from_millis(100));
        // Just verify no panic
    }

    #[test]
    fn test_record_error() {
        let metrics = Metrics::new();
        metrics.record_error("sync_error", "error");
        metrics.record_error("network_error", "warning");
        // Just verify no panic
    }

    #[test]
    fn test_queue_depth() {
        let metrics = Metrics::new();
        metrics.set_queue_depth("outbound", 10);
        metrics.increment_queue_depth("outbound", 5);
        metrics.increment_queue_depth("outbound", -3);
        // Just verify no panic
    }

    #[test]
    fn test_crdt_conflict() {
        let metrics = Metrics::new();
        metrics.record_crdt_conflict("message", "automatic");
        metrics.record_crdt_conflict("kanban", "manual");
        // Just verify no panic
    }

    #[tokio::test]
    async fn test_get_all_metrics() {
        let metrics = Metrics::new();
        metrics.record_sync_latency("push", "success", Duration::from_millis(100));
        metrics.record_error("test", "error");

        let all = metrics.get_all_metrics().await;
        assert!(all.contains_key("personal_storage_ops"));
    }

    #[test]
    fn test_global_metrics() {
        let metrics = global_metrics();
        metrics.record_error("test", "error");
        // Just verify no panic
    }
}