Skip to content

Commit

Permalink
feat: add metric for storage reads of missing contracts (#12317)
Browse files Browse the repository at this point in the history
  • Loading branch information
pugachAG authored Oct 25, 2024
1 parent e62f92c commit 3267b5c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions core/primitives/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion core/store/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/store/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,16 @@ pub static TRIE_MEMORY_PARTIAL_STORAGE_MISSING_VALUES_COUNT: LazyLock<IntCounter
.unwrap()
});

/// This metrics is useful to track witness contract distribution failures.
pub static STORAGE_MISSING_CONTRACTS_COUNT: LazyLock<IntCounterVec> = 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.");
Expand Down

0 comments on commit 3267b5c

Please sign in to comment.