@@ -53,6 +53,7 @@ use parking_lot::Mutex;
5353use prometheus_endpoint:: {
5454 register, Counter , CounterVec , Gauge , GaugeVec , Histogram , HistogramOpts , HistogramVec , Opts ,
5555 PrometheusError , Registry , U64 ,
56+ SourcedCounter , MetricSource
5657} ;
5758use sc_peerset:: PeersetHandle ;
5859use sp_consensus:: import_queue:: { BlockImportError , BlockImportResult , ImportQueue , Link } ;
@@ -240,12 +241,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
240241 local_peer_id_legacy
241242 ) ;
242243
243- // Initialize the metrics.
244- let metrics = match & params. metrics_registry {
245- Some ( registry) => Some ( Metrics :: register ( & registry) ?) ,
246- None => None
247- } ;
248-
249244 let checker = params. on_demand . as_ref ( )
250245 . map ( |od| od. checker ( ) . clone ( ) )
251246 . unwrap_or_else ( || Arc :: new ( AlwaysBadChecker ) ) ;
@@ -353,6 +348,17 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
353348 ( builder. build ( ) , bandwidth)
354349 } ;
355350
351+ // Initialize the metrics.
352+ let metrics = match & params. metrics_registry {
353+ Some ( registry) => {
354+ // Sourced metrics.
355+ BandwidthCounters :: register ( registry, bandwidth. clone ( ) ) ?;
356+ // Other (i.e. new) metrics.
357+ Some ( Metrics :: register ( registry) ?)
358+ }
359+ None => None
360+ } ;
361+
356362 // Listen on multiaddresses.
357363 for addr in & params. network_config . listen_addresses {
358364 if let Err ( err) = Swarm :: < B , H > :: listen_on ( & mut swarm, addr. clone ( ) ) {
@@ -1152,9 +1158,6 @@ struct Metrics {
11521158 kbuckets_num_nodes : GaugeVec < U64 > ,
11531159 listeners_local_addresses : Gauge < U64 > ,
11541160 listeners_errors_total : Counter < U64 > ,
1155- // Note: `network_bytes_total` is a monotonic gauge obtained by
1156- // sampling an existing counter.
1157- network_bytes_total : GaugeVec < U64 > ,
11581161 notifications_sizes : HistogramVec ,
11591162 notifications_streams_closed_total : CounterVec < U64 > ,
11601163 notifications_streams_opened_total : CounterVec < U64 > ,
@@ -1168,6 +1171,35 @@ struct Metrics {
11681171 requests_out_started_total : CounterVec < U64 > ,
11691172}
11701173
1174+ /// The source for bandwidth metrics.
1175+ #[ derive( Clone ) ]
1176+ struct BandwidthCounters ( Arc < transport:: BandwidthSinks > ) ;
1177+
1178+ impl BandwidthCounters {
1179+ fn register ( registry : & Registry , sinks : Arc < transport:: BandwidthSinks > )
1180+ -> Result < ( ) , PrometheusError >
1181+ {
1182+ register ( SourcedCounter :: new (
1183+ & Opts :: new (
1184+ "sub_libp2p_network_bytes_total" ,
1185+ "Total bandwidth usage"
1186+ ) . variable_label ( "direction" ) ,
1187+ BandwidthCounters ( sinks) ,
1188+ ) ?, registry) ?;
1189+
1190+ Ok ( ( ) )
1191+ }
1192+ }
1193+
1194+ impl MetricSource for BandwidthCounters {
1195+ type N = u64 ;
1196+
1197+ fn collect ( & self , mut set : impl FnMut ( & [ & str ] , Self :: N ) ) {
1198+ set ( & [ & "in" ] , self . 0 . total_inbound ( ) ) ;
1199+ set ( & [ & "out" ] , self . 0 . total_outbound ( ) ) ;
1200+ }
1201+ }
1202+
11711203impl Metrics {
11721204 fn register ( registry : & Registry ) -> Result < Self , PrometheusError > {
11731205 Ok ( Self {
@@ -1271,13 +1303,6 @@ impl Metrics {
12711303 "sub_libp2p_listeners_errors_total" ,
12721304 "Total number of non-fatal errors reported by a listener"
12731305 ) ?, registry) ?,
1274- network_bytes_total : register ( GaugeVec :: new (
1275- Opts :: new (
1276- "sub_libp2p_network_bytes_total" ,
1277- "Total bandwidth usage"
1278- ) ,
1279- & [ "direction" ]
1280- ) ?, registry) ?,
12811306 notifications_sizes : register ( HistogramVec :: new (
12821307 HistogramOpts {
12831308 common_opts : Opts :: new (
@@ -1725,8 +1750,6 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
17251750 this. is_major_syncing . store ( is_major_syncing, Ordering :: Relaxed ) ;
17261751
17271752 if let Some ( metrics) = this. metrics . as_ref ( ) {
1728- metrics. network_bytes_total . with_label_values ( & [ "in" ] ) . set ( this. service . bandwidth . total_inbound ( ) ) ;
1729- metrics. network_bytes_total . with_label_values ( & [ "out" ] ) . set ( this. service . bandwidth . total_outbound ( ) ) ;
17301753 metrics. is_major_syncing . set ( is_major_syncing as u64 ) ;
17311754 for ( proto, num_entries) in this. network_service . num_kbuckets_entries ( ) {
17321755 let proto = maybe_utf8_bytes_to_string ( proto. as_bytes ( ) ) ;
0 commit comments