mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 06:21:11 +00:00
Fix compilation of service metrics on Windows (#5548)
* Fix compilation of service metrics on Windows * Fix browser node * Ahh * Remove duplicate code...
This commit is contained in:
@@ -14,23 +14,22 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use crate::NetworkStatus;
|
use crate::NetworkStatus;
|
||||||
use prometheus_endpoint::{register, Gauge, U64, F64, Registry, PrometheusError, Opts, GaugeVec};
|
use prometheus_endpoint::{register, Gauge, U64, F64, Registry, PrometheusError, Opts, GaugeVec};
|
||||||
use sc_client::ClientInfo;
|
use sc_client::ClientInfo;
|
||||||
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
|
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
|
||||||
use std::convert::TryFrom;
|
|
||||||
use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto};
|
use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto};
|
||||||
use sp_transaction_pool::PoolStatus;
|
use sp_transaction_pool::PoolStatus;
|
||||||
use sp_utils::metrics::register_globals;
|
use sp_utils::metrics::register_globals;
|
||||||
|
|
||||||
#[cfg(any(windows, unix))]
|
|
||||||
use sysinfo::{self, ProcessExt, SystemExt};
|
use sysinfo::{self, ProcessExt, SystemExt};
|
||||||
|
|
||||||
#[cfg(any(unix, windows))]
|
#[cfg(not(target_os = "unknown"))]
|
||||||
use netstat2::{TcpState, ProtocolSocketInfo, iterate_sockets_info, AddressFamilyFlags, ProtocolFlags};
|
use netstat2::{
|
||||||
|
TcpState, ProtocolSocketInfo, iterate_sockets_info, AddressFamilyFlags, ProtocolFlags,
|
||||||
#[cfg(target_os = "linux")]
|
};
|
||||||
use procfs;
|
|
||||||
|
|
||||||
struct PrometheusMetrics {
|
struct PrometheusMetrics {
|
||||||
// system
|
// system
|
||||||
@@ -113,7 +112,6 @@ impl PrometheusMetrics {
|
|||||||
// --- internal
|
// --- internal
|
||||||
|
|
||||||
// generic internals
|
// generic internals
|
||||||
|
|
||||||
block_height: register(GaugeVec::new(
|
block_height: register(GaugeVec::new(
|
||||||
Opts::new("block_height", "Block height info of the chain"),
|
Opts::new("block_height", "Block height info of the chain"),
|
||||||
&["status"]
|
&["status"]
|
||||||
@@ -128,7 +126,6 @@ impl PrometheusMetrics {
|
|||||||
)?, registry)?,
|
)?, registry)?,
|
||||||
|
|
||||||
// I/ O
|
// I/ O
|
||||||
|
|
||||||
network_per_sec_bytes: register(GaugeVec::new(
|
network_per_sec_bytes: register(GaugeVec::new(
|
||||||
Opts::new("network_per_sec_bytes", "Networking bytes per second"),
|
Opts::new("network_per_sec_bytes", "Networking bytes per second"),
|
||||||
&["direction"]
|
&["direction"]
|
||||||
@@ -179,9 +176,9 @@ struct ProcessInfo {
|
|||||||
|
|
||||||
pub struct MetricsService {
|
pub struct MetricsService {
|
||||||
metrics: Option<PrometheusMetrics>,
|
metrics: Option<PrometheusMetrics>,
|
||||||
#[cfg(any(windows, unix))]
|
#[cfg(not(target_os = "unknown"))]
|
||||||
system: sysinfo::System,
|
system: sysinfo::System,
|
||||||
pid: Option<i32>,
|
pid: Option<sysinfo::Pid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
@@ -196,12 +193,14 @@ impl MetricsService {
|
|||||||
pid: Some(process.pid),
|
pid: Some(process.pid),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_info(&mut self) -> ProcessInfo {
|
fn process_info(&mut self) -> ProcessInfo {
|
||||||
let pid = self.pid.clone().expect("unix always has a pid. qed");
|
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.");
|
let process = procfs::process::Process::new(pid).expect("Our process exists. qed.");
|
||||||
info.threads = process.stat().ok().map(|s|
|
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|
|
info.open_fd = process.fd().ok().map(|i|
|
||||||
i.into_iter().fold(FdCounter::default(), |mut f, info| {
|
i.into_iter().fold(FdCounter::default(), |mut f, info| {
|
||||||
match info.target {
|
match info.target {
|
||||||
@@ -218,7 +217,6 @@ impl MetricsService {
|
|||||||
);
|
);
|
||||||
info
|
info
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(any(unix, windows), not(target_os = "linux")))]
|
#[cfg(all(any(unix, windows), not(target_os = "linux")))]
|
||||||
@@ -227,21 +225,22 @@ impl MetricsService {
|
|||||||
Self {
|
Self {
|
||||||
metrics,
|
metrics,
|
||||||
system: sysinfo::System::new(),
|
system: sysinfo::System::new(),
|
||||||
pid: sysinfo::get_current_pid().ok()
|
pid: sysinfo::get_current_pid().ok(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_info(&mut self) -> ProcessInfo {
|
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 {
|
impl MetricsService {
|
||||||
fn inner_new(metrics: Option<PrometheusMetrics>) -> Self {
|
fn inner_new(metrics: Option<PrometheusMetrics>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
metrics,
|
metrics,
|
||||||
pid: None
|
pid: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +251,6 @@ impl MetricsService {
|
|||||||
|
|
||||||
|
|
||||||
impl MetricsService {
|
impl MetricsService {
|
||||||
|
|
||||||
pub fn with_prometheus(registry: &Registry, name: &str, version: &str, roles: u64)
|
pub fn with_prometheus(registry: &Registry, name: &str, version: &str, roles: u64)
|
||||||
-> Result<Self, PrometheusError>
|
-> Result<Self, PrometheusError>
|
||||||
{
|
{
|
||||||
@@ -265,8 +263,8 @@ impl MetricsService {
|
|||||||
Self::inner_new(None)
|
Self::inner_new(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(windows, unix))]
|
#[cfg(not(target_os = "unknown"))]
|
||||||
fn _process_info_for(&mut self, pid: &i32) -> ProcessInfo {
|
fn process_info_for(&mut self, pid: &sysinfo::Pid) -> ProcessInfo {
|
||||||
let mut info = ProcessInfo::default();
|
let mut info = ProcessInfo::default();
|
||||||
if self.system.refresh_process(*pid) {
|
if self.system.refresh_process(*pid) {
|
||||||
let prc = self.system.get_process(*pid)
|
let prc = self.system.get_process(*pid)
|
||||||
@@ -277,7 +275,7 @@ impl MetricsService {
|
|||||||
info
|
info
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(unix, windows))]
|
#[cfg(not(target_os = "unknown"))]
|
||||||
fn connections_info(&self) -> Option<ConnectionsCount> {
|
fn connections_info(&self) -> Option<ConnectionsCount> {
|
||||||
self.pid.as_ref().and_then(|pid| {
|
self.pid.as_ref().and_then(|pid| {
|
||||||
let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
|
let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
|
||||||
@@ -287,13 +285,10 @@ impl MetricsService {
|
|||||||
iterate_sockets_info(af_flags, proto_flags).ok().map(|iter|
|
iterate_sockets_info(af_flags, proto_flags).ok().map(|iter|
|
||||||
iter.filter_map(|r|
|
iter.filter_map(|r|
|
||||||
r.ok().and_then(|s| {
|
r.ok().and_then(|s| {
|
||||||
if s.associated_pids.contains(&netstat_pid) {
|
match s.protocol_socket_info {
|
||||||
match s.protocol_socket_info {
|
ProtocolSocketInfo::Tcp(info)
|
||||||
ProtocolSocketInfo::Tcp(info) => Some(info.state),
|
if s.associated_pids.contains(&netstat_pid) => Some(info.state),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
).fold(ConnectionsCount::default(), |mut counter, socket_state| {
|
).fold(ConnectionsCount::default(), |mut counter, socket_state| {
|
||||||
@@ -317,7 +312,7 @@ impl MetricsService {
|
|||||||
&mut self,
|
&mut self,
|
||||||
info: &ClientInfo<T>,
|
info: &ClientInfo<T>,
|
||||||
txpool_status: &PoolStatus,
|
txpool_status: &PoolStatus,
|
||||||
net_status: &NetworkStatus<T>
|
net_status: &NetworkStatus<T>,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
let best_number = info.chain.best_number.saturated_into::<u64>();
|
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(&["download"]).set(
|
||||||
metrics.network_per_sec_bytes.with_label_values(&["upload"]).set(net_status.average_upload_per_sec);
|
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(&["finalized"]).set(finalized_number);
|
||||||
metrics.block_height.with_label_values(&["best"]).set(best_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.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_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 {
|
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(&["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();
|
let load = self.system.get_load_average();
|
||||||
metrics.load_avg.with_label_values(&["1min"]).set(load.one);
|
metrics.load_avg.with_label_values(&["1min"]).set(load.one);
|
||||||
|
|||||||
Reference in New Issue
Block a user