triglav 0.2.0

High-performance multi-path networking tool with intelligent uplink management
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
//! Prometheus metrics export.
//!
//! Exposes internal metrics in Prometheus format for scraping.

use std::sync::Arc;
use std::time::Duration;

use prometheus::{
    Encoder, Gauge, GaugeVec, Histogram, HistogramOpts, HistogramVec, IntCounter, IntCounterVec,
    IntGauge, IntGaugeVec, Opts, Registry, TextEncoder,
};

/// Prometheus metrics registry and collectors.
pub struct PrometheusMetrics {
    registry: Registry,

    // Connection metrics
    pub connections_total: IntCounter,
    pub connections_active: IntGauge,
    pub connections_by_user: IntGaugeVec,

    // Session metrics
    pub sessions_total: IntCounter,
    pub sessions_active: IntGauge,
    pub session_duration_seconds: Histogram,

    // Uplink metrics
    pub uplinks_total: IntGauge,
    pub uplinks_active: IntGauge,
    pub uplink_state: IntGaugeVec,
    pub uplink_health: GaugeVec,

    // Traffic metrics
    pub bytes_sent_total: IntCounterVec,
    pub bytes_received_total: IntCounterVec,
    pub packets_sent_total: IntCounterVec,
    pub packets_received_total: IntCounterVec,
    pub packets_dropped_total: IntCounterVec,
    pub packets_retransmitted_total: IntCounterVec,

    // Latency metrics
    pub rtt_seconds: GaugeVec,
    pub rtt_histogram: HistogramVec,
    pub jitter_seconds: GaugeVec,

    // Quality metrics
    pub packet_loss_ratio: GaugeVec,
    pub quality_score: GaugeVec,
    pub bandwidth_bytes_per_sec: GaugeVec,

    // NAT metrics
    pub nat_type: IntGaugeVec,
    pub external_port: IntGaugeVec,

    // Flow metrics
    pub active_flows: IntGauge,
    pub flow_bindings: IntGaugeVec,

    // Scheduler metrics
    pub scheduler_selections_total: IntCounterVec,
    pub scheduler_latency_seconds: Histogram,

    // Crypto metrics
    pub handshakes_total: IntCounter,
    pub handshakes_failed: IntCounter,
    pub rekeys_total: IntCounter,
    pub encrypt_operations: IntCounter,
    pub decrypt_operations: IntCounter,

    // Error metrics
    pub errors_total: IntCounterVec,

    // Server metrics (if running as server)
    pub server_uptime_seconds: Gauge,
    pub users_total: IntGauge,
    pub users_active: IntGauge,
}

impl PrometheusMetrics {
    /// Create a new metrics instance with all collectors registered.
    pub fn new() -> Result<Self, prometheus::Error> {
        let registry = Registry::new();

        // Connection metrics
        let connections_total = IntCounter::new(
            "triglav_connections_total",
            "Total number of connections established",
        )?;
        let connections_active = IntGauge::new(
            "triglav_connections_active",
            "Number of currently active connections",
        )?;
        let connections_by_user = IntGaugeVec::new(
            Opts::new("triglav_connections_by_user", "Active connections per user"),
            &["user_id"],
        )?;

        // Session metrics
        let sessions_total =
            IntCounter::new("triglav_sessions_total", "Total number of sessions created")?;
        let sessions_active = IntGauge::new(
            "triglav_sessions_active",
            "Number of currently active sessions",
        )?;
        let session_duration_seconds = Histogram::with_opts(
            HistogramOpts::new(
                "triglav_session_duration_seconds",
                "Session duration in seconds",
            )
            .buckets(vec![
                1.0, 5.0, 10.0, 30.0, 60.0, 300.0, 600.0, 1800.0, 3600.0,
            ]),
        )?;

        // Uplink metrics
        let uplinks_total = IntGauge::new(
            "triglav_uplinks_total",
            "Total number of configured uplinks",
        )?;
        let uplinks_active = IntGauge::new(
            "triglav_uplinks_active",
            "Number of currently active/usable uplinks",
        )?;
        let uplink_state = IntGaugeVec::new(
            Opts::new(
                "triglav_uplink_state",
                "Uplink connection state (0=disconnected, 1=connecting, 2=connected, 3=failed)",
            ),
            &["uplink_id", "interface"],
        )?;
        let uplink_health = GaugeVec::new(
            Opts::new("triglav_uplink_health", "Uplink health score (0-1)"),
            &["uplink_id", "interface"],
        )?;

        // Traffic metrics
        let bytes_sent_total = IntCounterVec::new(
            Opts::new("triglav_bytes_sent_total", "Total bytes sent"),
            &["uplink_id"],
        )?;
        let bytes_received_total = IntCounterVec::new(
            Opts::new("triglav_bytes_received_total", "Total bytes received"),
            &["uplink_id"],
        )?;
        let packets_sent_total = IntCounterVec::new(
            Opts::new("triglav_packets_sent_total", "Total packets sent"),
            &["uplink_id"],
        )?;
        let packets_received_total = IntCounterVec::new(
            Opts::new("triglav_packets_received_total", "Total packets received"),
            &["uplink_id"],
        )?;
        let packets_dropped_total = IntCounterVec::new(
            Opts::new("triglav_packets_dropped_total", "Total packets dropped"),
            &["uplink_id", "reason"],
        )?;
        let packets_retransmitted_total = IntCounterVec::new(
            Opts::new(
                "triglav_packets_retransmitted_total",
                "Total packets retransmitted",
            ),
            &["uplink_id"],
        )?;

        // Latency metrics
        let rtt_seconds = GaugeVec::new(
            Opts::new("triglav_rtt_seconds", "Current smoothed RTT in seconds"),
            &["uplink_id"],
        )?;
        let rtt_histogram = HistogramVec::new(
            HistogramOpts::new(
                "triglav_rtt_histogram_seconds",
                "RTT distribution in seconds",
            )
            .buckets(vec![
                0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5,
            ]),
            &["uplink_id"],
        )?;
        let jitter_seconds = GaugeVec::new(
            Opts::new(
                "triglav_jitter_seconds",
                "Current jitter (RTT variance) in seconds",
            ),
            &["uplink_id"],
        )?;

        // Quality metrics
        let packet_loss_ratio = GaugeVec::new(
            Opts::new("triglav_packet_loss_ratio", "Packet loss ratio (0-1)"),
            &["uplink_id"],
        )?;
        let quality_score = GaugeVec::new(
            Opts::new("triglav_quality_score", "Overall quality score (0-100)"),
            &["uplink_id"],
        )?;
        let bandwidth_bytes_per_sec = GaugeVec::new(
            Opts::new(
                "triglav_bandwidth_bytes_per_sec",
                "Estimated bandwidth in bytes/sec",
            ),
            &["uplink_id", "direction"],
        )?;

        // NAT metrics
        let nat_type = IntGaugeVec::new(
            Opts::new("triglav_nat_type", "NAT type (0=none, 1=full_cone, 2=restricted, 3=port_restricted, 4=symmetric, 5=unknown)"),
            &["uplink_id"]
        )?;
        let external_port = IntGaugeVec::new(
            Opts::new("triglav_external_port", "External port as seen by server"),
            &["uplink_id"],
        )?;

        // Flow metrics
        let active_flows = IntGauge::new("triglav_active_flows", "Number of active flows")?;
        let flow_bindings = IntGaugeVec::new(
            Opts::new(
                "triglav_flow_bindings",
                "Number of flows bound to each uplink",
            ),
            &["uplink_id"],
        )?;

        // Scheduler metrics
        let scheduler_selections_total = IntCounterVec::new(
            Opts::new(
                "triglav_scheduler_selections_total",
                "Total scheduler selections per uplink",
            ),
            &["uplink_id", "strategy"],
        )?;
        let scheduler_latency_seconds = Histogram::with_opts(
            HistogramOpts::new(
                "triglav_scheduler_latency_seconds",
                "Time spent in scheduler selection",
            )
            .buckets(vec![0.00001, 0.00005, 0.0001, 0.0005, 0.001, 0.005, 0.01]),
        )?;

        // Crypto metrics
        let handshakes_total = IntCounter::new(
            "triglav_handshakes_total",
            "Total number of Noise handshakes completed",
        )?;
        let handshakes_failed = IntCounter::new(
            "triglav_handshakes_failed",
            "Total number of failed Noise handshakes",
        )?;
        let rekeys_total =
            IntCounter::new("triglav_rekeys_total", "Total number of rekey operations")?;
        let encrypt_operations = IntCounter::new(
            "triglav_encrypt_operations_total",
            "Total encryption operations",
        )?;
        let decrypt_operations = IntCounter::new(
            "triglav_decrypt_operations_total",
            "Total decryption operations",
        )?;

        // Error metrics
        let errors_total = IntCounterVec::new(
            Opts::new("triglav_errors_total", "Total errors by type"),
            &["error_type"],
        )?;

        // Server metrics
        let server_uptime_seconds =
            Gauge::new("triglav_server_uptime_seconds", "Server uptime in seconds")?;
        let users_total = IntGauge::new("triglav_users_total", "Total registered users")?;
        let users_active = IntGauge::new("triglav_users_active", "Currently active users")?;

        // Register all metrics
        registry.register(Box::new(connections_total.clone()))?;
        registry.register(Box::new(connections_active.clone()))?;
        registry.register(Box::new(connections_by_user.clone()))?;
        registry.register(Box::new(sessions_total.clone()))?;
        registry.register(Box::new(sessions_active.clone()))?;
        registry.register(Box::new(session_duration_seconds.clone()))?;
        registry.register(Box::new(uplinks_total.clone()))?;
        registry.register(Box::new(uplinks_active.clone()))?;
        registry.register(Box::new(uplink_state.clone()))?;
        registry.register(Box::new(uplink_health.clone()))?;
        registry.register(Box::new(bytes_sent_total.clone()))?;
        registry.register(Box::new(bytes_received_total.clone()))?;
        registry.register(Box::new(packets_sent_total.clone()))?;
        registry.register(Box::new(packets_received_total.clone()))?;
        registry.register(Box::new(packets_dropped_total.clone()))?;
        registry.register(Box::new(packets_retransmitted_total.clone()))?;
        registry.register(Box::new(rtt_seconds.clone()))?;
        registry.register(Box::new(rtt_histogram.clone()))?;
        registry.register(Box::new(jitter_seconds.clone()))?;
        registry.register(Box::new(packet_loss_ratio.clone()))?;
        registry.register(Box::new(quality_score.clone()))?;
        registry.register(Box::new(bandwidth_bytes_per_sec.clone()))?;
        registry.register(Box::new(nat_type.clone()))?;
        registry.register(Box::new(external_port.clone()))?;
        registry.register(Box::new(active_flows.clone()))?;
        registry.register(Box::new(flow_bindings.clone()))?;
        registry.register(Box::new(scheduler_selections_total.clone()))?;
        registry.register(Box::new(scheduler_latency_seconds.clone()))?;
        registry.register(Box::new(handshakes_total.clone()))?;
        registry.register(Box::new(handshakes_failed.clone()))?;
        registry.register(Box::new(rekeys_total.clone()))?;
        registry.register(Box::new(encrypt_operations.clone()))?;
        registry.register(Box::new(decrypt_operations.clone()))?;
        registry.register(Box::new(errors_total.clone()))?;
        registry.register(Box::new(server_uptime_seconds.clone()))?;
        registry.register(Box::new(users_total.clone()))?;
        registry.register(Box::new(users_active.clone()))?;

        Ok(Self {
            registry,
            connections_total,
            connections_active,
            connections_by_user,
            sessions_total,
            sessions_active,
            session_duration_seconds,
            uplinks_total,
            uplinks_active,
            uplink_state,
            uplink_health,
            bytes_sent_total,
            bytes_received_total,
            packets_sent_total,
            packets_received_total,
            packets_dropped_total,
            packets_retransmitted_total,
            rtt_seconds,
            rtt_histogram,
            jitter_seconds,
            packet_loss_ratio,
            quality_score,
            bandwidth_bytes_per_sec,
            nat_type,
            external_port,
            active_flows,
            flow_bindings,
            scheduler_selections_total,
            scheduler_latency_seconds,
            handshakes_total,
            handshakes_failed,
            rekeys_total,
            encrypt_operations,
            decrypt_operations,
            errors_total,
            server_uptime_seconds,
            users_total,
            users_active,
        })
    }

    /// Encode metrics to Prometheus text format.
    pub fn encode(&self) -> Result<String, prometheus::Error> {
        let encoder = TextEncoder::new();
        let metric_families = self.registry.gather();
        let mut buffer = Vec::new();
        encoder.encode(&metric_families, &mut buffer)?;
        Ok(String::from_utf8(buffer).unwrap_or_default())
    }

    /// Record an RTT observation.
    pub fn record_rtt(&self, uplink_id: &str, rtt: Duration) {
        let seconds = rtt.as_secs_f64();
        self.rtt_seconds
            .with_label_values(&[uplink_id])
            .set(seconds);
        self.rtt_histogram
            .with_label_values(&[uplink_id])
            .observe(seconds);
    }

    /// Record packet sent.
    pub fn record_packet_sent(&self, uplink_id: &str, bytes: u64) {
        self.packets_sent_total
            .with_label_values(&[uplink_id])
            .inc();
        self.bytes_sent_total
            .with_label_values(&[uplink_id])
            .inc_by(bytes);
    }

    /// Record packet received.
    pub fn record_packet_received(&self, uplink_id: &str, bytes: u64) {
        self.packets_received_total
            .with_label_values(&[uplink_id])
            .inc();
        self.bytes_received_total
            .with_label_values(&[uplink_id])
            .inc_by(bytes);
    }

    /// Record packet dropped.
    pub fn record_packet_dropped(&self, uplink_id: &str, reason: &str) {
        self.packets_dropped_total
            .with_label_values(&[uplink_id, reason])
            .inc();
    }

    /// Record uplink state.
    pub fn set_uplink_state(&self, uplink_id: &str, interface: &str, state: i64) {
        self.uplink_state
            .with_label_values(&[uplink_id, interface])
            .set(state);
    }

    /// Record uplink health.
    pub fn set_uplink_health(&self, uplink_id: &str, interface: &str, health: f64) {
        self.uplink_health
            .with_label_values(&[uplink_id, interface])
            .set(health);
    }

    /// Record uplink quality metrics.
    pub fn set_uplink_quality(&self, uplink_id: &str, loss: f64, score: f64, jitter_secs: f64) {
        self.packet_loss_ratio
            .with_label_values(&[uplink_id])
            .set(loss);
        self.quality_score
            .with_label_values(&[uplink_id])
            .set(score);
        self.jitter_seconds
            .with_label_values(&[uplink_id])
            .set(jitter_secs);
    }

    /// Record bandwidth.
    pub fn set_bandwidth(&self, uplink_id: &str, send_bps: f64, recv_bps: f64) {
        self.bandwidth_bytes_per_sec
            .with_label_values(&[uplink_id, "send"])
            .set(send_bps);
        self.bandwidth_bytes_per_sec
            .with_label_values(&[uplink_id, "recv"])
            .set(recv_bps);
    }

    /// Record error.
    pub fn record_error(&self, error_type: &str) {
        self.errors_total.with_label_values(&[error_type]).inc();
    }

    /// Record scheduler selection.
    pub fn record_scheduler_selection(&self, uplink_id: &str, strategy: &str, latency: Duration) {
        self.scheduler_selections_total
            .with_label_values(&[uplink_id, strategy])
            .inc();
        self.scheduler_latency_seconds
            .observe(latency.as_secs_f64());
    }
}

impl Default for PrometheusMetrics {
    fn default() -> Self {
        Self::new().expect("Failed to create Prometheus metrics")
    }
}

/// Global metrics instance.
static METRICS: std::sync::OnceLock<Arc<PrometheusMetrics>> = std::sync::OnceLock::new();

/// Initialize the global metrics instance.
pub fn init_metrics() -> Arc<PrometheusMetrics> {
    METRICS
        .get_or_init(|| Arc::new(PrometheusMetrics::new().expect("Failed to initialize metrics")))
        .clone()
}

/// Get the global metrics instance.
pub fn get_metrics() -> Option<Arc<PrometheusMetrics>> {
    METRICS.get().cloned()
}

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

    #[test]
    fn test_metrics_creation() {
        let metrics = PrometheusMetrics::new().unwrap();

        metrics.connections_total.inc();
        metrics.record_rtt("uplink-1", Duration::from_millis(50));
        metrics.record_packet_sent("uplink-1", 1000);

        let output = metrics.encode().unwrap();
        assert!(output.contains("triglav_connections_total"));
        assert!(output.contains("triglav_rtt_seconds"));
    }

    #[test]
    fn test_global_metrics() {
        let m1 = init_metrics();
        let m2 = init_metrics();

        // Should be the same instance
        assert!(Arc::ptr_eq(&m1, &m2));
    }
}