From f202c58bbbfb67477d4d4f013d6aa5138ef2b375 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <bkchr@users.noreply.github.com>
Date: Tue, 7 Apr 2020 19:32:21 +0200
Subject: [PATCH] Fix compilation of service metrics on Windows (#5548)

* Fix compilation of service metrics on Windows

* Fix browser node

* Ahh

* Remove duplicate code...
---
 substrate/client/service/src/metrics.rs | 85 +++++++++++++------------
 1 file changed, 44 insertions(+), 41 deletions(-)

diff --git a/substrate/client/service/src/metrics.rs b/substrate/client/service/src/metrics.rs
index 740a795edae..6b7c32c2d02 100644
--- a/substrate/client/service/src/metrics.rs
+++ b/substrate/client/service/src/metrics.rs
@@ -14,23 +14,22 @@
 // You should have received a copy of the GNU General Public License
 // along with Substrate.  If not, see <http://www.gnu.org/licenses/>.
 
+use std::convert::TryFrom;
+
 use crate::NetworkStatus;
 use prometheus_endpoint::{register, Gauge, U64, F64, Registry, PrometheusError, Opts, GaugeVec};
 use sc_client::ClientInfo;
 use sc_telemetry::{telemetry, SUBSTRATE_INFO};
-use std::convert::TryFrom;
 use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto};
 use sp_transaction_pool::PoolStatus;
 use sp_utils::metrics::register_globals;
 
-#[cfg(any(windows, unix))]
 use sysinfo::{self, ProcessExt, SystemExt};
 
-#[cfg(any(unix, windows))]
-use netstat2::{TcpState, ProtocolSocketInfo, iterate_sockets_info, AddressFamilyFlags, ProtocolFlags};
-
-#[cfg(target_os = "linux")]
-use procfs;
+#[cfg(not(target_os = "unknown"))]
+use netstat2::{
+	TcpState, ProtocolSocketInfo, iterate_sockets_info, AddressFamilyFlags, ProtocolFlags,
+};
 
 struct PrometheusMetrics {
 	// system
@@ -42,7 +41,7 @@ struct PrometheusMetrics {
 	memory_usage_bytes: Gauge<U64>,
 	threads: Gauge<U64>,
 	open_files: GaugeVec<U64>,
-	
+
 	#[cfg(any(unix, windows))]
 	netstat: GaugeVec<U64>,
 
@@ -71,13 +70,13 @@ impl PrometheusMetrics {
 				.const_label("name", name)
 				.const_label("version", version)
 		)?, &registry)?.set(1);
-		
+
 		register(Gauge::<U64>::new(
 			"node_roles", "The roles the node is running as",
 		)?, &registry)?.set(roles);
 
 		register_globals(registry)?;
-		
+
 		Ok(Self {
 			// system
 			#[cfg(any(unix, windows))]
@@ -113,7 +112,6 @@ impl PrometheusMetrics {
 			// --- internal
 
 			// generic internals
-
 			block_height: register(GaugeVec::new(
 				Opts::new("block_height", "Block height info of the chain"),
 				&["status"]
@@ -128,7 +126,6 @@ impl PrometheusMetrics {
 			)?, registry)?,
 
 			// I/ O
-			
 			network_per_sec_bytes: register(GaugeVec::new(
 				Opts::new("network_per_sec_bytes", "Networking bytes per second"),
 				&["direction"]
@@ -179,9 +176,9 @@ struct ProcessInfo {
 
 pub struct MetricsService {
 	metrics: Option<PrometheusMetrics>,
-	#[cfg(any(windows, unix))]
+	#[cfg(not(target_os = "unknown"))]
 	system: sysinfo::System,
-	pid: Option<i32>,
+	pid: Option<sysinfo::Pid>,
 }
 
 #[cfg(target_os = "linux")]
@@ -196,12 +193,14 @@ impl MetricsService {
 			pid: Some(process.pid),
 		}
 	}
+
 	fn process_info(&mut self) -> ProcessInfo {
 		let pid = self.pid.clone().expect("unix always has a pid. qed");
-		let mut info = self._process_info_for(&pid);
+		let mut info = self.process_info_for(&pid);
 		let process = procfs::process::Process::new(pid).expect("Our process exists. qed.");
 		info.threads = process.stat().ok().map(|s|
-			u64::try_from(s.num_threads).expect("There are no negative thread counts. qed"));
+			u64::try_from(s.num_threads).expect("There are no negative thread counts. qed"),
+		);
 		info.open_fd = process.fd().ok().map(|i|
 			i.into_iter().fold(FdCounter::default(), |mut f, info| {
 				match info.target {
@@ -218,7 +217,6 @@ impl MetricsService {
 		);
 		info
 	}
-	
 }
 
 #[cfg(all(any(unix, windows), not(target_os = "linux")))]
@@ -227,24 +225,25 @@ impl MetricsService {
 		Self {
 			metrics,
 			system: sysinfo::System::new(),
-			pid: sysinfo::get_current_pid().ok()
+			pid: sysinfo::get_current_pid().ok(),
 		}
 	}
-	
+
 	fn process_info(&mut self) -> ProcessInfo {
-		self.pid.map(|pid| self._process_info_for(&pid)).unwrap_or_else(ProcessInfo::default)
+		self.pid.map(|pid| self.process_info_for(&pid)).unwrap_or_default()
 	}
 }
 
-#[cfg(not(any(unix, windows)))]
+
+#[cfg(target_os = "unknown")]
 impl MetricsService {
 	fn inner_new(metrics: Option<PrometheusMetrics>) -> Self {
 		Self {
 			metrics,
-			pid: None
+			pid: None,
 		}
 	}
-	
+
 	fn process_info(&mut self) -> ProcessInfo {
 		ProcessInfo::default()
 	}
@@ -252,7 +251,6 @@ impl MetricsService {
 
 
 impl MetricsService {
-
 	pub fn with_prometheus(registry: &Registry, name: &str, version: &str, roles: u64)
 		-> Result<Self, PrometheusError>
 	{
@@ -265,8 +263,8 @@ impl MetricsService {
 		Self::inner_new(None)
 	}
 
-	#[cfg(any(windows, unix))]
-	fn _process_info_for(&mut self, pid: &i32) -> ProcessInfo {
+	#[cfg(not(target_os = "unknown"))]
+	fn process_info_for(&mut self, pid: &sysinfo::Pid) -> ProcessInfo {
 		let mut info = ProcessInfo::default();
 		if self.system.refresh_process(*pid) {
 			let prc = self.system.get_process(*pid)
@@ -277,7 +275,7 @@ impl MetricsService {
 		info
 	}
 
-	#[cfg(any(unix, windows))]
+	#[cfg(not(target_os = "unknown"))]
 	fn connections_info(&self) -> Option<ConnectionsCount> {
 		self.pid.as_ref().and_then(|pid| {
 			let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
@@ -285,15 +283,12 @@ impl MetricsService {
 			let netstat_pid = *pid as u32;
 
 			iterate_sockets_info(af_flags, proto_flags).ok().map(|iter|
-				iter.filter_map(|r| 
+				iter.filter_map(|r|
 					r.ok().and_then(|s| {
-						if s.associated_pids.contains(&netstat_pid) {
-							match s.protocol_socket_info {
-								ProtocolSocketInfo::Tcp(info) => Some(info.state),
-								_ => None
-							}
-						} else {
-							None
+						match s.protocol_socket_info {
+							ProtocolSocketInfo::Tcp(info)
+								if s.associated_pids.contains(&netstat_pid) => Some(info.state),
+							_ => None
 						}
 					})
 				).fold(ConnectionsCount::default(), |mut counter, socket_state| {
@@ -317,7 +312,7 @@ impl MetricsService {
 		&mut self,
 		info: &ClientInfo<T>,
 		txpool_status: &PoolStatus,
-		net_status: &NetworkStatus<T>
+		net_status: &NetworkStatus<T>,
 	) {
 
 		let best_number = info.chain.best_number.saturated_into::<u64>();
@@ -377,8 +372,12 @@ impl MetricsService {
 			}
 
 
-			metrics.network_per_sec_bytes.with_label_values(&["download"]).set(net_status.average_download_per_sec);
-			metrics.network_per_sec_bytes.with_label_values(&["upload"]).set(net_status.average_upload_per_sec);
+			metrics.network_per_sec_bytes.with_label_values(&["download"]).set(
+				net_status.average_download_per_sec,
+			);
+			metrics.network_per_sec_bytes.with_label_values(&["upload"]).set(
+				net_status.average_upload_per_sec,
+			);
 
 			metrics.block_height.with_label_values(&["finalized"]).set(finalized_number);
 			metrics.block_height.with_label_values(&["best"]).set(best_number);
@@ -396,14 +395,18 @@ impl MetricsService {
 				metrics.database_cache.set(info.memory.database_cache.as_bytes() as u64);
 				metrics.state_cache.set(info.memory.state_cache.as_bytes() as u64);
 
-				metrics.state_db.with_label_values(&["non_canonical"]).set(info.memory.state_db.non_canonical.as_bytes() as u64);
+				metrics.state_db.with_label_values(&["non_canonical"]).set(
+					info.memory.state_db.non_canonical.as_bytes() as u64,
+				);
 				if let Some(pruning) = info.memory.state_db.pruning {
 					metrics.state_db.with_label_values(&["pruning"]).set(pruning.as_bytes() as u64);
 				}
-				metrics.state_db.with_label_values(&["pinned"]).set(info.memory.state_db.pinned.as_bytes() as u64);
+				metrics.state_db.with_label_values(&["pinned"]).set(
+					info.memory.state_db.pinned.as_bytes() as u64,
+				);
 			}
 
-			#[cfg(any(unix, windows))]
+			#[cfg(not(target_os = "unknown"))]
 			{
 				let load = self.system.get_load_average();
 				metrics.load_avg.with_label_values(&["1min"]).set(load.one);
-- 
GitLab