Skip to content

Commit e13c269

Browse files
authored
Merge pull request #2 from ermakov-oleg/fix-metrics
Fix prometheus metrics
2 parents 1e7bb20 + 4699d02 commit e13c269

File tree

5 files changed

+15
-20
lines changed

5 files changed

+15
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "redis-keys-stats"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["Ermakov Oleg <[email protected]>"]
55
edition = "2021"
66
license = "MIT"

src/prometheus.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::config::Config;
22
use crate::key_prefix::KeyPrefix;
33
use crate::stats::Result;
4-
use crate::utils::get_masked_dsn;
4+
use crate::utils::get_dsn_host;
55
use prometheus_client::encoding::text::encode;
66
use prometheus_client::encoding::EncodeLabelSet;
77
use prometheus_client::metrics::family::Family;
88
use prometheus_client::metrics::gauge::Gauge;
9-
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
109
use prometheus_client::registry::Registry;
1110
use std::io::Cursor;
1211
use std::thread;
@@ -17,10 +16,7 @@ lazy_static! {
1716
static ref KEYS_COUNT_BY_PREFIX: Family<KeyPrefixLabel, Gauge> =
1817
Family::<KeyPrefixLabel, Gauge>::default();
1918
static ref ALL_KEYS_COUNT: Family<DsnLabel, Gauge> = Family::<DsnLabel, Gauge>::default();
20-
static ref SCAN_DURATION: Family<DsnLabel, Histogram> =
21-
Family::<DsnLabel, Histogram>::new_with_constructor(|| Histogram::new(
22-
exponential_buckets(10.0, 1.5, 20)
23-
));
19+
static ref SCAN_DURATION: Family<DsnLabel, Gauge> = Family::<DsnLabel, Gauge>::default();
2420
static ref REGISTRY: Registry = make_registry();
2521
}
2622

@@ -49,13 +45,13 @@ pub fn start_metrics_server(port: u16) -> JoinHandle<()> {
4945

5046
pub fn update_metrics(config: &Config, result: &Result) {
5147
KEYS_COUNT_BY_PREFIX.clear();
52-
let dsn = get_masked_dsn(&config.dsn);
48+
let dsn = get_dsn_host(&config.dsn);
5349

5450
let dsn_label = DsnLabel { dsn: dsn.clone() };
5551

5652
SCAN_DURATION
5753
.get_or_create(&dsn_label)
58-
.observe(result.took.as_secs_f64());
54+
.set(result.took.as_secs() as i64);
5955
ALL_KEYS_COUNT
6056
.get_or_create(&dsn_label)
6157
.set(result.root_prefix.keys_count as i64);

src/stats.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use indicatif::{ProgressBar, ProgressStyle};
55

66
use crate::config::Config;
77
use crate::key_prefix::KeyPrefix;
8-
use crate::utils::get_masked_dsn;
8+
use crate::utils::get_dsn_host;
99

1010
/// The result of the analysis
1111
pub struct Result {
@@ -74,8 +74,8 @@ fn analyze_count(config: &mut Config, prefix: &mut KeyPrefix) {
7474
/// Connect to redis
7575
fn connect_redis(dsn: &str) -> redis::Connection {
7676
let client = redis::Client::open(dsn)
77-
.unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_masked_dsn(dsn)));
77+
.unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_dsn_host(dsn)));
7878
client
7979
.get_connection()
80-
.unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_masked_dsn(dsn)))
80+
.unwrap_or_else(|_| panic!("Failed to connect to redis ({})", get_dsn_host(dsn)))
8181
}

src/utils.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/// Get Redis DSN with password masked
2-
pub fn get_masked_dsn(dsn: &str) -> String {
3-
let mut parts: Vec<&str> = dsn.split('@').collect();
4-
if parts.len() == 2 {
5-
parts[0] = "redis://***:***@";
6-
}
7-
parts.join("")
1+
use redis::ConnectionInfo;
2+
3+
/// Get Redis DSN host
4+
pub fn get_dsn_host(dsn: &str) -> String {
5+
let conn_info: ConnectionInfo = dsn.parse().expect("parsing redis url");
6+
return format!("{}/{}", conn_info.addr, conn_info.redis.db);
87
}

0 commit comments

Comments
 (0)