Skip to content

Commit

Permalink
Merge pull request #2 from ermakov-oleg/fix-metrics
Browse files Browse the repository at this point in the history
Fix prometheus metrics
  • Loading branch information
ermakov-oleg authored May 17, 2024
2 parents 1e7bb20 + 4699d02 commit e13c269
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "redis-keys-stats"
version = "0.3.0"
version = "0.3.1"
authors = ["Ermakov Oleg <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
12 changes: 4 additions & 8 deletions src/prometheus.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,10 +16,7 @@ lazy_static! {
static ref KEYS_COUNT_BY_PREFIX: Family<KeyPrefixLabel, Gauge> =
Family::<KeyPrefixLabel, Gauge>::default();
static ref ALL_KEYS_COUNT: Family<DsnLabel, Gauge> = Family::<DsnLabel, Gauge>::default();
static ref SCAN_DURATION: Family<DsnLabel, Histogram> =
Family::<DsnLabel, Histogram>::new_with_constructor(|| Histogram::new(
exponential_buckets(10.0, 1.5, 20)
));
static ref SCAN_DURATION: Family<DsnLabel, Gauge> = Family::<DsnLabel, Gauge>::default();
static ref REGISTRY: Registry = make_registry();
}

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)))
}
13 changes: 6 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit e13c269

Please sign in to comment.