Add more store metrics

This commit is contained in:
Paul Hauner 2019-08-12 13:49:09 +10:00
parent 913ee4694e
commit 0b4a8893a4
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 51 additions and 3 deletions

View File

@ -1,4 +1,5 @@
use super::*;
use crate::metrics;
use db_key::Key;
use leveldb::database::kv::KV;
use leveldb::database::Database;
@ -62,15 +63,27 @@ impl Store for LevelDB {
fn get_bytes(&self, col: &str, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
let column_key = Self::get_key_for_col(col, key);
self.db
metrics::inc_counter(&metrics::DISK_DB_READ_COUNT);
let result = self
.db
.get(self.read_options(), column_key)
.map_err(Into::into)
.map_err(Into::into);
if let Ok(Some(bytes)) = &result {
metrics::inc_counter_by(&metrics::DISK_DB_READ_BYTES, bytes.len() as i64)
}
result
}
/// Store some `value` in `column`, indexed with `key`.
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
let column_key = Self::get_key_for_col(col, key);
metrics::inc_counter(&metrics::DISK_DB_WRITE_COUNT);
metrics::inc_counter_by(&metrics::DISK_DB_WRITE_BYTES, val.len() as i64);
self.db
.put(self.write_options(), column_key, val)
.map_err(Into::into)
@ -80,6 +93,8 @@ impl Store for LevelDB {
fn key_exists(&self, col: &str, key: &[u8]) -> Result<bool, Error> {
let column_key = Self::get_key_for_col(col, key);
metrics::inc_counter(&metrics::DISK_DB_EXISTS_COUNT);
self.db
.get(self.read_options(), column_key)
.map_err(Into::into)
@ -89,6 +104,9 @@ impl Store for LevelDB {
/// Removes `key` from `column`.
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
let column_key = Self::get_key_for_col(col, key);
metrics::inc_counter(&metrics::DISK_DB_DELETE_COUNT);
self.db
.delete(self.write_options(), column_key)
.map_err(Into::into)

View File

@ -5,7 +5,31 @@ use std::path::PathBuf;
lazy_static! {
pub static ref DISK_DB_SIZE: Result<IntGauge> =
try_create_int_gauge("database_size", "Size of the on-disk database (bytes)");
try_create_int_gauge("store_disk_db_size", "Size of the on-disk database (bytes)");
pub static ref DISK_DB_WRITE_BYTES: Result<IntCounter> = try_create_int_counter(
"store_disk_db_write_bytes",
"Number of bytes attempted to be written to the on-disk DB"
);
pub static ref DISK_DB_READ_BYTES: Result<IntCounter> = try_create_int_counter(
"store_disk_db_read_bytes",
"Number of bytes read from the on-disk DB"
);
pub static ref DISK_DB_READ_COUNT: Result<IntCounter> = try_create_int_counter(
"store_disk_db_read_count",
"Total number of reads to the on-disk DB"
);
pub static ref DISK_DB_WRITE_COUNT: Result<IntCounter> = try_create_int_counter(
"store_disk_db_write_count",
"Total number of writes to the on-disk DB"
);
pub static ref DISK_DB_EXISTS_COUNT: Result<IntCounter> = try_create_int_counter(
"store_disk_db_exists_count",
"Total number of checks if a key is in the on-disk DB"
);
pub static ref DISK_DB_DELETE_COUNT: Result<IntCounter> = try_create_int_counter(
"store_disk_db_delete_count",
"Total number of deletions from the on-disk DB"
);
}
/// Updates the global metrics registry with store-related information.

View File

@ -41,6 +41,12 @@ pub fn inc_counter(counter: &Result<IntCounter>) {
}
}
pub fn inc_counter_by(counter: &Result<IntCounter>, value: i64) {
if let Ok(counter) = counter {
counter.inc_by(value);
}
}
pub fn set_gauge(gauge: &Result<IntGauge>, value: i64) {
if let Ok(gauge) = gauge {
gauge.set(value);