Skip to content

Commit

Permalink
reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
brayniac committed Oct 12, 2023
1 parent 3130e11 commit 0cabee4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 67 deletions.
86 changes: 19 additions & 67 deletions src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,19 @@ mod handlers {
data.push(format!("# TYPE {name} gauge\n{name} {value}"));
}
} else if any.downcast_ref::<AtomicHistogram>().is_some() {
let key = if let Ok(h) = HistogramMetric::try_from(metric.name()) {
h
} else {
continue;
};

if let Some(delta) = metrics_state.deltas.get(&key) {
let percentiles: Vec<f64> = PERCENTILES.iter().map(|p| p.1).collect();

let result = delta.percentiles(&percentiles).unwrap();

let result: Vec<(&'static str, f64, u64)> = PERCENTILES
.iter()
.zip(result.iter())
.map(|((label, percentile), (_, value))| (*label, *percentile, value.end()))
.collect();

for (_label, percentile, value) in result {
if let Some(description) = metric.description() {
data.push(format!(
"# TYPE {name} gauge\n# HELP {name} {description}\n{name}{{percentile=\"{:02}\"}} {value} {timestamp}",
percentile,
));
} else {
data.push(format!(
"# TYPE {name} gauge\n{name}{{percentile=\"{:02}\"}} {value} {timestamp}",
percentile,
));
}
let percentiles = metrics_state.percentiles(metric.name());

for (_label, percentile, value) in percentiles {
if let Some(description) = metric.description() {
data.push(format!(
"# TYPE {name} gauge\n# HELP {name} {description}\n{name}{{percentile=\"{:02}\"}} {value} {timestamp}",
percentile,
));
} else {
data.push(format!(
"# TYPE {name} gauge\n{name}{{percentile=\"{:02}\"}} {value} {timestamp}",
percentile,
));
}
}
}
Expand Down Expand Up @@ -239,26 +223,10 @@ mod handlers {

data.push(format!("\"{name}\": {value}"));
} else if any.downcast_ref::<AtomicHistogram>().is_some() {
let key = if let Ok(h) = HistogramMetric::try_from(metric.name()) {
h
} else {
continue;
};

if let Some(delta) = metrics_state.deltas.get(&key) {
let percentiles: Vec<f64> = PERCENTILES.iter().map(|p| p.1).collect();

let result = delta.percentiles(&percentiles).unwrap();

let result: Vec<(&'static str, f64, u64)> = PERCENTILES
.iter()
.zip(result.iter())
.map(|((label, percentile), (_, value))| (*label, *percentile, value.end()))
.collect();
let percentiles = metrics_state.percentiles(metric.name());

for (label, _percentile, value) in result {
data.push(format!("\"{name}/{label}\": {value}",));
}
for (label, _percentile, value) in percentiles {
data.push(format!("\"{name}/{label}\": {value}",));
}
}
}
Expand Down Expand Up @@ -307,26 +275,10 @@ mod handlers {

data.push(format!("\"{name}\": {value}"));
} else if any.downcast_ref::<AtomicHistogram>().is_some() {
let key = if let Ok(h) = HistogramMetric::try_from(metric.name()) {
h
} else {
continue;
};

if let Some(delta) = metrics_state.deltas.get(&key) {
let percentiles: Vec<f64> = PERCENTILES.iter().map(|p| p.1).collect();
let percentiles = metrics_state.percentiles(metric.name());

let result = delta.percentiles(&percentiles).unwrap();

let result: Vec<(&'static str, f64, u64)> = PERCENTILES
.iter()
.zip(result.iter())
.map(|((label, percentile), (_, value))| (*label, *percentile, value.end()))
.collect();

for (label, _percentile, value) in result {
data.push(format!("\"{name}/{label}\": {value}",));
}
for (label, _percentile, value) in percentiles {
data.push(format!("\"{name}/{label}\": {value}",));
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ impl HistogramMetricsSnapshot {

self.previous = current;
}

pub fn percentiles(&self, metric: &str) -> Vec<(String, f64, u64)> {
let mut result = Vec::new();

let metric = if let Ok(h) = HistogramMetric::try_from(metric) {
h
} else {
return result;
};

let percentiles: Vec<f64> = PERCENTILES
.iter()
.map(|(_, percentile)| *percentile)
.collect();

if let Some(snapshot) = self.deltas.get(&metric) {
if let Ok(percentiles) = snapshot.percentiles(&percentiles) {
for ((label, _), (percentile, bucket)) in PERCENTILES.iter().zip(percentiles.iter())
{
result.push((label.to_string(), *percentile, bucket.end()));
}
}
}

result
}
}

#[derive(Default)]
Expand Down

0 comments on commit 0cabee4

Please sign in to comment.