upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/metrics/mod.rs
blob: a16b9fe993c5a69f97f301f1428ee32efb9b2d59 (plain)
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
//! Prometheus metrics for ngit-grasp relay.
//!
//! This module provides comprehensive monitoring metrics including:
//! - WebSocket connection tracking (with privacy-preserving IP aggregation)
//! - Git operation metrics (clone, fetch, push)
//! - Repository bandwidth tracking (top-N only for cardinality control)
//! - Nostr event metrics
//! - Sync metrics (GRASP-02 proactive sync)
//!
//! # Privacy
//! IP addresses are NEVER exposed in metrics. The `ConnectionTracker` maintains
//! per-IP counts internally only for abuse detection. Only aggregate counts
//! are exposed to Prometheus.

pub mod bandwidth;
pub mod connection;

pub use crate::sync::SyncMetrics;

use std::sync::Arc;
use std::time::Instant;

use lazy_static::lazy_static;
use prometheus::{
    Counter, CounterVec, Encoder, Gauge, GaugeVec, Histogram, HistogramOpts, HistogramVec, Opts,
    Registry, TextEncoder,
};

use bandwidth::BandwidthTracker;
use connection::ConnectionTracker;

lazy_static! {
    /// Global Prometheus registry for ngit-grasp metrics
    pub static ref REGISTRY: Registry = Registry::new();
}

/// Central metrics collection for ngit-grasp relay.
///
/// Thread-safe and designed for concurrent access from multiple tokio tasks.
#[derive(Clone)]
pub struct Metrics {
    inner: Arc<MetricsInner>,
}

struct MetricsInner {
    /// Connection tracking with abuse detection
    pub connection_tracker: ConnectionTracker,

    /// Repository bandwidth tracking (top-N only)
    pub bandwidth_tracker: BandwidthTracker,

    /// Sync metrics (GRASP-02 proactive sync)
    pub sync_metrics: Option<crate::sync::SyncMetrics>,

    // === WebSocket Metrics ===
    /// Total WebSocket connections since startup
    pub websocket_connections_total: Counter,
    /// Connection duration histogram
    pub websocket_connection_duration: Histogram,
    /// Messages received by type (REQ, EVENT, CLOSE)
    pub websocket_messages_received: CounterVec,
    /// Messages sent by type (EVENT, EOSE, OK, NOTICE)
    pub websocket_messages_sent: CounterVec,

    // === Git Operation Metrics ===
    /// Git operations by type and status
    pub git_operations_total: CounterVec,
    /// Git operation duration histogram
    pub git_operation_duration: HistogramVec,
    /// Total bytes transferred
    pub git_bytes_total: CounterVec,
    /// Push authorization results
    pub git_push_authorization: CounterVec,

    // === Nostr Event Metrics ===
    /// Events received by kind
    pub events_received_total: CounterVec,
    /// Events successfully stored by kind
    pub events_stored_total: CounterVec,
    /// Events rejected by kind and reason
    pub events_rejected_total: CounterVec,

    // === Repository Metrics ===
    /// Total repositories hosted (counted from disk on each metrics request)
    pub repositories_total: Gauge,
    /// Git data directory path for counting repositories on disk
    pub git_data_path: Option<String>,

    // === System Health Metrics ===
    /// Server start time for uptime calculation
    pub start_time: Instant,
    /// Build information gauge (stored to prevent unregistration from Prometheus)
    #[allow(dead_code)]
    pub build_info: GaugeVec,
}

impl Metrics {
    /// Creates a new Metrics instance and registers all metrics with Prometheus.
    ///
    /// # Arguments
    /// * `abuse_threshold` - Number of connections from a single IP before flagging as abuse
    /// * `git_data_path` - Optional path to git data directory for counting repositories
    pub fn new(abuse_threshold: u32, git_data_path: Option<String>) -> Self {
        let inner = MetricsInner::new(abuse_threshold, git_data_path);
        Self {
            inner: Arc::new(inner),
        }
    }

    /// Returns the sync metrics if registered.
    pub fn sync_metrics(&self) -> Option<&crate::sync::SyncMetrics> {
        self.inner.sync_metrics.as_ref()
    }

    /// Returns the connection tracker for WebSocket connection management.
    pub fn connection_tracker(&self) -> &ConnectionTracker {
        &self.inner.connection_tracker
    }

    /// Returns the bandwidth tracker for repository bandwidth tracking.
    pub fn bandwidth_tracker(&self) -> &BandwidthTracker {
        &self.inner.bandwidth_tracker
    }

    // === WebSocket Recording Methods ===

    /// Record a new WebSocket connection
    pub fn record_websocket_connection(&self) {
        self.inner.websocket_connections_total.inc();
    }

    /// Start timing a WebSocket connection, returns timer that records on drop
    pub fn start_connection_timer(&self) -> HistogramTimer {
        HistogramTimer::new(self.inner.websocket_connection_duration.clone())
    }

    /// Record a received WebSocket message
    pub fn record_message_received(&self, msg_type: &str) {
        self.inner
            .websocket_messages_received
            .with_label_values(&[msg_type])
            .inc();
    }

    /// Record a sent WebSocket message
    pub fn record_message_sent(&self, msg_type: &str) {
        self.inner
            .websocket_messages_sent
            .with_label_values(&[msg_type])
            .inc();
    }

    // === Git Operation Recording Methods ===

    /// Record a git operation completion
    pub fn record_git_operation(&self, operation: &str, status: &str) {
        self.inner
            .git_operations_total
            .with_label_values(&[operation, status])
            .inc();
    }

    /// Start timing a git operation, returns a timer
    pub fn start_git_operation_timer(&self, operation: &str) -> GitOperationTimer {
        GitOperationTimer::new(
            self.inner.git_operation_duration.clone(),
            operation.to_string(),
        )
    }

    /// Record bytes transferred for a git operation
    pub fn record_git_bytes(&self, direction: &str, bytes: u64) {
        self.inner
            .git_bytes_total
            .with_label_values(&[direction])
            .inc_by(bytes as f64);
    }

    /// Record a push authorization result
    pub fn record_push_authorization(&self, result: &str) {
        self.inner
            .git_push_authorization
            .with_label_values(&[result])
            .inc();
    }

    // === Nostr Event Recording Methods ===

    /// Record a received Nostr event
    pub fn record_event_received(&self, kind: u64) {
        self.inner
            .events_received_total
            .with_label_values(&[&kind.to_string()])
            .inc();
    }

    /// Record a stored Nostr event
    pub fn record_event_stored(&self, kind: u64) {
        self.inner
            .events_stored_total
            .with_label_values(&[&kind.to_string()])
            .inc();
    }

    /// Record a rejected Nostr event
    pub fn record_event_rejected(&self, kind: u64, reason: &str) {
        self.inner
            .events_rejected_total
            .with_label_values(&[&kind.to_string(), reason])
            .inc();
    }

    // === Repository Metrics ===

    /// Set the total number of repositories
    pub fn set_repositories_total(&self, count: u64) {
        self.inner.repositories_total.set(count as f64);
    }

    /// Count all git repositories on disk.
    ///
    /// This scans the git data directory for all `*.git` directories.
    ///
    /// # Arguments
    /// * `git_data_path` - Path to the git data directory (e.g., "./data/git")
    ///
    /// # Returns
    /// The number of repositories found on disk
    pub fn count_repositories_on_disk(git_data_path: &str) -> u64 {
        use std::fs;
        use std::path::Path;

        let git_dir = Path::new(git_data_path);
        if !git_dir.exists() {
            return 0;
        }

        let mut count = 0u64;
        if let Ok(entries) = fs::read_dir(git_dir) {
            for npub_entry in entries.flatten() {
                if let Ok(npub_meta) = npub_entry.metadata() {
                    if npub_meta.is_dir() {
                        // This is a npub directory, scan for *.git repos inside
                        if let Ok(repo_entries) = fs::read_dir(npub_entry.path()) {
                            for repo_entry in repo_entries.flatten() {
                                if let Some(name) = repo_entry.file_name().to_str() {
                                    if name.ends_with(".git") {
                                        if let Ok(repo_meta) = repo_entry.metadata() {
                                            if repo_meta.is_dir() {
                                                count += 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        count
    }

    // === Rendering ===

    /// Render all metrics in Prometheus text format.
    ///
    /// This method:
    /// 1. Refreshes the top-N bandwidth metrics if needed
    /// 2. Counts repositories on disk (if git_data_path configured)
    /// 3. Updates uptime
    /// 4. Gathers all metrics from the registry
    /// 5. Encodes them in Prometheus text format
    pub fn render(&self) -> String {
        // Refresh top-N bandwidth repos if needed
        self.inner.bandwidth_tracker.maybe_refresh_top_n();

        // Count repositories on disk and update metric
        if let Some(git_data_path) = &self.inner.git_data_path {
            let count = Self::count_repositories_on_disk(git_data_path);
            self.inner.repositories_total.set(count as f64);
        }

        // Gather and encode metrics
        let encoder = TextEncoder::new();
        let metric_families = REGISTRY.gather();
        let mut buffer = Vec::new();
        encoder.encode(&metric_families, &mut buffer).unwrap();

        // Add uptime as a comment (it's derived, not a registered metric)
        let uptime = self.inner.start_time.elapsed().as_secs();
        let mut output = String::from_utf8(buffer).unwrap();
        output.push_str(&format!(
            "\n# HELP ngit_uptime_seconds Seconds since server startup\n# TYPE ngit_uptime_seconds counter\nngit_uptime_seconds {}\n",
            uptime
        ));

        output
    }

    /// Check if the system is under high load (for sync scheduling)
    pub fn is_high_load(&self, threshold: u64) -> bool {
        self.inner.connection_tracker.active_connections() > threshold
    }
}

impl MetricsInner {
    fn new(abuse_threshold: u32, git_data_path: Option<String>) -> Self {
        // Create connection tracker
        let connection_tracker = ConnectionTracker::new(abuse_threshold, &REGISTRY);

        // Create bandwidth tracker
        let bandwidth_tracker = BandwidthTracker::new(&REGISTRY);

        // Create sync metrics (may fail if already registered in tests)
        let sync_metrics = crate::sync::SyncMetrics::register(&REGISTRY).ok();
        if sync_metrics.is_some() {
            tracing::info!("Sync metrics registered with Prometheus");
        }

        // WebSocket metrics
        let websocket_connections_total = Counter::with_opts(Opts::new(
            "ngit_websocket_connections_total",
            "Total WebSocket connections since startup",
        ))
        .unwrap();
        REGISTRY
            .register(Box::new(websocket_connections_total.clone()))
            .unwrap();

        let websocket_connection_duration = Histogram::with_opts(
            HistogramOpts::new(
                "ngit_websocket_connection_duration_seconds",
                "Duration of WebSocket connections",
            )
            .buckets(vec![1.0, 5.0, 15.0, 30.0, 60.0, 300.0, 900.0, 3600.0]),
        )
        .unwrap();
        REGISTRY
            .register(Box::new(websocket_connection_duration.clone()))
            .unwrap();

        let websocket_messages_received = CounterVec::new(
            Opts::new(
                "ngit_websocket_messages_received_total",
                "WebSocket messages received by type",
            ),
            &["type"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(websocket_messages_received.clone()))
            .unwrap();

        let websocket_messages_sent = CounterVec::new(
            Opts::new(
                "ngit_websocket_messages_sent_total",
                "WebSocket messages sent by type",
            ),
            &["type"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(websocket_messages_sent.clone()))
            .unwrap();

        // Git operation metrics
        let git_operations_total = CounterVec::new(
            Opts::new(
                "ngit_git_operations_total",
                "Git operations by type and status",
            ),
            &["operation", "status"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(git_operations_total.clone()))
            .unwrap();

        let git_operation_duration = HistogramVec::new(
            HistogramOpts::new(
                "ngit_git_operation_duration_seconds",
                "Duration of git operations",
            )
            .buckets(vec![0.1, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0, 60.0]),
            &["operation"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(git_operation_duration.clone()))
            .unwrap();

        let git_bytes_total = CounterVec::new(
            Opts::new(
                "ngit_git_bytes_total",
                "Total bytes transferred for git operations",
            ),
            &["direction"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(git_bytes_total.clone()))
            .unwrap();

        let git_push_authorization = CounterVec::new(
            Opts::new(
                "ngit_git_push_authorization_total",
                "Push authorization results",
            ),
            &["result"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(git_push_authorization.clone()))
            .unwrap();

        // Nostr event metrics
        let events_received_total = CounterVec::new(
            Opts::new(
                "ngit_events_received_total",
                "Nostr events received by kind",
            ),
            &["kind"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(events_received_total.clone()))
            .unwrap();

        let events_stored_total = CounterVec::new(
            Opts::new(
                "ngit_events_stored_total",
                "Nostr events successfully stored by kind",
            ),
            &["kind"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(events_stored_total.clone()))
            .unwrap();

        let events_rejected_total = CounterVec::new(
            Opts::new(
                "ngit_events_rejected_total",
                "Nostr events rejected by kind and reason",
            ),
            &["kind", "reason"],
        )
        .unwrap();
        REGISTRY
            .register(Box::new(events_rejected_total.clone()))
            .unwrap();

        // Repository metrics
        let repositories_total = Gauge::with_opts(Opts::new(
            "ngit_repositories_total",
            "Total repositories hosted",
        ))
        .unwrap();
        REGISTRY
            .register(Box::new(repositories_total.clone()))
            .unwrap();

        // Build info
        let build_info = GaugeVec::new(
            Opts::new("ngit_build_info", "Build information"),
            &["version", "commit"],
        )
        .unwrap();
        REGISTRY.register(Box::new(build_info.clone())).unwrap();

        // Set build info gauge to 1 (it's just for labels)
        build_info
            .with_label_values(&[
                env!("CARGO_PKG_VERSION"),
                option_env!("GIT_HASH").unwrap_or("unknown"),
            ])
            .set(1.0);

        Self {
            connection_tracker,
            bandwidth_tracker,
            sync_metrics,
            websocket_connections_total,
            websocket_connection_duration,
            websocket_messages_received,
            websocket_messages_sent,
            git_operations_total,
            git_operation_duration,
            git_bytes_total,
            git_push_authorization,
            events_received_total,
            events_stored_total,
            events_rejected_total,
            repositories_total,
            git_data_path,
            start_time: Instant::now(),
            build_info,
        }
    }
}

/// Timer for tracking WebSocket connection duration.
/// Records the elapsed time when dropped.
pub struct HistogramTimer {
    histogram: Histogram,
    start: Instant,
}

impl HistogramTimer {
    fn new(histogram: Histogram) -> Self {
        Self {
            histogram,
            start: Instant::now(),
        }
    }
}

impl Drop for HistogramTimer {
    fn drop(&mut self) {
        let elapsed = self.start.elapsed().as_secs_f64();
        self.histogram.observe(elapsed);
    }
}

/// Timer for tracking Git operation duration.
/// Records the elapsed time when dropped.
pub struct GitOperationTimer {
    histogram_vec: HistogramVec,
    operation: String,
    start: Instant,
}

impl GitOperationTimer {
    fn new(histogram_vec: HistogramVec, operation: String) -> Self {
        Self {
            histogram_vec,
            operation,
            start: Instant::now(),
        }
    }
}

impl Drop for GitOperationTimer {
    fn drop(&mut self) {
        let elapsed = self.start.elapsed().as_secs_f64();
        self.histogram_vec
            .with_label_values(&[&self.operation])
            .observe(elapsed);
    }
}

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

    #[test]
    fn test_count_repositories_on_disk() {
        use std::fs;
        use tempfile::TempDir;

        // Create temporary directory structure
        let temp_dir = TempDir::new().unwrap();
        let git_data_path = temp_dir.path();

        // Initially should be 0
        let count = Metrics::count_repositories_on_disk(git_data_path.to_str().unwrap());
        assert_eq!(count, 0);

        // Create some fake repositories
        let npub1 = git_data_path.join("npub1test");
        fs::create_dir_all(&npub1).unwrap();
        fs::create_dir_all(npub1.join("repo1.git")).unwrap();
        fs::create_dir_all(npub1.join("repo2.git")).unwrap();

        let npub2 = git_data_path.join("npub2test");
        fs::create_dir_all(&npub2).unwrap();
        fs::create_dir_all(npub2.join("repo3.git")).unwrap();

        // Should count 3 repositories
        let count = Metrics::count_repositories_on_disk(git_data_path.to_str().unwrap());
        assert_eq!(count, 3);

        // Create a non-.git directory (should be ignored)
        fs::create_dir_all(npub1.join("not-a-repo")).unwrap();
        let count = Metrics::count_repositories_on_disk(git_data_path.to_str().unwrap());
        assert_eq!(count, 3);

        // Create a file with .git suffix (should be ignored, not a directory)
        fs::write(npub1.join("file.git"), "content").unwrap();
        let count = Metrics::count_repositories_on_disk(git_data_path.to_str().unwrap());
        assert_eq!(count, 3);
    }

    /// Comprehensive test for Metrics functionality including repository counting.
    ///
    /// NOTE: This test creates a Metrics instance which registers with the global
    /// Prometheus REGISTRY. Due to this global state, we cannot have multiple tests
    /// that create Metrics instances - they would conflict. Therefore, this single
    /// test covers:
    /// 1. Metrics creation and basic operations
    /// 2. Repository counting on disk via render()
    ///
    /// If additional Metrics tests are needed, they should either be added to this
    /// test or use a separate test-specific Prometheus registry.
    #[test]
    fn test_metrics_with_repository_counting() {
        use std::fs;
        use tempfile::TempDir;

        // Create temporary directory structure for repository counting
        let temp_dir = TempDir::new().unwrap();
        let git_data_path = temp_dir.path();

        // Create Metrics with git_data_path for repository counting
        let metrics = Metrics::new(10, Some(git_data_path.to_str().unwrap().to_string()));

        // Test basic metrics operations
        metrics.record_websocket_connection();
        metrics.record_message_received("REQ");
        metrics.record_message_sent("EVENT");
        metrics.record_git_operation("clone", "success");
        metrics.record_git_bytes("in", 1024);
        metrics.record_event_received(1);
        metrics.record_event_stored(1);
        metrics.record_event_rejected(1, "invalid_signature");
        metrics.set_repositories_total(5);

        // Test repository counting via render()
        // Render should count 0 repos initially (even though we set it to 5 above,
        // render() recounts from disk)
        let output = metrics.render();
        assert!(output.contains("ngit_repositories_total 0"));

        // Create some repositories
        let npub1 = git_data_path.join("npub1test");
        fs::create_dir_all(&npub1).unwrap();
        fs::create_dir_all(npub1.join("repo1.git")).unwrap();
        fs::create_dir_all(npub1.join("repo2.git")).unwrap();

        // Render should count 2 repos now
        let output = metrics.render();
        assert!(output.contains("ngit_repositories_total 2"));

        // Add another repo
        fs::create_dir_all(npub1.join("repo3.git")).unwrap();

        // Render should count 3 repos
        let output = metrics.render();
        assert!(output.contains("ngit_repositories_total 3"));
    }
}