#![allow(missing_docs)]
use prometheus_static_metric::make_static_metric;
use std::cell::Cell;
pub use prometheus::*;
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,
},
}
struct CkbHeaderMapMemoryHitMissStatistics: IntCounter{
"type" => {
hit,
miss,
},
}
}
pub struct Metrics {
pub ckb_chain_tip: IntGauge,
pub ckb_chain_unverified_tip: IntGauge,
pub ckb_chain_async_process_block_duration: Histogram,
pub ckb_chain_process_lonely_block_duration: Histogram,
pub ckb_chain_consume_unverified_block_duration: Histogram,
pub ckb_chain_consume_unverified_block_waiting_block_duration: Histogram,
pub ckb_chain_execute_callback_duration: Histogram,
pub ckb_chain_orphan_count: IntGauge,
pub ckb_chain_lonely_block_ch_len: IntGauge,
pub ckb_chain_unverified_block_ch_len: IntGauge,
pub ckb_chain_preload_unverified_block_ch_len: IntGauge,
pub ckb_chain_load_full_unverified_block: Histogram,
pub ckb_sync_msg_process_duration: HistogramVec,
pub ckb_sync_block_fetch_duration: Histogram,
pub ckb_header_map_limit_memory_duration: Histogram,
pub ckb_header_map_ops_duration: HistogramVec,
pub ckb_header_map_memory_count: IntGauge,
pub ckb_header_map_memory_hit_miss_count: CkbHeaderMapMemoryHitMissStatistics,
pub ckb_freezer_size: IntGauge,
pub ckb_freezer_read: IntCounter,
pub ckb_freezer_number: IntGauge,
pub ckb_relay_transaction_short_id_collide: IntCounter,
pub ckb_relay_cb_verify_duration: Histogram,
pub ckb_block_process_duration: Histogram,
pub ckb_tx_pool_sync_process: Histogram,
pub ckb_tx_pool_async_process: 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,
pub ckb_inflight_blocks_count: IntGauge,
pub ckb_inflight_timeout_count: IntCounter,
}
static METRICS: std::sync::LazyLock<Metrics> = std::sync::LazyLock::new(|| {
Metrics {
ckb_chain_tip: register_int_gauge!("ckb_chain_tip", "The CKB chain tip header number").unwrap(),
ckb_chain_unverified_tip: register_int_gauge!(
"ckb_chain_unverified_tip",
"The CKB chain unverified tip header number"
)
.unwrap(),
ckb_chain_async_process_block_duration: register_histogram!(
"ckb_chain_async_process_block_duration",
"The CKB chain asynchronous_process_block duration (seconds)"
)
.unwrap(),
ckb_chain_process_lonely_block_duration: register_histogram!(
"ckb_chain_process_lonely_block_duration",
"The CKB chain consume_orphan thread's process_lonely_block duration (seconds)"
)
.unwrap(),
ckb_chain_consume_unverified_block_duration: register_histogram!(
"ckb_chain_consume_unverified_block_duration",
"The CKB chain consume_unverified thread's consume_unverified_block duration (seconds)"
)
.unwrap(),
ckb_chain_consume_unverified_block_waiting_block_duration: register_histogram!(
"ckb_chain_consume_unverified_block_waiting_block_duration",
"The CKB chain consume_unverified thread's consume_unverified_block waiting for block duration (seconds)"
).unwrap(),
ckb_chain_execute_callback_duration: register_histogram!(
"ckb_chain_execute_callback_duration",
"The CKB chain execute_callback duration (seconds)"
).unwrap(),
ckb_chain_orphan_count: register_int_gauge!(
"ckb_chain_orphan_count",
"The CKB chain orphan blocks count",
).unwrap(),
ckb_chain_lonely_block_ch_len: register_int_gauge!(
"ckb_chain_lonely_block_ch_len",
"The CKB chain lonely block channel length",
).unwrap(),
ckb_chain_unverified_block_ch_len: register_int_gauge!(
"ckb_chain_unverified_block_ch_len",
"The CKB chain unverified block channel length",
).unwrap(),
ckb_chain_preload_unverified_block_ch_len: register_int_gauge!(
"ckb_chain_preload_unverified_block_ch_len",
"The CKB chain fill unverified block channel length",
).unwrap(),
ckb_chain_load_full_unverified_block: register_histogram!(
"ckb_chain_load_full_unverified_block",
"The CKB chain load_full_unverified_block duration (seconds)"
).unwrap(),
ckb_sync_msg_process_duration: register_histogram_vec!(
"ckb_sync_msg_process_duration",
"The CKB sync message process duration (seconds)",
&["msg_type"],
).unwrap(),
ckb_sync_block_fetch_duration: register_histogram!(
"ckb_sync_block_fetch_duration",
"The CKB sync block fetch duration (seconds)"
).unwrap(),
ckb_header_map_limit_memory_duration: register_histogram!(
"ckb_header_map_limit_memory_duration",
"The CKB header map limit_memory job duration (seconds)"
).unwrap(),
ckb_header_map_ops_duration: register_histogram_vec!(
"ckb_header_map_ops_duration",
"The CKB header map operation duration (seconds)",
&["operation"],
).unwrap(),
ckb_header_map_memory_count: register_int_gauge!(
"ckb_header_map_memory_count",
"The CKB HeaderMap memory count",
).unwrap(),
ckb_header_map_memory_hit_miss_count: CkbHeaderMapMemoryHitMissStatistics::from(
®ister_int_counter_vec!(
"ckb_header_map_memory_hit_miss_count",
"The CKB HeaderMap memory hit count",
&["type"]
)
.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_freezer_number: register_int_gauge!("ckb_freezer_number", "The CKB freezer number").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_block_process_duration: register_histogram!(
"ckb_block_process_duration",
"The CKB block process duration"
)
.unwrap(),
ckb_tx_pool_sync_process: register_histogram!(
"ckb_tx_pool_sync_process",
"The CKB tx_pool sync process tx duration"
)
.unwrap(),
ckb_tx_pool_async_process: register_histogram!(
"ckb_tx_pool_async_process",
"The CKB tx_pool async process tx 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(),
ckb_inflight_blocks_count: register_int_gauge!(
"ckb_inflight_blocks_count",
"The CKB inflight blocks count"
)
.unwrap(),
ckb_inflight_timeout_count: register_int_counter!(
"ckb_inflight_timeout_count",
"The CKB inflight timeout count"
).unwrap(),
}
});
pub static METRICS_SERVICE_ENABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::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());
}
}