Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 4c03656

Browse files
authored
client/network/service: Add primary dimension to connection metrics (#6472)
* client/network/service: Add primary dimension to connection metrics Two nodes can be interconnected via one or more connections. The first of those connections is called the primary connection. This commit adds another dimension to the `sub_libp2p_connections_{closed,opened}_total` metrics to differentiate primary and non-primary connections being opened / closed. By intuition more than one connection between two nodes is rare. Tracking the fact whether a connection is primary or not will help prove or disprove this intuition. * .maintain/monitoring: Ensure to sum over all connections_closed variants * client/network/service: Rename is_primary to is_first * client/network/service: Split by metric name with two additional metrics * Revert ".maintain/monitoring: Ensure to sum over all connections_closed variants" This reverts commit 2d2f93e. * client/network/service: Remove labels from distinct metrics
1 parent 034055a commit 4c03656

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

client/network/src/service.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ struct Metrics {
854854
// This list is ordered alphabetically
855855
connections_closed_total: CounterVec<U64>,
856856
connections_opened_total: CounterVec<U64>,
857+
distinct_peers_connections_closed_total: Counter<U64>,
858+
distinct_peers_connections_opened_total: Counter<U64>,
857859
import_queue_blocks_submitted: Counter<U64>,
858860
import_queue_finality_proofs_submitted: Counter<U64>,
859861
import_queue_justifications_submitted: Counter<U64>,
@@ -889,17 +891,25 @@ impl Metrics {
889891
connections_closed_total: register(CounterVec::new(
890892
Opts::new(
891893
"sub_libp2p_connections_closed_total",
892-
"Total number of connections closed, by reason and direction"
894+
"Total number of connections closed, by direction and reason"
893895
),
894896
&["direction", "reason"]
895897
)?, registry)?,
896898
connections_opened_total: register(CounterVec::new(
897899
Opts::new(
898900
"sub_libp2p_connections_opened_total",
899-
"Total number of connections opened"
901+
"Total number of connections opened by direction"
900902
),
901903
&["direction"]
902904
)?, registry)?,
905+
distinct_peers_connections_closed_total: register(Counter::new(
906+
"sub_libp2p_distinct_peers_connections_closed_total",
907+
"Total number of connections closed with distinct peers"
908+
)?, registry)?,
909+
distinct_peers_connections_opened_total: register(Counter::new(
910+
"sub_libp2p_distinct_peers_connections_opened_total",
911+
"Total number of connections opened with distinct peers"
912+
)?, registry)?,
903913
import_queue_blocks_submitted: register(Counter::new(
904914
"import_queue_blocks_submitted",
905915
"Number of blocks submitted to the import queue.",
@@ -1214,40 +1224,44 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
12141224
}
12151225
this.event_streams.send(ev);
12161226
},
1217-
Poll::Ready(SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. }) => {
1227+
Poll::Ready(SwarmEvent::ConnectionEstablished { peer_id, endpoint, num_established }) => {
12181228
trace!(target: "sub-libp2p", "Libp2p => Connected({:?})", peer_id);
1229+
12191230
if let Some(metrics) = this.metrics.as_ref() {
1220-
match endpoint {
1221-
ConnectedPoint::Dialer { .. } =>
1222-
metrics.connections_opened_total.with_label_values(&["out"]).inc(),
1223-
ConnectedPoint::Listener { .. } =>
1224-
metrics.connections_opened_total.with_label_values(&["in"]).inc(),
1231+
let direction = match endpoint {
1232+
ConnectedPoint::Dialer { .. } => "out",
1233+
ConnectedPoint::Listener { .. } => "in",
1234+
};
1235+
metrics.connections_opened_total.with_label_values(&[direction]).inc();
1236+
1237+
if num_established.get() == 1 {
1238+
metrics.distinct_peers_connections_opened_total.inc();
12251239
}
12261240
}
12271241
},
1228-
Poll::Ready(SwarmEvent::ConnectionClosed { peer_id, cause, endpoint, .. }) => {
1242+
Poll::Ready(SwarmEvent::ConnectionClosed { peer_id, cause, endpoint, num_established }) => {
12291243
trace!(target: "sub-libp2p", "Libp2p => Disconnected({:?}, {:?})", peer_id, cause);
12301244
if let Some(metrics) = this.metrics.as_ref() {
1231-
let dir = match endpoint {
1245+
let direction = match endpoint {
12321246
ConnectedPoint::Dialer { .. } => "out",
12331247
ConnectedPoint::Listener { .. } => "in",
12341248
};
1235-
1236-
match cause {
1237-
ConnectionError::IO(_) =>
1238-
metrics.connections_closed_total.with_label_values(&[dir, "transport-error"]).inc(),
1249+
let reason = match cause {
1250+
ConnectionError::IO(_) => "transport-error",
12391251
ConnectionError::Handler(NodeHandlerWrapperError::Handler(EitherError::A(EitherError::A(
12401252
EitherError::A(EitherError::A(EitherError::B(
1241-
EitherError::A(PingFailure::Timeout)))))))) =>
1242-
metrics.connections_closed_total.with_label_values(&[dir, "ping-timeout"]).inc(),
1253+
EitherError::A(PingFailure::Timeout)))))))) => "ping-timeout",
12431254
ConnectionError::Handler(NodeHandlerWrapperError::Handler(EitherError::A(EitherError::A(
12441255
EitherError::A(EitherError::A(EitherError::A(
1245-
EitherError::B(LegacyConnectionKillError)))))))) =>
1246-
metrics.connections_closed_total.with_label_values(&[dir, "force-closed"]).inc(),
1247-
ConnectionError::Handler(NodeHandlerWrapperError::Handler(_)) =>
1248-
metrics.connections_closed_total.with_label_values(&[dir, "protocol-error"]).inc(),
1249-
ConnectionError::Handler(NodeHandlerWrapperError::KeepAliveTimeout) =>
1250-
metrics.connections_closed_total.with_label_values(&[dir, "keep-alive-timeout"]).inc(),
1256+
EitherError::B(LegacyConnectionKillError)))))))) => "force-closed",
1257+
ConnectionError::Handler(NodeHandlerWrapperError::Handler(_)) => "protocol-error",
1258+
ConnectionError::Handler(NodeHandlerWrapperError::KeepAliveTimeout) => "keep-alive-timeout",
1259+
};
1260+
metrics.connections_closed_total.with_label_values(&[direction, reason]).inc();
1261+
1262+
// `num_established` represents the number of *remaining* connections.
1263+
if num_established == 0 {
1264+
metrics.distinct_peers_connections_closed_total.inc();
12511265
}
12521266
}
12531267
},

0 commit comments

Comments
 (0)