Skip to content

Commit

Permalink
Merge pull request paritytech#7 from paritytech/ashley-prometheus
Browse files Browse the repository at this point in the history
Improve metric names and use GaugeVecs for some things
  • Loading branch information
nodebreaker0-0 authored Jan 29, 2020
2 parents f64a482 + 6a25ea8 commit 18cf2be
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 107 deletions.
35 changes: 0 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 39 additions & 64 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,68 +51,40 @@ use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
use sp_transaction_pool::{TransactionPool, TransactionPoolMaintainer};
use sp_blockchain;
use prometheus_exporter::{create_gauge, Gauge, U64, F64, Registry, PrometheusError};
use prometheus_exporter::{register, Gauge, U64, F64, Registry, PrometheusError, Opts, GaugeVec};

struct ServiceMetrics {
finality_height: Gauge<U64>,
best_height: Gauge<U64>,
peers_num: Gauge<U64>,
tx_count: Gauge<U64>,
node_memory: Gauge<U64>,
node_cpu: Gauge<F64>,
node_download: Gauge<U64>,
node_upload: Gauge<U64>,
sync_target: Gauge<U64>,
block_height_number: GaugeVec<U64>,
peers_count: Gauge<U64>,
ready_transactions_number: Gauge<U64>,
memory_usage_bytes: Gauge<U64>,
cpu_usage_percentage: Gauge<F64>,
network_per_sec_bytes: GaugeVec<U64>,
}

impl ServiceMetrics {
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
finality_height: create_gauge(
"finality_block_height_number",
"Height of the highest finalized block",
registry
)?,
best_height: create_gauge(
"best_block_height_number",
"Height of the highest block",
registry
)?,
peers_num: create_gauge(
"peers_count",
"Number of network gossip peers",
registry
)?,
tx_count: create_gauge(
"transaction_count",
"Number of transactions",
registry
)?,
node_memory: create_gauge(
"memory_usage",
"Node memory usage",
registry
)?,
node_cpu: create_gauge(
"cpu_usage",
"Node CPU usage",
registry
)?,
node_download: create_gauge(
"receive_byte_per_sec",
"Received bytes per second",
registry
)?,
node_upload: create_gauge(
"sent_byte_per_sec",
"Sent bytes per second",
registry
)?,
sync_target: create_gauge(
"sync_target_number",
"Block sync target number",
registry
)?,
block_height_number: register(GaugeVec::new(
Opts::new("block_height_number", "Height of the chain"),
&["status"]
)?, registry)?,
peers_count: register(Gauge::new(
"peers_count", "Number of network gossip peers",
)?, registry)?,
ready_transactions_number: register(Gauge::new(
"ready_transactions_number", "Number of transactions in the ready queue",
)?, registry)?,
memory_usage_bytes: register(Gauge::new(
"memory_usage_bytes", "Node memory usage",
)?, registry)?,
cpu_usage_percentage: register(Gauge::new(
"cpu_usage_percentage", "Node CPU usage",
)?, registry)?,
network_per_sec_bytes: register(GaugeVec::new(
Opts::new("network_per_sec_bytes", "Networking bytes per second"),
&["direction"]
)?, registry)?,
})
}
}
Expand Down Expand Up @@ -1087,16 +1059,19 @@ ServiceBuilder<
"disk_write_per_sec" => info.usage.as_ref().map(|usage| usage.io.bytes_written).unwrap_or(0),
);
if let Some(metrics) = metrics.as_ref() {
metrics.node_memory.set(memory);
metrics.node_cpu.set(f64::from(cpu_usage));
metrics.tx_count.set(txpool_status.ready as u64);
metrics.finality_height.set(finalized_number);
metrics.best_height.set(best_number);
metrics.peers_num.set(num_peers as u64);
metrics.node_download.set(net_status.average_download_per_sec);
metrics.node_upload.set(net_status.average_upload_per_sec);
metrics.memory_usage_bytes.set(memory);
metrics.cpu_usage_percentage.set(f64::from(cpu_usage));
metrics.ready_transactions_number.set(txpool_status.ready as u64);
metrics.peers_count.set(num_peers as u64);

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_number.with_label_values(&["finalized"]).set(finalized_number);
metrics.block_height_number.with_label_values(&["best"]).set(best_number);

if let Some(best_seen_block) = best_seen_block {
metrics.sync_target.set(best_seen_block);
metrics.block_height_number.with_label_values(&["sync_target"]).set(best_seen_block);
}
}

Expand Down
2 changes: 1 addition & 1 deletion utils/prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2018"
[dependencies]
log = "0.4.8"
hyper = { version = "0.13.1", default-features = false, features = ["stream"] }
prometheus = { version = "0.7", features = ["process"]}
prometheus = "0.7"
tokio = "0.2"
futures-util = { version = "0.3.1", default-features = false, features = ["io"] }
derive_more = "0.99"
Expand Down
17 changes: 10 additions & 7 deletions utils/prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@
use futures_util::{FutureExt, future::Future};
use hyper::http::StatusCode;
use hyper::{Server, Body, Response, service::{service_fn, make_service_fn}};
use prometheus::{Encoder, Opts, TextEncoder, core::Atomic};
use prometheus::{Encoder, TextEncoder, core::Collector};
use std::net::SocketAddr;
#[cfg(not(target_os = "unknown"))]
mod networking;

pub use prometheus::{
Registry, Error as PrometheusError,
core::{GenericGauge as Gauge, AtomicF64 as F64, AtomicI64 as I64, AtomicU64 as U64}
Registry, Error as PrometheusError, Opts,
core::{
GenericGauge as Gauge, GenericCounter as Counter,
GenericGaugeVec as GaugeVec, GenericCounterVec as CounterVec,
AtomicF64 as F64, AtomicI64 as I64, AtomicU64 as U64,
}
};

pub fn create_gauge<T: Atomic + 'static>(name: &str, description: &str, registry: &Registry) -> Result<Gauge<T>, PrometheusError> {
let gauge = Gauge::with_opts(Opts::new(name, description))?;
registry.register(Box::new(gauge.clone()))?;
Ok(gauge)
pub fn register<T: Clone + Collector + 'static>(metric: T, registry: &Registry) -> Result<T, PrometheusError> {
registry.register(Box::new(metric.clone()))?;
Ok(metric)
}

#[derive(Debug, derive_more::Display, derive_more::From)]
Expand Down

0 comments on commit 18cf2be

Please sign in to comment.