diff --git a/Cargo.lock b/Cargo.lock index 353bd01..d45c2ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -354,7 +354,7 @@ dependencies = [ [[package]] name = "redis-keys-stats" -version = "0.3.0" +version = "0.3.1" dependencies = [ "humantime", "indicatif", diff --git a/Cargo.toml b/Cargo.toml index 25523c3..cedeec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "redis-keys-stats" -version = "0.3.0" +version = "0.3.1" authors = ["Ermakov Oleg "] edition = "2021" license = "MIT" diff --git a/src/prometheus.rs b/src/prometheus.rs index bbcfc27..f0dc547 100644 --- a/src/prometheus.rs +++ b/src/prometheus.rs @@ -1,12 +1,11 @@ use crate::config::Config; use crate::key_prefix::KeyPrefix; use crate::stats::Result; -use crate::utils::get_masked_dsn; +use crate::utils::get_dsn_host; use prometheus_client::encoding::text::encode; use prometheus_client::encoding::EncodeLabelSet; use prometheus_client::metrics::family::Family; use prometheus_client::metrics::gauge::Gauge; -use prometheus_client::metrics::histogram::{exponential_buckets, Histogram}; use prometheus_client::registry::Registry; use std::io::Cursor; use std::thread; @@ -17,10 +16,7 @@ lazy_static! { static ref KEYS_COUNT_BY_PREFIX: Family = Family::::default(); static ref ALL_KEYS_COUNT: Family = Family::::default(); - static ref SCAN_DURATION: Family = - Family::::new_with_constructor(|| Histogram::new( - exponential_buckets(10.0, 1.5, 20) - )); + static ref SCAN_DURATION: Family = Family::::default(); static ref REGISTRY: Registry = make_registry(); } @@ -49,13 +45,13 @@ pub fn start_metrics_server(port: u16) -> JoinHandle<()> { pub fn update_metrics(config: &Config, result: &Result) { KEYS_COUNT_BY_PREFIX.clear(); - let dsn = get_masked_dsn(&config.dsn); + let dsn = get_dsn_host(&config.dsn); let dsn_label = DsnLabel { dsn: dsn.clone() }; SCAN_DURATION .get_or_create(&dsn_label) - .observe(result.took.as_secs_f64()); + .set(result.took.as_secs() as i64); ALL_KEYS_COUNT .get_or_create(&dsn_label) .set(result.root_prefix.keys_count as i64); diff --git a/src/stats.rs b/src/stats.rs index 38cd7e1..0a43922 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -5,7 +5,7 @@ use indicatif::{ProgressBar, ProgressStyle}; use crate::config::Config; use crate::key_prefix::KeyPrefix; -use crate::utils::get_masked_dsn; +use crate::utils::get_dsn_host; /// The result of the analysis pub struct Result { @@ -74,8 +74,8 @@ fn analyze_count(config: &mut Config, prefix: &mut KeyPrefix) { /// Connect to redis fn connect_redis(dsn: &str) -> redis::Connection { let client = redis::Client::open(dsn) - .unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_masked_dsn(dsn))); + .unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_dsn_host(dsn))); client .get_connection() - .unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_masked_dsn(dsn))) + .unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_dsn_host(dsn))) } diff --git a/src/utils.rs b/src/utils.rs index f5dd2e8..9b7e036 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,7 @@ -/// Get Redis DSN with password masked -pub fn get_masked_dsn(dsn: &str) -> String { - let mut parts: Vec<&str> = dsn.split('@').collect(); - if parts.len() == 2 { - parts[0] = "redis://***:***@"; - } - parts.join("") +use redis::ConnectionInfo; + +/// Get Redis DSN host +pub fn get_dsn_host(dsn: &str) -> String { + let conn_info: ConnectionInfo = dsn.parse().expect("parsing redis url"); + return format!("{}/{}", conn_info.addr, conn_info.redis.db); }