Skip to content

Commit

Permalink
metrics: return histograms for time metrics
Browse files Browse the repository at this point in the history
create local type MetricsMap
implement receive_metric function for local types
add error management
  • Loading branch information
Keksoj committed Nov 20, 2023
1 parent e2d5a2a commit aa9df5f
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 137 deletions.
19 changes: 17 additions & 2 deletions command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,15 @@ message BackendMetrics {
map<string, FilteredMetrics> metrics = 2;
}

// A metric, in a "filtered" format, which means: sendable to outside programs.
message FilteredMetrics {
oneof inner {
uint64 gauge = 1;
int64 count = 2;
uint64 time = 3;
Percentiles percentiles = 4;
FilteredTimeSerie time_serie = 5;
FilteredHistogram histogram = 6;
}
}

Expand All @@ -588,7 +590,6 @@ message FilteredTimeSerie {
repeated uint32 last_hour = 3;
}


message Percentiles {
required uint64 samples = 1;
required uint64 p_50 = 2;
Expand All @@ -600,6 +601,20 @@ message Percentiles {
required uint64 p_100 = 8;
}

// a histogram meant to be translated to prometheus
message FilteredHistogram {
required uint64 sum = 1;
required uint64 count = 2;
repeated Bucket buckets = 3;
}

// a prometheus histogram bucket
message Bucket {
required uint64 count = 1;
// upper range of the bucket (le = less or equal)
required uint64 le = 2;
}

message RequestCounts {
map<string, int32> map = 1;
}
}
34 changes: 34 additions & 0 deletions command/src/proto/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::proto::{
DisplayError,
};

use super::command::FilteredHistogram;

impl Display for CertificateAndKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let versions = self.versions.iter().fold(String::new(), |acc, tls_v| {
Expand Down Expand Up @@ -232,6 +234,7 @@ fn print_proxy_metrics(proxy_metrics: &BTreeMap<String, FilteredMetrics>) {
let filtered = filter_metrics(proxy_metrics);
print_gauges_and_counts(&filtered);
print_percentiles(&filtered);
print_histograms(&filtered);
}

fn print_worker_metrics(worker_metrics: &WorkerMetrics) -> Result<(), DisplayError> {
Expand Down Expand Up @@ -372,6 +375,37 @@ fn print_percentiles(filtered_metrics: &BTreeMap<String, FilteredMetrics>) {
percentile_table.printstd();
}

fn print_histograms(filtered_metrics: &BTreeMap<String, FilteredMetrics>) {
let histograms: BTreeMap<String, FilteredHistogram> = filtered_metrics
.iter()
.filter_map(|(name, metric)| match metric.inner.clone() {
Some(filtered_metrics::Inner::Histogram(hist)) => Some((name.to_owned(), hist)),
_ => None,
})
.collect();

for (name, histogram) in histograms {
print_histogram(&name, &histogram);
}
}

fn print_histogram(metric_name: &str, hist: &FilteredHistogram) {
println!("{}", metric_name);
let mut first_row = Row::new(vec![cell!("sum"), cell!("count")]);
let mut value_row = Row::new(vec![cell!(hist.sum), cell!(hist.count)]);

for bucket in &hist.buckets {
first_row.add_cell(cell!(bucket.le));
value_row.add_cell(cell!(bucket.count));
}

let mut table = Table::new();
table.set_format(*prettytable::format::consts::FORMAT_BOX_CHARS);
table.add_row(first_row);
table.add_row(value_row);
table.printstd();
}

fn print_available_metrics(available_metrics: &AvailableMetrics) -> Result<(), DisplayError> {
println!("Available metrics on the proxy level:");
for metric_name in &available_metrics.proxy_metrics {
Expand Down
Loading

0 comments on commit aa9df5f

Please sign in to comment.