iroh_gossip/
metrics.rs

1//! Metrics for iroh-gossip
2
3use iroh_metrics::{Counter, MetricsGroup};
4use serde::Serialize;
5
6use crate::proto::{
7    self,
8    state::MessageKind,
9    topic::{InEvent, OutEvent},
10};
11
12/// Enum of metrics for the module
13#[derive(Debug, Default, MetricsGroup)]
14#[metrics(name = "gossip")]
15pub struct Metrics {
16    /// Number of control messages sent
17    pub msgs_ctrl_sent: Counter,
18    /// Number of control messages received
19    pub msgs_ctrl_recv: Counter,
20    /// Number of data messages sent
21    pub msgs_data_sent: Counter,
22    /// Number of data messages received
23    pub msgs_data_recv: Counter,
24    /// Total size of all data messages sent
25    pub msgs_data_sent_size: Counter,
26    /// Total size of all data messages received
27    pub msgs_data_recv_size: Counter,
28    /// Total size of all control messages sent
29    pub msgs_ctrl_sent_size: Counter,
30    /// Total size of all control messages received
31    pub msgs_ctrl_recv_size: Counter,
32    /// Number of times we connected to a peer
33    pub neighbor_up: Counter,
34    /// Number of times we disconnected from a peer
35    pub neighbor_down: Counter,
36    /// Number of messages we broadcasted to all nodes
37    pub msgs_broadcast_swarm: Counter,
38    /// Number of messages we broadcasted to direct neighbors
39    pub msgs_broadcast_neighbors: Counter,
40    /// Number of topcis we joined.
41    pub topics_joined: Counter,
42    /// Number of topcis we left.
43    pub topics_quit: Counter,
44    /// Number of times we successfully dialed a remote node.
45    pub peers_dialed_success: Counter,
46    /// Number of times we failed to dial a remote node.
47    pub peers_dialed_failure: Counter,
48    /// Number of times we accepted a connection from a remote node.
49    pub peers_accepted: Counter,
50    /// Number of times the main actor loop ticked
51    pub actor_tick_main: Counter,
52}
53
54impl Metrics {
55    /// Track an [`InEvent`].
56    pub fn track_in_event<PI: Serialize>(&self, in_event: &InEvent<PI>) {
57        match in_event {
58            InEvent::RecvMessage(_, message) => match message.kind() {
59                MessageKind::Data => {
60                    self.msgs_data_recv.inc();
61                    self.msgs_data_recv_size
62                        .inc_by(message.size().unwrap_or(0) as u64);
63                }
64                MessageKind::Control => {
65                    self.msgs_ctrl_recv.inc();
66                    self.msgs_ctrl_recv_size
67                        .inc_by(message.size().unwrap_or(0) as u64);
68                }
69            },
70            InEvent::Command(cmd) => match cmd {
71                proto::Command::Broadcast(_, scope) => match scope {
72                    proto::Scope::Swarm => inc(&self.msgs_broadcast_swarm),
73                    proto::Scope::Neighbors => inc(&self.msgs_broadcast_neighbors),
74                },
75                proto::Command::Join(_) => {}
76                proto::Command::Quit => {}
77            },
78            InEvent::TimerExpired(_) => {}
79            InEvent::PeerDisconnected(_) => {}
80            InEvent::UpdatePeerData(_) => {}
81        }
82    }
83
84    /// Track an [`OutEvent`].
85    pub fn track_out_event<PI: Serialize>(&self, out_event: &OutEvent<PI>) {
86        match out_event {
87            OutEvent::SendMessage(_to, message) => match message.kind() {
88                MessageKind::Data => {
89                    self.msgs_data_sent.inc();
90                    self.msgs_data_sent_size
91                        .inc_by(message.size().unwrap_or(0) as u64);
92                }
93                MessageKind::Control => {
94                    self.msgs_ctrl_sent.inc();
95                    self.msgs_ctrl_sent_size
96                        .inc_by(message.size().unwrap_or(0) as u64);
97                }
98            },
99            OutEvent::EmitEvent(event) => match event {
100                proto::Event::NeighborUp(_peer) => inc(&self.neighbor_up),
101                proto::Event::NeighborDown(_peer) => inc(&self.neighbor_down),
102                _ => {}
103            },
104            _ => {}
105        }
106    }
107}
108
109pub(crate) fn inc(counter: &Counter) {
110    counter.inc();
111}