Skip to content

Commit 9e5da37

Browse files
Count events per source and name in metrics
1 parent fe6dd8e commit 9e5da37

File tree

10 files changed

+118
-22
lines changed

10 files changed

+118
-22
lines changed

bootstrap/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ impl ClientState {
170170
select! {
171171
Some(p2p_event) = p2p_receiver.recv() => {
172172
match p2p_event {
173-
P2pEvent::Count => {
174-
self.metrics.count(MetricCounter::EventLoopEvent);
173+
P2pEvent::Count { source, name } => {
174+
self.metrics.count(MetricCounter::EventLoopEvent { source, name });
175175
},
176176
P2pEvent::IncomingGetRecord => {
177177
self.metrics.count(MetricCounter::IncomingGetRecord);

client/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,8 @@ impl ClientState {
732732
select! {
733733
Some(p2p_event) = p2p_receiver.recv() => {
734734
match p2p_event {
735-
P2pEvent::Count => {
736-
self.metrics.count(MetricCounter::EventLoopEvent);
735+
P2pEvent::Count { source, name } => {
736+
self.metrics.count(MetricCounter::EventLoopEvent { source, name });
737737
},
738738
P2pEvent::IncomingGetRecord => {
739739
self.metrics.count(MetricCounter::IncomingGetRecord);

core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [1.2.13]
44

5+
- Added event source and name to event count metric
56
- Trigger maintenance on empty blocks
67

78
## [1.2.12](https://github.com/availproject/avail-light/tree/avail-light-core-v1.2.12) - 2025-10-21

core/src/network/p2p.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ pub enum OutputEvent {
102102
ExternalMultiaddressUpdate(Multiaddr),
103103
EstablishedConnection,
104104
OutgoingConnectionError,
105-
Count,
105+
Count {
106+
source: &'static str,
107+
name: &'static str,
108+
},
106109
PutRecord {
107110
block_num: u32,
108111
records: Vec<Record>,

core/src/network/p2p/event_loop.rs

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,68 @@ use crate::{
3737
types::TimeToLive,
3838
};
3939

40+
trait SwarmEventExt {
41+
fn get_event_type(&self) -> (&'static str, &'static str);
42+
}
43+
44+
impl SwarmEventExt for SwarmEvent<ConfigurableBehaviourEvent> {
45+
fn get_event_type(&self) -> (&'static str, &'static str) {
46+
match self {
47+
SwarmEvent::Behaviour(ConfigurableBehaviourEvent::Kademlia(kad_event)) => {
48+
match kad_event {
49+
kad::Event::RoutingUpdated { .. } => ("Kademlia", "RoutingUpdated"),
50+
kad::Event::RoutablePeer { .. } => ("Kademlia", "RoutablePeer"),
51+
kad::Event::UnroutablePeer { .. } => ("Kademlia", "UnroutablePeer"),
52+
kad::Event::PendingRoutablePeer { .. } => ("Kademlia", "PendingRoutablePeer"),
53+
kad::Event::InboundRequest { request } => match request {
54+
InboundRequest::GetRecord { .. } => {
55+
("Kademlia", "InboundRequest::GetRecord")
56+
},
57+
InboundRequest::PutRecord { .. } => {
58+
("Kademlia", "InboundRequest::PutRecord")
59+
},
60+
InboundRequest::FindNode { .. } => ("Kademlia", "InboundRequest::FindNode"),
61+
_ => ("Kademlia", "InboundRequest::Other"),
62+
},
63+
kad::Event::ModeChanged { .. } => ("Kademlia", "ModeChanged"),
64+
kad::Event::OutboundQueryProgressed { result, .. } => match result {
65+
QueryResult::GetRecord(_) => ("Kademlia", "Query::GetRecord"),
66+
QueryResult::GetClosestPeers(_) => ("Kademlia", "Query::GetClosestPeers"),
67+
QueryResult::PutRecord(_) => ("Kademlia", "Query::PutRecord"),
68+
QueryResult::Bootstrap(_) => ("Kademlia", "Query::Bootstrap"),
69+
_ => ("Kademlia", "Query::Other"),
70+
},
71+
}
72+
},
73+
SwarmEvent::Behaviour(ConfigurableBehaviourEvent::Identify(identify_event)) => {
74+
match identify_event {
75+
identify::Event::Received { .. } => ("Identify", "Received"),
76+
identify::Event::Sent { .. } => ("Identify", "Sent"),
77+
identify::Event::Pushed { .. } => ("Identify", "Pushed"),
78+
identify::Event::Error { .. } => ("Identify", "Error"),
79+
}
80+
},
81+
SwarmEvent::Behaviour(ConfigurableBehaviourEvent::AutoNat(autonat_event)) => {
82+
match autonat_event {
83+
autonat::Event::InboundProbe(_) => ("AutoNat", "InboundProbe"),
84+
autonat::Event::OutboundProbe(_) => ("AutoNat", "OutboundProbe"),
85+
autonat::Event::StatusChanged { .. } => ("AutoNat", "StatusChanged"),
86+
}
87+
},
88+
SwarmEvent::Behaviour(ConfigurableBehaviourEvent::Ping(_)) => ("Ping", "Event"),
89+
SwarmEvent::NewListenAddr { .. } => ("Swarm", "NewListenAddr"),
90+
SwarmEvent::ConnectionClosed { .. } => ("Swarm", "ConnectionClosed"),
91+
SwarmEvent::IncomingConnection { .. } => ("Swarm", "IncomingConnection"),
92+
SwarmEvent::IncomingConnectionError { .. } => ("Swarm", "IncomingConnectionError"),
93+
SwarmEvent::ExternalAddrConfirmed { .. } => ("Swarm", "ExternalAddrConfirmed"),
94+
SwarmEvent::ConnectionEstablished { .. } => ("Swarm", "ConnectionEstablished"),
95+
SwarmEvent::OutgoingConnectionError { .. } => ("Swarm", "OutgoingConnectionError"),
96+
SwarmEvent::Dialing { .. } => ("Swarm", "Dialing"),
97+
_ => ("Swarm", "Other"),
98+
}
99+
}
100+
}
101+
40102
struct EventLoopConfig {
41103
// Used for checking protocol version
42104
is_fat_client: bool,
@@ -155,10 +217,11 @@ impl EventLoop {
155217
#[cfg(not(target_arch = "wasm32"))]
156218
tokio::select! {
157219
event = self.swarm.next() => {
158-
self.handle_event(event.expect("Swarm stream should be infinite")).await;
220+
let event = event.expect("Swarm stream should be infinite");
221+
let (source, name) = event.get_event_type();
222+
self.handle_event(event).await;
159223
event_counter.add_event();
160-
161-
_ = self.event_sender.send(OutputEvent::Count);
224+
_ = self.event_sender.send(OutputEvent::Count { source, name });
162225
},
163226
command = self.command_receiver.recv() => match command {
164227
Some(c) => _ = (c)(&mut self),
@@ -169,7 +232,11 @@ impl EventLoop {
169232
},
170233
},
171234
_ = report_timer.tick() => {
172-
debug!("Events per {}s: {:.2}", event_counter.duration_secs(), event_counter.count_events());
235+
info!(
236+
count = format!("{:.2}", event_counter.count_events()),
237+
"Events per {}s",
238+
event_counter.duration_secs()
239+
);
173240
event_counter.reset_counter();
174241
},
175242
// if the shutdown was triggered,
@@ -186,10 +253,11 @@ impl EventLoop {
186253
#[cfg(target_arch = "wasm32")]
187254
tokio::select! {
188255
event = self.swarm.next() => {
189-
self.handle_event(event.expect("Swarm stream should be infinite")).await;
256+
let event = event.expect("Swarm stream should be infinite");
257+
let (source, name) = event.get_event_type();
258+
self.handle_event(event).await;
190259
event_counter.add_event();
191-
192-
_ = self.event_sender.send(OutputEvent::Count);
260+
_ = self.event_sender.send(OutputEvent::Count { source, name });
193261
},
194262
command = self.command_receiver.recv() => match command {
195263
Some(c) => _ = (c)(&mut self),
@@ -200,7 +268,11 @@ impl EventLoop {
200268
},
201269
},
202270
_ = tokio::time::sleep(next_tick.checked_duration_since(now).unwrap_or_default()) => {
203-
debug!("Events per {}s: {:.2}", event_counter.duration_secs(), event_counter.count_events());
271+
info!(
272+
count = format!("{:.2}", event_counter.count_events()),
273+
"Events per {}s",
274+
event_counter.duration_secs()
275+
);
204276
event_counter.reset_counter();
205277
next_tick += event_counter.report_interval;
206278
},

core/src/network/p2p/memory_swarm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ mod tests {
641641
info!("Connection event detected: {:?}", event);
642642
connection_events.push(event);
643643
},
644-
OutputEvent::Count => {
644+
OutputEvent::Count { .. } => {
645645
// Count events are expected
646646
},
647647
other => {

core/src/telemetry/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ pub enum MetricCounter {
1919
EstablishedConnections,
2020
IncomingPutRecord,
2121
IncomingGetRecord,
22-
EventLoopEvent,
22+
EventLoopEvent {
23+
source: &'static str,
24+
name: &'static str,
25+
},
2326
DHTPutRecords,
2427
RpcConnected,
2528
RpcConnectionSwitched,
@@ -42,7 +45,7 @@ impl MetricName for MetricCounter {
4245
EstablishedConnections => "light.established_connections",
4346
IncomingPutRecord => "light.incoming_put_record",
4447
IncomingGetRecord => "light.incoming_get_record",
45-
EventLoopEvent => "light.event_loop_event",
48+
EventLoopEvent { .. } => "light.event_loop_event",
4649
DHTPutRecords => "light.dht.put.records",
4750
RpcConnected => "light.rpc.connected",
4851
RpcConnectionSwitched => "light.rpc.connection_switched",
@@ -62,7 +65,10 @@ impl MetricCounter {
6265
MetricCounter::EstablishedConnections,
6366
MetricCounter::IncomingPutRecord,
6467
MetricCounter::IncomingGetRecord,
65-
MetricCounter::EventLoopEvent,
68+
MetricCounter::EventLoopEvent {
69+
source: Default::default(),
70+
name: Default::default(),
71+
},
6672
MetricCounter::DHTPutRecords,
6773
MetricCounter::RpcConnected,
6874
MetricCounter::RpcConnectionSwitched,
@@ -78,6 +84,16 @@ impl MetricCounter {
7884
(_, _) => true,
7985
}
8086
}
87+
88+
/// Returns attributes specific to this counter
89+
pub fn attributes(&self) -> Vec<(&'static str, &'static str)> {
90+
match self {
91+
MetricCounter::EventLoopEvent { source, name } => {
92+
vec![("source", *source), ("name", *name)]
93+
},
94+
_ => vec![],
95+
}
96+
}
8197
}
8298

8399
#[derive(Clone, Debug)]

core/src/telemetry/otlp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ impl Metrics {
6363
if !counter.is_allowed(&self.origin) {
6464
return;
6565
}
66-
self.counters[&counter.name()].add(value, &self.attributes());
66+
let mut attrs = self.attributes();
67+
for (key, val) in counter.attributes() {
68+
attrs.push(KeyValue::new(key, val.to_string()));
69+
}
70+
self.counters[&counter.name()].add(value, &attrs);
6771
}
6872

6973
/// Puts metric to the metric buffer if it is allowed.

crawler/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ impl CrawlerState {
224224
select! {
225225
Some(p2p_event) = p2p_receiver.recv() => {
226226
match p2p_event {
227-
P2pEvent::Count => {
228-
self.metrics.count(MetricCounter::EventLoopEvent);
227+
P2pEvent::Count { source, name } => {
228+
self.metrics.count(MetricCounter::EventLoopEvent { source, name });
229229
},
230230
P2pEvent::Ping{ rtt, .. } => {
231231
self.metrics.record(MetricValue::DHTPingLatency(rtt.as_millis() as f64));

fat/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ impl FatState {
378378
select! {
379379
Some(p2p_event) = p2p_receiver.recv() => {
380380
match p2p_event {
381-
P2pEvent::Count => {
382-
self.metrics.count(MetricCounter::EventLoopEvent)
381+
P2pEvent::Count { source, name } => {
382+
self.metrics.count(MetricCounter::EventLoopEvent { source, name })
383383
},
384384
P2pEvent::IncomingGetRecord => {
385385
self.metrics.count(MetricCounter::IncomingGetRecord)

0 commit comments

Comments
 (0)