fuel_core_metrics/
p2p_metrics.rs1use crate::global_registry;
2use prometheus_client::metrics::{
3 counter::Counter,
4 gauge::Gauge,
5};
6use std::sync::OnceLock;
7
8pub struct P2PMetrics {
9 pub unique_peers: Counter,
10 pub blocks_requested: Gauge,
11 pub p2p_req_res_cache_hits: Counter,
12 pub p2p_req_res_cache_misses: Counter,
13}
14
15impl P2PMetrics {
16 fn new() -> Self {
17 let unique_peers = Counter::default();
18 let blocks_requested = Gauge::default();
19 let p2p_req_res_cache_hits = Counter::default();
20 let p2p_req_res_cache_misses = Counter::default();
21
22 let metrics = P2PMetrics {
23 unique_peers,
24 blocks_requested,
25 p2p_req_res_cache_hits,
26 p2p_req_res_cache_misses,
27 };
28
29 let mut registry = global_registry().registry.lock();
30 registry.register(
31 "Peer_Counter",
32 "A Counter which keeps track of each unique peer the p2p service has connected to",
33 metrics.unique_peers.clone(),
34 );
35
36 registry.register(
37 "Blocks_Requested",
38 "A Gauge which keeps track of how many blocks were requested and served over the p2p req/res protocol",
39 metrics.blocks_requested.clone()
40 );
41
42 registry.register(
43 "P2p_Req_Res_Cache_Hits",
44 "A Counter which keeps track of the number of cache hits for the p2p req/res protocol",
45 metrics.p2p_req_res_cache_hits.clone()
46 );
47
48 registry.register(
49 "P2p_Req_Res_Cache_Misses",
50 "A Counter which keeps track of the number of cache misses for the p2p req/res protocol",
51 metrics.p2p_req_res_cache_misses.clone()
52 );
53
54 metrics
55 }
56}
57
58static P2P_METRICS: OnceLock<P2PMetrics> = OnceLock::new();
59
60pub fn p2p_metrics() -> &'static P2PMetrics {
61 P2P_METRICS.get_or_init(P2PMetrics::new)
62}
63
64pub fn increment_unique_peers() {
65 p2p_metrics().unique_peers.inc();
66}
67
68pub fn set_blocks_requested(count: usize) {
69 p2p_metrics().blocks_requested.set(count as i64);
70}
71
72pub fn increment_p2p_req_res_cache_hits() {
73 p2p_metrics().p2p_req_res_cache_hits.inc();
74}
75
76pub fn increment_p2p_req_res_cache_misses() {
77 p2p_metrics().p2p_req_res_cache_misses.inc();
78}