Skip to content

Commit

Permalink
re: Remove usage of lazy_static! in runtime (#5367)
Browse files Browse the repository at this point in the history
Let's remove usage of `lazy_static!` in runtime.

Part of #531
  • Loading branch information
pmnoxx authored Nov 24, 2021
1 parent 3515193 commit 18558c5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 68 deletions.
2 changes: 1 addition & 1 deletion 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 runtime/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serde_json = "1.0"
log = "0.4"
tracing = "0.1"
rand = "0.7"
lazy_static = "1.4"
once_cell = "1.5.2"
num-rational = "0.3"
num-bigint = "0.3"
num-traits = "0.2.11"
Expand Down
24 changes: 12 additions & 12 deletions runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Runtime {
) -> Result<(Receipt, ExecutionOutcomeWithId), RuntimeError> {
let _span =
tracing::debug_span!(target: "runtime", "Runtime::process_transaction").entered();
near_metrics::inc_counter(&metrics::TRANSACTION_PROCESSED_TOTAL);
metrics::TRANSACTION_PROCESSED_TOTAL.inc();

match verify_and_charge_transaction(
&apply_state.config,
Expand All @@ -236,7 +236,7 @@ impl Runtime {
apply_state.current_protocol_version,
) {
Ok(verification_result) => {
near_metrics::inc_counter(&metrics::TRANSACTION_PROCESSED_SUCCESSFULLY_TOTAL);
metrics::TRANSACTION_PROCESSED_SUCCESSFULLY_TOTAL.inc();
state_update.commit(StateChangeCause::TransactionProcessing {
tx_hash: signed_transaction.get_hash(),
});
Expand Down Expand Up @@ -279,7 +279,7 @@ impl Runtime {
Ok((receipt, outcome))
}
Err(e) => {
near_metrics::inc_counter(&metrics::TRANSACTION_PROCESSED_FAILED_TOTAL);
metrics::TRANSACTION_PROCESSED_FAILED_TOTAL.inc();
state_update.rollback();
return Err(e);
}
Expand Down Expand Up @@ -333,7 +333,7 @@ impl Runtime {
}
match action {
Action::CreateAccount(_) => {
near_metrics::inc_counter(&metrics::ACTION_CREATE_ACCOUNT_TOTAL);
metrics::ACTION_CREATE_ACCOUNT_TOTAL.inc();
action_create_account(
&apply_state.config.transaction_costs,
&apply_state.config.account_creation_config,
Expand All @@ -345,7 +345,7 @@ impl Runtime {
);
}
Action::DeployContract(deploy_contract) => {
near_metrics::inc_counter(&metrics::ACTION_DEPLOY_CONTRACT_TOTAL);
metrics::ACTION_DEPLOY_CONTRACT_TOTAL.inc();
action_deploy_contract(
state_update,
account.as_mut().expect(EXPECT_ACCOUNT_EXISTS),
Expand All @@ -356,7 +356,7 @@ impl Runtime {
)?;
}
Action::FunctionCall(function_call) => {
near_metrics::inc_counter(&metrics::ACTION_FUNCTION_CALL_TOTAL);
metrics::ACTION_FUNCTION_CALL_TOTAL.inc();
action_function_call(
state_update,
apply_state,
Expand All @@ -374,7 +374,7 @@ impl Runtime {
)?;
}
Action::Transfer(transfer) => {
near_metrics::inc_counter(&metrics::ACTION_TRANSFER_TOTAL);
metrics::ACTION_TRANSFER_TOTAL.inc();
if let Some(account) = account.as_mut() {
action_transfer(account, transfer)?;
// Check if this is a gas refund, then try to refund the access key allowance.
Expand Down Expand Up @@ -403,7 +403,7 @@ impl Runtime {
}
}
Action::Stake(stake) => {
near_metrics::inc_counter(&metrics::ACTION_STAKE_TOTAL);
metrics::ACTION_STAKE_TOTAL.inc();
action_stake(
account.as_mut().expect(EXPECT_ACCOUNT_EXISTS),
&mut result,
Expand All @@ -416,7 +416,7 @@ impl Runtime {
)?;
}
Action::AddKey(add_key) => {
near_metrics::inc_counter(&metrics::ACTION_ADD_KEY_TOTAL);
metrics::ACTION_ADD_KEY_TOTAL.inc();
action_add_key(
apply_state,
state_update,
Expand All @@ -427,7 +427,7 @@ impl Runtime {
)?;
}
Action::DeleteKey(delete_key) => {
near_metrics::inc_counter(&metrics::ACTION_DELETE_KEY_TOTAL);
metrics::ACTION_DELETE_KEY_TOTAL.inc();
action_delete_key(
&apply_state.config.transaction_costs,
state_update,
Expand All @@ -439,7 +439,7 @@ impl Runtime {
)?;
}
Action::DeleteAccount(delete_account) => {
near_metrics::inc_counter(&metrics::ACTION_DELETE_ACCOUNT_TOTAL);
metrics::ACTION_DELETE_ACCOUNT_TOTAL.inc();
action_delete_account(
state_update,
account,
Expand All @@ -453,7 +453,7 @@ impl Runtime {
}
#[cfg(feature = "protocol_feature_chunk_only_producers")]
Action::StakeChunkOnly(stake) => {
near_metrics::inc_counter(&metrics::ACTION_STAKE_CHUNK_ONLY_TOTAL);
metrics::ACTION_STAKE_CHUNK_ONLY_TOTAL.inc();
action_stake(
account.as_mut().expect(EXPECT_ACCOUNT_EXISTS),
&mut result,
Expand Down
136 changes: 82 additions & 54 deletions runtime/runtime/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,88 @@
use near_metrics::{try_create_int_counter, IntCounter};
use once_cell::sync::Lazy;

lazy_static::lazy_static! {
pub static ref ACTION_CREATE_ACCOUNT_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_action_create_account_total",
"The number of CreateAccount actions called since starting this node"
);
pub static ref ACTION_DEPLOY_CONTRACT_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_action_deploy_contract_total",
"The number of DeployContract actions called since starting this node"
);
pub static ref ACTION_FUNCTION_CALL_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_action_function_call_total",
"The number of FunctionCall actions called since starting this node"
);
pub static ref ACTION_TRANSFER_TOTAL: near_metrics::Result<IntCounter> = try_create_int_counter(
pub static ACTION_CREATE_ACCOUNT_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_create_account_total",
"The number of CreateAccount actions called since starting this node",
)
.unwrap()
});
pub static ACTION_DEPLOY_CONTRACT_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_deploy_contract_total",
"The number of DeployContract actions called since starting this node",
)
.unwrap()
});
pub static ACTION_FUNCTION_CALL_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_function_call_total",
"The number of FunctionCall actions called since starting this node",
)
.unwrap()
});
pub static ACTION_TRANSFER_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_transfer_total",
"The number of Transfer actions called since starting this node"
);
pub static ref ACTION_STAKE_TOTAL: near_metrics::Result<IntCounter> = try_create_int_counter(
"The number of Transfer actions called since starting this node",
)
.unwrap()
});
pub static ACTION_STAKE_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_stake_total",
"The number of stake actions called since starting this node"
);
pub static ref ACTION_STAKE_CHUNK_ONLY_TOTAL: near_metrics::Result<IntCounter> = try_create_int_counter(
"The number of stake actions called since starting this node",
)
.unwrap()
});
#[cfg(feature = "protocol_feature_chunk_only_producers")]
pub static ACTION_STAKE_CHUNK_ONLY_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_stake_chunk_only_total",
"The number of chunk-only stake actions called since starting this node"
);
pub static ref ACTION_ADD_KEY_TOTAL: near_metrics::Result<IntCounter> = try_create_int_counter(
"The number of chunk-only stake actions called since starting this node",
)
.unwrap()
});
pub static ACTION_ADD_KEY_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_add_key_total",
"The number of AddKey actions called since starting this node"
);
pub static ref ACTION_DELETE_KEY_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_action_delete_key_total",
"The number of DeleteKey actions called since starting this node"
);
pub static ref ACTION_DELETE_ACCOUNT_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_action_delete_account_total",
"The number of DeleteAccount actions called since starting this node"
);
pub static ref TRANSACTION_PROCESSED_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_transaction_processed_total",
"The number of transactions processed since starting this node"
);
pub static ref TRANSACTION_PROCESSED_SUCCESSFULLY_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_transaction_processed_successfully_total",
"The number of transactions processed successfully since starting this node"
);
pub static ref TRANSACTION_PROCESSED_FAILED_TOTAL: near_metrics::Result<IntCounter> =
try_create_int_counter(
"near_transaction_processed_failed_total",
"The number of transactions processed and failed since starting this node"
);
}
"The number of AddKey actions called since starting this node",
)
.unwrap()
});
pub static ACTION_DELETE_KEY_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_delete_key_total",
"The number of DeleteKey actions called since starting this node",
)
.unwrap()
});
pub static ACTION_DELETE_ACCOUNT_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_action_delete_account_total",
"The number of DeleteAccount actions called since starting this node",
)
.unwrap()
});
pub static TRANSACTION_PROCESSED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_transaction_processed_total",
"The number of transactions processed since starting this node",
)
.unwrap()
});
pub static TRANSACTION_PROCESSED_SUCCESSFULLY_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_transaction_processed_successfully_total",
"The number of transactions processed successfully since starting this node",
)
.unwrap()
});
pub static TRANSACTION_PROCESSED_FAILED_TOTAL: Lazy<IntCounter> = Lazy::new(|| {
try_create_int_counter(
"near_transaction_processed_failed_total",
"The number of transactions processed and failed since starting this node",
)
.unwrap()
});

0 comments on commit 18558c5

Please sign in to comment.