1use iroh_metrics::{Counter, MetricsGroup};
4use serde::Serialize;
5
6use crate::proto::{
7 self,
8 state::MessageKind,
9 topic::{InEvent, OutEvent},
10};
11
12#[derive(Debug, Default, MetricsGroup)]
14#[metrics(name = "gossip")]
15pub struct Metrics {
16 pub msgs_ctrl_sent: Counter,
18 pub msgs_ctrl_recv: Counter,
20 pub msgs_data_sent: Counter,
22 pub msgs_data_recv: Counter,
24 pub msgs_data_sent_size: Counter,
26 pub msgs_data_recv_size: Counter,
28 pub msgs_ctrl_sent_size: Counter,
30 pub msgs_ctrl_recv_size: Counter,
32 pub neighbor_up: Counter,
34 pub neighbor_down: Counter,
36 pub msgs_broadcast_swarm: Counter,
38 pub msgs_broadcast_neighbors: Counter,
40 pub topics_joined: Counter,
42 pub topics_quit: Counter,
44 pub peers_dialed_success: Counter,
46 pub peers_dialed_failure: Counter,
48 pub peers_accepted: Counter,
50 pub actor_tick_main: Counter,
52}
53
54impl Metrics {
55 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 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}