Skip to content

Commit

Permalink
refactor: Remove usage of lazy_static! from chain (#5318)
Browse files Browse the repository at this point in the history
This is continuation of #5317
This time we will format metrics in `chain/chain/src/metrics.rs` file.

Part of PR: https://github.com/near/nearcore/pull/5321/files
  • Loading branch information
pmnoxx authored Nov 24, 2021
1 parent ea68aed commit 3515193
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion chain/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chrono = { version = "0.4.4", features = ["serde"] }
failure = "0.1"
failure_derive = "0.1"
itertools = "0.10.0"
lazy_static = "1.4"
once_cell = "1.5.2"
rand = "0.7"
serde = { version = "1", features = [ "derive" ] }
cached = "0.23"
Expand Down
26 changes: 12 additions & 14 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ impl Chain {
F3: Copy + FnMut(ChallengeBody) -> (),
{
let block_hash = *block.hash();
let timer = near_metrics::start_timer(&metrics::BLOCK_PROCESSING_TIME);
let timer = metrics::BLOCK_PROCESSING_TIME.start_timer();
let res = self.process_block_single(
me,
block,
Expand All @@ -906,9 +906,9 @@ impl Chain {
block_orphaned_with_missing_chunks,
on_challenge,
);
near_metrics::stop_timer(timer);
timer.observe_duration();
if res.is_ok() {
near_metrics::inc_counter(&metrics::BLOCK_PROCESSED_SUCCESSFULLY_TOTAL);
metrics::BLOCK_PROCESSED_SUCCESSFULLY_TOTAL.inc();

if let Some(new_res) = self.check_orphans(
me,
Expand Down Expand Up @@ -1226,8 +1226,8 @@ impl Chain {
F2: Copy + FnMut(OrphanMissingChunks) -> (),
F3: FnMut(ChallengeBody) -> (),
{
near_metrics::inc_counter(&metrics::BLOCK_PROCESSED_TOTAL);
near_metrics::set_gauge(&metrics::NUM_ORPHANS, self.orphans.len() as i64);
metrics::BLOCK_PROCESSED_TOTAL.inc();
metrics::NUM_ORPHANS.set(self.orphans.len() as i64);

let prev_head = self.store.head()?;
let mut chain_update = self.chain_update();
Expand Down Expand Up @@ -1261,11 +1261,9 @@ impl Chain {
}
}
stake /= NEAR_BASE;
near_metrics::set_gauge(
&metrics::VALIDATOR_AMOUNT_STAKED,
i64::try_from(stake).unwrap_or(i64::MAX),
);
near_metrics::set_gauge(&metrics::VALIDATOR_ACTIVE_TOTAL, count);
metrics::VALIDATOR_AMOUNT_STAKED
.set(i64::try_from(stake).unwrap_or(i64::MAX));
metrics::VALIDATOR_ACTIVE_TOTAL.set(count);
}
}
None => {}
Expand Down Expand Up @@ -1570,7 +1568,7 @@ impl Chain {
debug!(target: "chain", "Check orphans: found {} orphans", orphans.len());
for orphan in orphans.into_iter() {
let block_hash = orphan.hash();
let timer = near_metrics::start_timer(&metrics::BLOCK_PROCESSING_TIME);
let timer = metrics::BLOCK_PROCESSING_TIME.start_timer();
let res = self.process_block_single(
me,
orphan.block,
Expand All @@ -1580,10 +1578,10 @@ impl Chain {
orphan_misses_chunks,
on_challenge,
);
near_metrics::stop_timer(timer);
timer.observe_duration();
match res {
Ok(maybe_tip) => {
near_metrics::inc_counter(&metrics::BLOCK_PROCESSED_SUCCESSFULLY_TOTAL);
metrics::BLOCK_PROCESSED_SUCCESSFULLY_TOTAL.inc();
maybe_new_head = maybe_tip;
queue.push(block_hash);
}
Expand Down Expand Up @@ -4432,7 +4430,7 @@ impl<'a> ChainUpdate<'a> {
let tip = Tip::from_header(header);

self.chain_store_update.save_body_head(&tip)?;
near_metrics::set_gauge(&metrics::BLOCK_HEIGHT_HEAD, tip.height as i64);
metrics::BLOCK_HEIGHT_HEAD.set(tip.height as i64);
debug!(target: "chain", "Head updated to {} at {}", tip.last_block_hash, tip.height);
Ok(Some(tip))
} else {
Expand Down
3 changes: 0 additions & 3 deletions chain/chain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[macro_use]
extern crate lazy_static;

pub use chain::{collect_receipts, Chain, MAX_ORPHAN_SIZE};
pub use doomslug::{Doomslug, DoomslugBlockProductionReadiness, DoomslugThresholdMode};
pub use lightclient::{create_light_client_block_view, get_epoch_block_producers_view};
Expand Down
56 changes: 33 additions & 23 deletions chain/chain/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@ use near_metrics::{
try_create_histogram, try_create_int_counter, try_create_int_gauge, Histogram, IntCounter,
IntGauge,
};
use once_cell::sync::Lazy;

lazy_static! {
pub static ref BLOCK_PROCESSED_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter("near_block_processed_total", "Total number of blocks processed");
pub static ref BLOCK_PROCESSED_SUCCESSFULLY_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_block_processed_successfully_total",
"Total number of blocks processed successfully"
);
pub static ref BLOCK_PROCESSING_TIME: near_metrics::Result<Histogram> =
try_create_histogram("near_block_processing_time", "Time taken to process blocks");
pub static ref BLOCK_HEIGHT_HEAD: near_metrics::Result<IntGauge> = try_create_int_gauge(
"near_block_height_head",
"Height of the current head of the blockchain"
);
pub static ref VALIDATOR_AMOUNT_STAKED: near_metrics::Result<IntGauge> = try_create_int_gauge(
pub static BLOCK_PROCESSED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter("near_block_processed_total", "Total number of blocks processed")
.unwrap()
});
pub static BLOCK_PROCESSED_SUCCESSFULLY_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_block_processed_successfully_total",
"Total number of blocks processed successfully",
)
.unwrap()
});
pub static BLOCK_PROCESSING_TIME: Lazy<Histogram> = Lazy::new(|| {
try_create_histogram("near_block_processing_time", "Time taken to process blocks").unwrap()
});
pub static BLOCK_HEIGHT_HEAD: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_block_height_head", "Height of the current head of the blockchain")
.unwrap()
});
pub static VALIDATOR_AMOUNT_STAKED: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_validators_stake_total",
"The total stake of all active validators during the last block"
);
pub static ref VALIDATOR_ACTIVE_TOTAL: near_metrics::Result<IntGauge> = try_create_int_gauge(
"The total stake of all active validators during the last block",
)
.unwrap()
});
pub static VALIDATOR_ACTIVE_TOTAL: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_validator_active_total",
"The total number of validators active after last block"
);
pub static ref NUM_ORPHANS: near_metrics::Result<IntGauge> =
try_create_int_gauge("near_num_orphans", "Number of orphan blocks.");
}
"The total number of validators active after last block",
)
.unwrap()
});
pub static NUM_ORPHANS: Lazy<IntGauge> =
Lazy::new(|| try_create_int_gauge("near_num_orphans", "Number of orphan blocks.").unwrap());
2 changes: 1 addition & 1 deletion chain/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ serde_json = "1"
sysinfo = { git = "https://github.com/near/sysinfo", rev = "3cb97ee79a02754407d2f0f63628f247d7c65e7b" }
strum = { version = "0.20", features = ["derive"] }
cached = "0.23"
lazy_static = "1.4"
once_cell = "1.5.2"
borsh = "0.9"
reed-solomon-erasure = "4"
num-rational = "0.3"
Expand Down
10 changes: 2 additions & 8 deletions chain/client/src/chunks_delay_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ impl ChunksDelayTracker {
}

fn update_chunks_metric(&mut self, head_height: BlockHeight) {
near_metrics::set_gauge(
&metrics::CHUNKS_RECEIVING_DELAY_US,
self.get_max_delay(head_height).as_micros() as i64,
);
metrics::CHUNKS_RECEIVING_DELAY_US.set(self.get_max_delay(head_height).as_micros() as i64);
}

// Computes the difference between the latest block we are aware of and the current head.
Expand All @@ -78,10 +75,7 @@ impl ChunksDelayTracker {
}

fn update_blocks_ahead_metric(&mut self, head_height: BlockHeight) {
near_metrics::set_gauge(
&metrics::BLOCKS_AHEAD_OF_HEAD,
self.get_blocks_ahead(head_height) as i64,
);
metrics::BLOCKS_AHEAD_OF_HEAD.set(self.get_blocks_ahead(head_height) as i64);
}

fn update_metrics(&mut self, head_height: BlockHeight) {
Expand Down
8 changes: 4 additions & 4 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ impl Client {
seen: to_timestamp(Clock::utc()),
})?;

near_metrics::inc_counter(&metrics::BLOCK_PRODUCED_TOTAL);
metrics::BLOCK_PRODUCED_TOTAL.inc();

Ok(Some(block))
}
Expand Down Expand Up @@ -622,7 +622,7 @@ impl Client {
encoded_chunk.chunk_hash().0,
);

near_metrics::inc_counter(&metrics::CHUNK_PRODUCED_TOTAL);
metrics::CHUNK_PRODUCED_TOTAL.inc();
Ok(Some((encoded_chunk, merkle_paths, outgoing_receipts)))
}

Expand Down Expand Up @@ -1020,15 +1020,15 @@ impl Client {
};
self.chain.blocks_with_missing_chunks.prune_blocks_below_height(last_finalized_height);
if !self.config.archive {
let timer = near_metrics::start_timer(&metrics::GC_TIME);
let timer = metrics::GC_TIME.start_timer();
if let Err(err) = self
.chain
.clear_data(self.runtime_adapter.get_tries(), self.config.gc_blocks_limit)
{
error!(target: "client", "Can't clear old data, {:?}", err);
debug_assert!(false);
};
near_metrics::stop_timer(timer);
timer.observe_duration();
}

if self.runtime_adapter.is_next_block_epoch_start(block.hash()).unwrap_or(false) {
Expand Down
15 changes: 7 additions & 8 deletions chain/client/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use log::info;
use sysinfo::{get_current_pid, set_open_files_limit, Pid, ProcessExt, System, SystemExt};

use near_chain_configs::{ClientConfig, LogSummaryStyle};
use near_metrics::set_gauge;
use near_network::types::NetworkInfo;
use near_primitives::block::Tip;
use near_primitives::network::PeerId;
Expand Down Expand Up @@ -151,14 +150,14 @@ impl InfoHelper {
};

let is_validator = validator_info.map(|v| v.is_validator).unwrap_or_default();
set_gauge(&metrics::IS_VALIDATOR, is_validator as i64);
set_gauge(&metrics::RECEIVED_BYTES_PER_SECOND, network_info.received_bytes_per_sec as i64);
set_gauge(&metrics::SENT_BYTES_PER_SECOND, network_info.sent_bytes_per_sec as i64);
set_gauge(&metrics::BLOCKS_PER_MINUTE, (avg_bls * (60 as f64)) as i64);
set_gauge(&metrics::CPU_USAGE, cpu_usage as i64);
set_gauge(&metrics::MEMORY_USAGE, (memory_usage * 1024) as i64);
(metrics::IS_VALIDATOR.set(is_validator as i64));
(metrics::RECEIVED_BYTES_PER_SECOND.set(network_info.received_bytes_per_sec as i64));
(metrics::SENT_BYTES_PER_SECOND.set(network_info.sent_bytes_per_sec as i64));
(metrics::BLOCKS_PER_MINUTE.set((avg_bls * (60 as f64)) as i64));
(metrics::CPU_USAGE.set(cpu_usage as i64));
(metrics::MEMORY_USAGE.set((memory_usage * 1024) as i64));
let teragas = 1_000_000_000_000u64;
set_gauge(&metrics::AVG_TGAS_USAGE, (avg_gas_used as f64 / teragas as f64).round() as i64);
(metrics::AVG_TGAS_USAGE.set((avg_gas_used as f64 / teragas as f64).round() as i64));

self.started = Clock::instant();
self.num_blocks_processed = 0;
Expand Down
3 changes: 0 additions & 3 deletions chain/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[macro_use]
extern crate lazy_static;

pub use near_client_primitives::types::{
Error, GetBlock, GetBlockProof, GetBlockProofResponse, GetBlockWithMerkleTree, GetChunk,
GetExecutionOutcome, GetExecutionOutcomeResponse, GetExecutionOutcomesForBlock, GetGasPrice,
Expand Down
91 changes: 58 additions & 33 deletions chain/client/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,69 @@ use near_metrics::{
try_create_histogram, try_create_int_counter, try_create_int_gauge, Histogram, IntCounter,
IntGauge,
};
use once_cell::sync::Lazy;

lazy_static! {
pub static ref BLOCK_PRODUCED_TOTAL: near_metrics::Result<IntCounter> = try_create_int_counter(
pub static BLOCK_PRODUCED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_block_produced_total",
"Total number of blocks produced since starting this node"
);
pub static ref CHUNK_PRODUCED_TOTAL: near_metrics::Result<IntCounter> = try_create_int_counter(
"Total number of blocks produced since starting this node",
)
.unwrap()
});
pub static CHUNK_PRODUCED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_chunk_produced_total",
"Total number of chunks produced since starting this node"
);
pub static ref IS_VALIDATOR: near_metrics::Result<IntGauge> =
try_create_int_gauge("near_is_validator", "Bool to denote if it is currently validating");
pub static ref RECEIVED_BYTES_PER_SECOND: near_metrics::Result<IntGauge> = try_create_int_gauge(
"Total number of chunks produced since starting this node",
)
.unwrap()
});
pub static IS_VALIDATOR: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_is_validator", "Bool to denote if it is currently validating")
.unwrap()
});
pub static RECEIVED_BYTES_PER_SECOND: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_received_bytes_per_second",
"Number of bytes per second received over the network overall"
);
pub static ref SENT_BYTES_PER_SECOND: near_metrics::Result<IntGauge> = try_create_int_gauge(
"Number of bytes per second received over the network overall",
)
.unwrap()
});
pub static SENT_BYTES_PER_SECOND: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_sent_bytes_per_second",
"Number of bytes per second sent over the network overall"
);
pub static ref BLOCKS_PER_MINUTE: near_metrics::Result<IntGauge> =
try_create_int_gauge("near_blocks_per_minute", "Blocks produced per minute");
pub static ref CPU_USAGE: near_metrics::Result<IntGauge> =
try_create_int_gauge("near_cpu_usage_ratio", "Percent of CPU usage");
pub static ref MEMORY_USAGE: near_metrics::Result<IntGauge> =
try_create_int_gauge("near_memory_usage_bytes", "Amount of RAM memory usage");
pub static ref GC_TIME: near_metrics::Result<Histogram> =
try_create_histogram("near_gc_time", "Time taken to do garbage collection");
pub static ref AVG_TGAS_USAGE: near_metrics::Result<IntGauge> = try_create_int_gauge(
"Number of bytes per second sent over the network overall",
)
.unwrap()
});
pub static BLOCKS_PER_MINUTE: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_blocks_per_minute", "Blocks produced per minute").unwrap()
});
pub static CPU_USAGE: Lazy<IntGauge> =
Lazy::new(|| try_create_int_gauge("near_cpu_usage_ratio", "Percent of CPU usage").unwrap());
pub static MEMORY_USAGE: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_memory_usage_bytes", "Amount of RAM memory usage").unwrap()
});
pub static GC_TIME: Lazy<Histogram> = Lazy::new(|| {
try_create_histogram("near_gc_time", "Time taken to do garbage collection").unwrap()
});
pub static AVG_TGAS_USAGE: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_chunk_tgas_used",
"Number of Tgas (10^12 of gas) used by the last processed chunk"
);
pub static ref CHUNKS_RECEIVING_DELAY_US: near_metrics::Result<IntGauge> = try_create_int_gauge(
"Number of Tgas (10^12 of gas) used by the last processed chunk",
)
.unwrap()
});
pub static CHUNKS_RECEIVING_DELAY_US: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_chunks_receiving_delay_us",
"Max delay between receiving a block and its chunks for several most recent blocks"
);
pub static ref BLOCKS_AHEAD_OF_HEAD: near_metrics::Result<IntGauge> = try_create_int_gauge(
"Max delay between receiving a block and its chunks for several most recent blocks",
)
.unwrap()
});
pub static BLOCKS_AHEAD_OF_HEAD: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_blocks_ahead_of_head",
"Height difference between the current head and the newest block or chunk received"
);
}
"Height difference between the current head and the newest block or chunk received",
)
.unwrap()
});
Loading

0 comments on commit 3515193

Please sign in to comment.