#![allow(missing_docs)]
use prometheus::{
register_histogram, register_histogram_vec, register_int_counter, register_int_gauge,
register_int_gauge_vec, Histogram, HistogramVec, IntCounter, IntGauge, IntGaugeVec,
};
use prometheus_static_metric::make_static_metric;
use std::cell::Cell;
pub fn gather() -> Vec<prometheus::proto::MetricFamily> {
prometheus::gather()
}
make_static_metric! {
struct CkbSysMemProcessStatistics: IntGauge{
"type" => {
rss,
vms,
},
}
struct CkbSysMemJemallocStatistics: IntGauge{
"type" => {
allocated,
resident,
active,
mapped,
retained,
metadata,
},
}
struct CkbTxPoolEntryStatistics: IntGauge{
"type" => {
pending,
gap,
proposed,
},
}
}
pub struct Metrics {
pub ckb_chain_tip: IntGauge,
pub ckb_freezer_size: IntGauge,
pub ckb_freezer_read: IntCounter,
pub ckb_relay_transaction_short_id_collide: IntCounter,
pub ckb_relay_cb_verify_duration: Histogram,
pub ckb_relay_cb_transaction_count: IntCounter,
pub ckb_relay_cb_reconstruct_ok: IntCounter,
pub ckb_relay_cb_fresh_tx_cnt: IntCounter,
pub ckb_relay_cb_reconstruct_fail: IntCounter,
pub ckb_shared_best_number: IntGauge,
pub ckb_sys_mem_process: CkbSysMemProcessStatistics,
pub ckb_sys_mem_jemalloc: CkbSysMemJemallocStatistics,
pub ckb_tx_pool_entry: CkbTxPoolEntryStatistics,
pub ckb_message_bytes: HistogramVec,
pub ckb_sys_mem_rocksdb: IntGaugeVec,
pub ckb_network_ban_peer: IntCounter,
}
static METRICS: once_cell::sync::Lazy<Metrics> = once_cell::sync::Lazy::new(|| Metrics {
ckb_chain_tip: register_int_gauge!("ckb_chain_tip", "The CKB chain tip header number").unwrap(),
ckb_freezer_size: register_int_gauge!("ckb_freezer_size", "The CKB freezer size").unwrap(),
ckb_freezer_read: register_int_counter!("ckb_freezer_read", "The CKB freezer read").unwrap(),
ckb_relay_transaction_short_id_collide: register_int_counter!(
"ckb_relay_transaction_short_id_collide",
"The CKB relay transaction short id collide"
)
.unwrap(),
ckb_relay_cb_verify_duration: register_histogram!(
"ckb_relay_cb_verify_duration",
"The CKB relay compact block verify duration"
)
.unwrap(),
ckb_relay_cb_transaction_count: register_int_counter!(
"ckb_relay_cb_transaction_count",
"The CKB relay compact block transaction count"
)
.unwrap(),
ckb_relay_cb_reconstruct_ok: register_int_counter!(
"ckb_relay_cb_reconstruct_ok",
"The CKB relay compact block reconstruct ok count"
)
.unwrap(),
ckb_relay_cb_fresh_tx_cnt: register_int_counter!(
"ckb_relay_cb_fresh_tx_cnt",
"The CKB relay compact block fresh tx count"
)
.unwrap(),
ckb_relay_cb_reconstruct_fail: register_int_counter!(
"ckb_relay_cb_reconstruct_fail",
"The CKB relay compact block reconstruct fail count"
)
.unwrap(),
ckb_shared_best_number: register_int_gauge!(
"ckb_shared_best_number",
"The CKB shared best header number"
)
.unwrap(),
ckb_sys_mem_process: CkbSysMemProcessStatistics::from(
®ister_int_gauge_vec!(
"ckb_sys_mem_process",
"CKB system memory for process statistics",
&["type"]
)
.unwrap(),
),
ckb_sys_mem_jemalloc: CkbSysMemJemallocStatistics::from(
®ister_int_gauge_vec!(
"ckb_sys_mem_jemalloc",
"CKB system memory for jemalloc statistics",
&["type"]
)
.unwrap(),
),
ckb_tx_pool_entry: CkbTxPoolEntryStatistics::from(
®ister_int_gauge_vec!(
"ckb_tx_pool_entry",
"CKB tx-pool entry status statistics",
&["type"]
)
.unwrap(),
),
ckb_message_bytes: register_histogram_vec!(
"ckb_message_bytes",
"The CKB message bytes",
&["direction", "protocol_name", "msg_item_name", "status_code"],
vec![
500.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0, 50000.0, 100000.0, 200000.0, 500000.0
]
)
.unwrap(),
ckb_sys_mem_rocksdb: register_int_gauge_vec!(
"ckb_sys_mem_rocksdb",
"CKB system memory for rocksdb statistics",
&["type", "cf"]
)
.unwrap(),
ckb_network_ban_peer: register_int_counter!(
"ckb_network_ban_peer",
"CKB network baned peer count"
)
.unwrap(),
});
pub static METRICS_SERVICE_ENABLED: once_cell::sync::OnceCell<bool> =
once_cell::sync::OnceCell::new();
thread_local! {
static ENABLE_COLLECT_METRICS: Cell<Option<bool>>= Cell::default();
}
pub fn handle() -> Option<&'static Metrics> {
let enabled_collect_metrics: bool =
ENABLE_COLLECT_METRICS.with(
|enable_collect_metrics| match enable_collect_metrics.get() {
Some(enabled) => enabled,
None => match METRICS_SERVICE_ENABLED.get().copied() {
Some(enabled) => {
enable_collect_metrics.set(Some(enabled));
enabled
}
None => false,
},
},
);
if enabled_collect_metrics {
Some(&METRICS)
} else {
None
}
}
#[cfg(test)]
mod tests {
use crate::METRICS;
use std::ops::Deref;
#[test]
fn test_metrics_name() {
let _ = METRICS.deref();
}
#[test]
#[should_panic]
fn test_bad_metrics_name() {
let res = prometheus::register_int_gauge!(
"ckb.chain.tip",
"a bad metric which contains '.' in its name"
);
assert!(res.is_err());
let res = prometheus::register_int_gauge!(
"ckb-chain-tip",
"a bad metric which contains '-' in its name"
);
assert!(res.is_err());
}
}