diff --git a/core/primitives/src/errors.rs b/core/primitives/src/errors.rs index 6cf280f42e6..6383b2fd2d7 100644 --- a/core/primitives/src/errors.rs +++ b/core/primitives/src/errors.rs @@ -100,6 +100,17 @@ pub enum MissingTrieValueContext { TrieStorage, } +impl MissingTrieValueContext { + pub fn metrics_label(&self) -> &str { + match self { + Self::TrieIterator => "trie_iterator", + Self::TriePrefetchingStorage => "trie_prefetching_storage", + Self::TrieMemoryPartialStorage => "trie_memory_partial_storage", + Self::TrieStorage => "trie_storage", + } + } +} + /// Errors which may occur during working with trie storages, storing /// trie values (trie nodes and state values) by their hashes. #[derive( diff --git a/core/store/src/contract.rs b/core/store/src/contract.rs index cae007059da..10854be9250 100644 --- a/core/store/src/contract.rs +++ b/core/store/src/contract.rs @@ -1,4 +1,5 @@ -use crate::TrieStorage; +use crate::{metrics, TrieStorage}; +use near_primitives::errors::StorageError; use near_primitives::hash::CryptoHash; use near_primitives::stateless_validation::contract_distribution::CodeHash; use near_vm_runner::ContractCode; @@ -124,6 +125,12 @@ impl ContractStorage { match self.storage.retrieve_raw_bytes(&code_hash) { Ok(raw_code) => Some(ContractCode::new(raw_code.to_vec(), Some(code_hash))), + Err(StorageError::MissingTrieValue(context, _)) => { + metrics::STORAGE_MISSING_CONTRACTS_COUNT + .with_label_values(&[context.metrics_label()]) + .inc(); + None + } Err(_) => None, } } diff --git a/core/store/src/metrics.rs b/core/store/src/metrics.rs index bced4fbcbe8..8bd18411df7 100644 --- a/core/store/src/metrics.rs +++ b/core/store/src/metrics.rs @@ -561,6 +561,16 @@ pub static TRIE_MEMORY_PARTIAL_STORAGE_MISSING_VALUES_COUNT: LazyLock = LazyLock::new(|| { + try_create_int_counter_vec( + "near_storage_missing_contracts_count", + "Number of contract reads from storage resulted in MissingTrieValue error", + &["context"], + ) + .unwrap() +}); + fn export_store_stats(store: &Store, temperature: Temperature) { if let Some(stats) = store.get_store_statistics() { tracing::debug!(target:"metrics", "Exporting the db metrics for {temperature:?} store.");