11//! Metrics for iroh-gossip
22
33use iroh_metrics:: { Counter , MetricsGroup } ;
4+ use serde:: Serialize ;
5+
6+ use crate :: proto:: {
7+ self ,
8+ state:: MessageKind ,
9+ topic:: { InEvent , OutEvent } ,
10+ } ;
411
512/// Enum of metrics for the module
613#[ derive( Debug , Default , MetricsGroup ) ]
@@ -26,20 +33,79 @@ pub struct Metrics {
2633 pub neighbor_up : Counter ,
2734 /// Number of times we disconnected from a peer
2835 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 ,
2950 /// Number of times the main actor loop ticked
3051 pub actor_tick_main : Counter ,
31- /// Number of times the actor ticked for a message received
32- pub actor_tick_rx : Counter ,
33- /// Number of times the actor ticked for an endpoint event
34- pub actor_tick_endpoint : Counter ,
35- /// Number of times the actor ticked for a dialer event
36- pub actor_tick_dialer : Counter ,
37- /// Number of times the actor ticked for a successful dialer event
38- pub actor_tick_dialer_success : Counter ,
39- /// Number of times the actor ticked for a failed dialer event
40- pub actor_tick_dialer_failure : Counter ,
41- /// Number of times the actor ticked for an incoming event
42- pub actor_tick_in_event_rx : Counter ,
43- /// Number of times the actor ticked for a timer event
44- pub actor_tick_timers : Counter ,
52+ }
53+
54+ impl 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+
109+ pub ( crate ) fn inc ( counter : & Counter ) {
110+ counter. inc ( ) ;
45111}
0 commit comments