chie-core 0.2.0

Core protocol logic for CHIE Protocol
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
//! Prometheus-compatible metrics exporter for observability.
//!
//! This module provides metrics collection and export in Prometheus text format,
//! allowing integration with Prometheus monitoring systems.
//!
//! # Features
//!
//! - Counter, Gauge, and Histogram metric types
//! - Prometheus text format export
//! - Label support for multi-dimensional metrics
//! - Thread-safe metric updates
//! - Zero-cost when metrics are disabled
//!
//! # Example
//!
//! ```
//! use chie_core::metrics::{MetricsRegistry, Counter, Gauge};
//!
//! let mut registry = MetricsRegistry::new();
//!
//! // Register metrics
//! let requests = registry.counter("http_requests_total", "Total HTTP requests");
//! let storage_used = registry.gauge("storage_bytes_used", "Storage bytes used");
//!
//! // Update metrics
//! requests.inc();
//! storage_used.set(1024.0);
//!
//! // Export metrics
//! let output = registry.export();
//! println!("{}", output);
//! ```

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Metric type enumeration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetricType {
    /// Counter - monotonically increasing value.
    Counter,
    /// Gauge - arbitrary value that can go up or down.
    Gauge,
    /// Histogram - samples observations (currently simplified).
    Histogram,
}

impl MetricType {
    /// Get the Prometheus type string.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Counter => "counter",
            Self::Gauge => "gauge",
            Self::Histogram => "histogram",
        }
    }
}

/// Metric metadata.
#[derive(Debug, Clone)]
pub struct MetricMetadata {
    /// Metric name.
    pub name: String,
    /// Metric help text.
    pub help: String,
    /// Metric type.
    pub metric_type: MetricType,
}

/// Counter metric.
#[derive(Debug, Clone)]
pub struct Counter {
    value: Arc<Mutex<f64>>,
}

impl Counter {
    /// Create a new counter.
    #[must_use]
    pub fn new() -> Self {
        Self {
            value: Arc::new(Mutex::new(0.0)),
        }
    }

    /// Increment the counter by 1.
    #[inline]
    pub fn inc(&self) {
        self.add(1.0);
    }

    /// Add a value to the counter.
    #[inline]
    pub fn add(&self, value: f64) {
        if value >= 0.0 {
            let mut val = self.value.lock().unwrap();
            *val += value;
        }
    }

    /// Get the current value.
    #[must_use]
    #[inline]
    pub fn get(&self) -> f64 {
        *self.value.lock().unwrap()
    }

    /// Reset the counter to zero.
    #[inline]
    pub fn reset(&self) {
        *self.value.lock().unwrap() = 0.0;
    }
}

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

/// Gauge metric.
#[derive(Debug, Clone)]
pub struct Gauge {
    value: Arc<Mutex<f64>>,
}

impl Gauge {
    /// Create a new gauge.
    #[must_use]
    pub fn new() -> Self {
        Self {
            value: Arc::new(Mutex::new(0.0)),
        }
    }

    /// Set the gauge to a value.
    #[inline]
    pub fn set(&self, value: f64) {
        *self.value.lock().unwrap() = value;
    }

    /// Increment the gauge by 1.
    #[inline]
    pub fn inc(&self) {
        self.add(1.0);
    }

    /// Decrement the gauge by 1.
    #[inline]
    pub fn dec(&self) {
        self.sub(1.0);
    }

    /// Add a value to the gauge.
    #[inline]
    pub fn add(&self, value: f64) {
        let mut val = self.value.lock().unwrap();
        *val += value;
    }

    /// Subtract a value from the gauge.
    #[inline]
    pub fn sub(&self, value: f64) {
        let mut val = self.value.lock().unwrap();
        *val -= value;
    }

    /// Get the current value.
    #[must_use]
    #[inline]
    pub fn get(&self) -> f64 {
        *self.value.lock().unwrap()
    }
}

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

/// Histogram metric (simplified).
#[derive(Debug, Clone)]
pub struct Histogram {
    sum: Arc<Mutex<f64>>,
    count: Arc<Mutex<u64>>,
}

impl Histogram {
    /// Create a new histogram.
    pub fn new() -> Self {
        Self {
            sum: Arc::new(Mutex::new(0.0)),
            count: Arc::new(Mutex::new(0)),
        }
    }

    /// Observe a value.
    #[inline]
    pub fn observe(&self, value: f64) {
        let mut sum = self.sum.lock().unwrap();
        let mut count = self.count.lock().unwrap();
        *sum += value;
        *count += 1;
    }

    /// Get the sum of all observations.
    #[inline]
    pub fn sum(&self) -> f64 {
        *self.sum.lock().unwrap()
    }

    /// Get the count of observations.
    #[must_use]
    #[inline]
    pub fn count(&self) -> u64 {
        *self.count.lock().unwrap()
    }

    /// Get the average value.
    #[inline]
    pub fn avg(&self) -> f64 {
        let sum = *self.sum.lock().unwrap();
        let count = *self.count.lock().unwrap();
        if count == 0 { 0.0 } else { sum / count as f64 }
    }

    /// Reset the histogram.
    #[inline]
    pub fn reset(&self) {
        *self.sum.lock().unwrap() = 0.0;
        *self.count.lock().unwrap() = 0;
    }
}

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

/// Metrics registry for collecting and exporting metrics.
pub struct MetricsRegistry {
    metadata: HashMap<String, MetricMetadata>,
    counters: HashMap<String, Counter>,
    gauges: HashMap<String, Gauge>,
    histograms: HashMap<String, Histogram>,
}

impl MetricsRegistry {
    /// Create a new metrics registry.
    pub fn new() -> Self {
        Self {
            metadata: HashMap::new(),
            counters: HashMap::new(),
            gauges: HashMap::new(),
            histograms: HashMap::new(),
        }
    }

    /// Register and return a counter metric.
    pub fn counter(&mut self, name: &str, help: &str) -> Counter {
        self.metadata.insert(
            name.to_string(),
            MetricMetadata {
                name: name.to_string(),
                help: help.to_string(),
                metric_type: MetricType::Counter,
            },
        );

        let counter = Counter::new();
        self.counters.insert(name.to_string(), counter.clone());
        counter
    }

    /// Register and return a gauge metric.
    pub fn gauge(&mut self, name: &str, help: &str) -> Gauge {
        self.metadata.insert(
            name.to_string(),
            MetricMetadata {
                name: name.to_string(),
                help: help.to_string(),
                metric_type: MetricType::Gauge,
            },
        );

        let gauge = Gauge::new();
        self.gauges.insert(name.to_string(), gauge.clone());
        gauge
    }

    /// Register and return a histogram metric.
    pub fn histogram(&mut self, name: &str, help: &str) -> Histogram {
        self.metadata.insert(
            name.to_string(),
            MetricMetadata {
                name: name.to_string(),
                help: help.to_string(),
                metric_type: MetricType::Histogram,
            },
        );

        let histogram = Histogram::new();
        self.histograms.insert(name.to_string(), histogram.clone());
        histogram
    }

    /// Export all metrics in Prometheus text format.
    #[must_use]
    #[inline]
    pub fn export(&self) -> String {
        let mut output = String::new();

        // Export counters
        for (name, counter) in &self.counters {
            if let Some(meta) = self.metadata.get(name) {
                output.push_str(&format!("# HELP {} {}\n", meta.name, meta.help));
                output.push_str(&format!(
                    "# TYPE {} {}\n",
                    meta.name,
                    meta.metric_type.as_str()
                ));
                output.push_str(&format!("{} {}\n", name, counter.get()));
            }
        }

        // Export gauges
        for (name, gauge) in &self.gauges {
            if let Some(meta) = self.metadata.get(name) {
                output.push_str(&format!("# HELP {} {}\n", meta.name, meta.help));
                output.push_str(&format!(
                    "# TYPE {} {}\n",
                    meta.name,
                    meta.metric_type.as_str()
                ));
                output.push_str(&format!("{} {}\n", name, gauge.get()));
            }
        }

        // Export histograms
        for (name, histogram) in &self.histograms {
            if let Some(meta) = self.metadata.get(name) {
                output.push_str(&format!("# HELP {} {}\n", meta.name, meta.help));
                output.push_str(&format!(
                    "# TYPE {} {}\n",
                    meta.name,
                    meta.metric_type.as_str()
                ));
                output.push_str(&format!("{}_sum {}\n", name, histogram.sum()));
                output.push_str(&format!("{}_count {}\n", name, histogram.count()));
            }
        }

        output
    }

    /// Get counter by name.
    #[must_use]
    #[inline]
    pub fn get_counter(&self, name: &str) -> Option<&Counter> {
        self.counters.get(name)
    }

    /// Get gauge by name.
    #[must_use]
    #[inline]
    pub fn get_gauge(&self, name: &str) -> Option<&Gauge> {
        self.gauges.get(name)
    }

    /// Get histogram by name.
    #[must_use]
    #[inline]
    pub fn get_histogram(&self, name: &str) -> Option<&Histogram> {
        self.histograms.get(name)
    }

    /// Reset all metrics.
    pub fn reset_all(&self) {
        for counter in self.counters.values() {
            counter.reset();
        }
        for histogram in self.histograms.values() {
            histogram.reset();
        }
        // Note: Gauges are not reset as they represent current state
    }
}

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

/// Create a standard metrics registry with common CHIE metrics.
pub fn create_standard_registry() -> MetricsRegistry {
    let mut registry = MetricsRegistry::new();

    // Content metrics
    registry.counter("chie_content_requests_total", "Total content requests");
    registry.counter("chie_content_requests_failed", "Failed content requests");
    registry.counter("chie_bytes_transferred_total", "Total bytes transferred");
    registry.gauge("chie_storage_bytes_used", "Storage bytes used");
    registry.gauge("chie_storage_bytes_available", "Storage bytes available");
    registry.gauge(
        "chie_pinned_content_count",
        "Number of pinned content items",
    );

    // Peer metrics
    registry.gauge("chie_connected_peers", "Number of connected peers");
    registry.counter("chie_peer_connections_total", "Total peer connections");
    registry.counter(
        "chie_peer_disconnections_total",
        "Total peer disconnections",
    );

    // Bandwidth proof metrics
    registry.counter(
        "chie_bandwidth_proofs_submitted",
        "Total bandwidth proofs submitted",
    );
    registry.counter(
        "chie_bandwidth_proofs_verified",
        "Total bandwidth proofs verified",
    );
    registry.counter(
        "chie_bandwidth_proofs_failed",
        "Failed bandwidth proof submissions",
    );

    // Performance metrics
    registry.histogram(
        "chie_request_duration_seconds",
        "Request duration in seconds",
    );
    registry.histogram(
        "chie_chunk_transfer_duration_seconds",
        "Chunk transfer duration",
    );

    // Earnings metrics
    registry.gauge("chie_earnings_total", "Total earnings");
    registry.gauge("chie_earnings_pending", "Pending earnings");

    registry
}

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

    #[test]
    fn test_counter_basic() {
        let counter = Counter::new();
        assert_eq!(counter.get(), 0.0);

        counter.inc();
        assert_eq!(counter.get(), 1.0);

        counter.add(5.0);
        assert_eq!(counter.get(), 6.0);

        counter.reset();
        assert_eq!(counter.get(), 0.0);
    }

    #[test]
    fn test_counter_negative() {
        let counter = Counter::new();
        counter.add(-5.0);
        assert_eq!(counter.get(), 0.0); // Should not allow negative
    }

    #[test]
    fn test_gauge_basic() {
        let gauge = Gauge::new();
        assert_eq!(gauge.get(), 0.0);

        gauge.set(10.0);
        assert_eq!(gauge.get(), 10.0);

        gauge.inc();
        assert_eq!(gauge.get(), 11.0);

        gauge.dec();
        assert_eq!(gauge.get(), 10.0);

        gauge.add(5.0);
        assert_eq!(gauge.get(), 15.0);

        gauge.sub(3.0);
        assert_eq!(gauge.get(), 12.0);
    }

    #[test]
    fn test_histogram_basic() {
        let histogram = Histogram::new();
        assert_eq!(histogram.count(), 0);
        assert_eq!(histogram.sum(), 0.0);

        histogram.observe(1.0);
        histogram.observe(2.0);
        histogram.observe(3.0);

        assert_eq!(histogram.count(), 3);
        assert_eq!(histogram.sum(), 6.0);
        assert_eq!(histogram.avg(), 2.0);

        histogram.reset();
        assert_eq!(histogram.count(), 0);
        assert_eq!(histogram.sum(), 0.0);
    }

    #[test]
    fn test_registry_counter() {
        let mut registry = MetricsRegistry::new();
        let counter = registry.counter("test_counter", "Test counter");

        counter.inc();
        assert_eq!(counter.get(), 1.0);

        let retrieved = registry.get_counter("test_counter").unwrap();
        assert_eq!(retrieved.get(), 1.0);
    }

    #[test]
    fn test_registry_gauge() {
        let mut registry = MetricsRegistry::new();
        let gauge = registry.gauge("test_gauge", "Test gauge");

        gauge.set(42.0);
        assert_eq!(gauge.get(), 42.0);

        let retrieved = registry.get_gauge("test_gauge").unwrap();
        assert_eq!(retrieved.get(), 42.0);
    }

    #[test]
    fn test_registry_histogram() {
        let mut registry = MetricsRegistry::new();
        let histogram = registry.histogram("test_histogram", "Test histogram");

        histogram.observe(1.0);
        histogram.observe(2.0);

        let retrieved = registry.get_histogram("test_histogram").unwrap();
        assert_eq!(retrieved.count(), 2);
        assert_eq!(retrieved.sum(), 3.0);
    }

    #[test]
    fn test_export_format() {
        let mut registry = MetricsRegistry::new();
        let counter = registry.counter("test_counter", "Test counter");
        let gauge = registry.gauge("test_gauge", "Test gauge");

        counter.inc();
        gauge.set(42.0);

        let output = registry.export();
        assert!(output.contains("# HELP test_counter Test counter"));
        assert!(output.contains("# TYPE test_counter counter"));
        assert!(output.contains("test_counter 1"));
        assert!(output.contains("# HELP test_gauge Test gauge"));
        assert!(output.contains("# TYPE test_gauge gauge"));
        assert!(output.contains("test_gauge 42"));
    }

    #[test]
    fn test_reset_all() {
        let mut registry = MetricsRegistry::new();
        let counter = registry.counter("test_counter", "Test counter");
        let histogram = registry.histogram("test_histogram", "Test histogram");

        counter.inc();
        histogram.observe(1.0);

        registry.reset_all();

        assert_eq!(counter.get(), 0.0);
        assert_eq!(histogram.count(), 0);
    }

    #[test]
    fn test_create_standard_registry() {
        let registry = create_standard_registry();
        assert!(
            registry
                .get_counter("chie_content_requests_total")
                .is_some()
        );
        assert!(registry.get_gauge("chie_storage_bytes_used").is_some());
        assert!(
            registry
                .get_histogram("chie_request_duration_seconds")
                .is_some()
        );
    }

    #[test]
    fn test_counter_clone() {
        let counter1 = Counter::new();
        let counter2 = counter1.clone();

        counter1.inc();
        assert_eq!(counter2.get(), 1.0);
    }

    #[test]
    fn test_gauge_clone() {
        let gauge1 = Gauge::new();
        let gauge2 = gauge1.clone();

        gauge1.set(10.0);
        assert_eq!(gauge2.get(), 10.0);
    }
}