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
use lazy_static::lazy_static;
use once_cell::race::OnceBox;
use prometheus_client::{
    metrics::counter::Counter,
    registry::Registry,
};
use std::boxed::Box;

pub struct P2PMetrics {
    pub gossip_sub_registry: OnceBox<Registry>,
    // For descriptions of each Counter, see the `new` function where each Counter/Histogram is initialized
    pub peer_metrics: Registry,
    pub unique_peers: Counter,
}

impl P2PMetrics {
    fn new() -> Self {
        let peer_metrics = Registry::default();

        let unique_peers = Counter::default();

        let mut metrics = P2PMetrics {
            gossip_sub_registry: OnceBox::new(),
            peer_metrics,
            unique_peers,
        };

        metrics.peer_metrics.register(
            "Peer_Counter",
            "A Counter which keeps track of each unique peer the p2p service has connected to",
            Box::new(metrics.unique_peers.clone()),
        );

        metrics
    }
}

lazy_static! {
    pub static ref P2P_METRICS: P2PMetrics = P2PMetrics::new();
}