Skip to content

Commit e37a388

Browse files
authored
Merge of #5404
2 parents eab3672 + 8ceb702 commit e37a388

File tree

2 files changed

+75
-68
lines changed

2 files changed

+75
-68
lines changed

beacon_node/lighthouse_network/src/peer_manager/mod.rs

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ pub use libp2p::identity::Keypair;
2626
#[allow(clippy::mutable_key_type)] // PeerId in hashmaps are no longer permitted by clippy
2727
pub mod peerdb;
2828

29+
use crate::peer_manager::peerdb::client::ClientKind;
30+
use libp2p::multiaddr;
2931
pub use peerdb::peer_info::{
3032
ConnectionDirection, PeerConnectionStatus, PeerConnectionStatus::*, PeerInfo,
3133
};
3234
use peerdb::score::{PeerAction, ReportSource};
3335
pub use peerdb::sync_status::{SyncInfo, SyncStatus};
3436
use std::collections::{hash_map::Entry, HashMap};
3537
use std::net::IpAddr;
38+
use strum::IntoEnumIterator;
39+
3640
pub mod config;
3741
mod network_behaviour;
3842

@@ -464,19 +468,6 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
464468
"observed_address" => ?info.observed_addr,
465469
"protocols" => ?info.protocols
466470
);
467-
468-
// update the peer client kind metric if the peer is connected
469-
if matches!(
470-
peer_info.connection_status(),
471-
PeerConnectionStatus::Connected { .. }
472-
| PeerConnectionStatus::Disconnecting { .. }
473-
) {
474-
metrics::inc_gauge_vec(
475-
&metrics::PEERS_PER_CLIENT,
476-
&[peer_info.client().kind.as_ref()],
477-
);
478-
metrics::dec_gauge_vec(&metrics::PEERS_PER_CLIENT, &[previous_kind.as_ref()]);
479-
}
480471
}
481472
} else {
482473
error!(self.log, "Received an Identify response from an unknown peer"; "peer_id" => peer_id.to_string());
@@ -812,11 +803,8 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
812803
// start a ping and status timer for the peer
813804
self.status_peers.insert(*peer_id);
814805

815-
let connected_peers = self.network_globals.connected_peers() as i64;
816-
817806
// increment prometheus metrics
818807
metrics::inc_counter(&metrics::PEER_CONNECT_EVENT_COUNT);
819-
metrics::set_gauge(&metrics::PEERS_CONNECTED, connected_peers);
820808

821809
true
822810
}
@@ -1267,6 +1255,70 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
12671255
);
12681256
}
12691257
}
1258+
1259+
// Update peer count related metrics.
1260+
fn update_peer_count_metrics(&self) {
1261+
let mut peers_connected = 0;
1262+
let mut clients_per_peer = HashMap::new();
1263+
let mut peers_connected_mutli: HashMap<(&str, &str), i32> = HashMap::new();
1264+
1265+
for (_, peer_info) in self.network_globals.peers.read().connected_peers() {
1266+
peers_connected += 1;
1267+
1268+
*clients_per_peer
1269+
.entry(peer_info.client().kind.to_string())
1270+
.or_default() += 1;
1271+
1272+
let direction = match peer_info.connection_direction() {
1273+
Some(ConnectionDirection::Incoming) => "inbound",
1274+
Some(ConnectionDirection::Outgoing) => "outbound",
1275+
None => "none",
1276+
};
1277+
// Note: the `transport` is set to `unknown` if the `listening_addresses` list is empty.
1278+
// This situation occurs when the peer is initially registered in PeerDB, but the peer
1279+
// info has not yet been updated at `PeerManager::identify`.
1280+
let transport = peer_info
1281+
.listening_addresses()
1282+
.iter()
1283+
.find_map(|addr| {
1284+
addr.iter().find_map(|proto| match proto {
1285+
multiaddr::Protocol::QuicV1 => Some("quic"),
1286+
multiaddr::Protocol::Tcp(_) => Some("tcp"),
1287+
_ => None,
1288+
})
1289+
})
1290+
.unwrap_or("unknown");
1291+
*peers_connected_mutli
1292+
.entry((direction, transport))
1293+
.or_default() += 1;
1294+
}
1295+
1296+
// PEERS_CONNECTED
1297+
metrics::set_gauge(&metrics::PEERS_CONNECTED, peers_connected);
1298+
1299+
// PEERS_PER_CLIENT
1300+
for client_kind in ClientKind::iter() {
1301+
let value = clients_per_peer.get(&client_kind.to_string()).unwrap_or(&0);
1302+
metrics::set_gauge_vec(
1303+
&metrics::PEERS_PER_CLIENT,
1304+
&[client_kind.as_ref()],
1305+
*value as i64,
1306+
);
1307+
}
1308+
1309+
// PEERS_CONNECTED_MULTI
1310+
for direction in ["inbound", "outbound", "none"] {
1311+
for transport in ["quic", "tcp", "unknown"] {
1312+
metrics::set_gauge_vec(
1313+
&metrics::PEERS_CONNECTED_MULTI,
1314+
&[direction, transport],
1315+
*peers_connected_mutli
1316+
.get(&(direction, transport))
1317+
.unwrap_or(&0) as i64,
1318+
);
1319+
}
1320+
}
1321+
}
12701322
}
12711323

12721324
enum ConnectingType {

beacon_node/lighthouse_network/src/peer_manager/network_behaviour.rs

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::net::IpAddr;
44
use std::task::{Context, Poll};
55

66
use futures::StreamExt;
7-
use libp2p::core::{multiaddr, ConnectedPoint};
7+
use libp2p::core::ConnectedPoint;
88
use libp2p::identity::PeerId;
99
use libp2p::swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
1010
use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
@@ -243,35 +243,11 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
243243
self.events.push(PeerManagerEvent::MetaData(peer_id));
244244
}
245245

246-
// increment prometheus metrics
246+
// Update the prometheus metrics
247247
if self.metrics_enabled {
248-
let remote_addr = endpoint.get_remote_address();
249-
let direction = if endpoint.is_dialer() {
250-
"outbound"
251-
} else {
252-
"inbound"
253-
};
254-
255-
match remote_addr.iter().find(|proto| {
256-
matches!(
257-
proto,
258-
multiaddr::Protocol::QuicV1 | multiaddr::Protocol::Tcp(_)
259-
)
260-
}) {
261-
Some(multiaddr::Protocol::QuicV1) => {
262-
metrics::inc_gauge_vec(&metrics::PEERS_CONNECTED_MULTI, &[direction, "quic"]);
263-
}
264-
Some(multiaddr::Protocol::Tcp(_)) => {
265-
metrics::inc_gauge_vec(&metrics::PEERS_CONNECTED_MULTI, &[direction, "tcp"]);
266-
}
267-
Some(_) => unreachable!(),
268-
None => {
269-
error!(self.log, "Connection established via unknown transport"; "addr" => %remote_addr)
270-
}
271-
};
272-
273-
metrics::inc_gauge(&metrics::PEERS_CONNECTED);
274248
metrics::inc_counter(&metrics::PEER_CONNECT_EVENT_COUNT);
249+
250+
self.update_peer_count_metrics();
275251
}
276252

277253
// Count dialing peers in the limit if the peer dialed us.
@@ -309,7 +285,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
309285
fn on_connection_closed(
310286
&mut self,
311287
peer_id: PeerId,
312-
endpoint: &ConnectedPoint,
288+
_endpoint: &ConnectedPoint,
313289
remaining_established: usize,
314290
) {
315291
if remaining_established > 0 {
@@ -337,33 +313,12 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
337313
// reference so that peer manager can track this peer.
338314
self.inject_disconnect(&peer_id);
339315

340-
let remote_addr = endpoint.get_remote_address();
341316
// Update the prometheus metrics
342317
if self.metrics_enabled {
343-
let direction = if endpoint.is_dialer() {
344-
"outbound"
345-
} else {
346-
"inbound"
347-
};
348-
349-
match remote_addr.iter().find(|proto| {
350-
matches!(
351-
proto,
352-
multiaddr::Protocol::QuicV1 | multiaddr::Protocol::Tcp(_)
353-
)
354-
}) {
355-
Some(multiaddr::Protocol::QuicV1) => {
356-
metrics::dec_gauge_vec(&metrics::PEERS_CONNECTED_MULTI, &[direction, "quic"]);
357-
}
358-
Some(multiaddr::Protocol::Tcp(_)) => {
359-
metrics::dec_gauge_vec(&metrics::PEERS_CONNECTED_MULTI, &[direction, "tcp"]);
360-
}
361-
// If it's an unknown protocol we already logged when connection was established.
362-
_ => {}
363-
};
364318
// Legacy standard metrics.
365-
metrics::dec_gauge(&metrics::PEERS_CONNECTED);
366319
metrics::inc_counter(&metrics::PEER_DISCONNECT_EVENT_COUNT);
320+
321+
self.update_peer_count_metrics();
367322
}
368323
}
369324

0 commit comments

Comments
 (0)