Skip to content

Commit

Permalink
create and use response::BackendMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Mar 22, 2023
1 parent a5b0009 commit 5f18b71
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 81 deletions.
6 changes: 3 additions & 3 deletions bin/src/ctl/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ fn print_cluster_metrics(cluster_metrics: &Option<BTreeMap<String, ClusterMetric
}

if let Some(backends) = &cluster_metrics_data.backends {
for (backend_id, backend_metrics) in backends.iter() {
println!("\n{cluster_id}/{backend_id}\n--------");
let filtered = filter_metrics(backend_metrics);
for backend_metrics in backends.iter() {
println!("\n{cluster_id}/{}\n--------", backend_metrics.backend_id);
let filtered = filter_metrics(&backend_metrics.metrics);
print_gauges_and_counts(&filtered);
print_percentiles(&filtered);
}
Expand Down
47 changes: 25 additions & 22 deletions command/assets/answer_metrics.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,34 @@
}
}
},
"backends": {
"cluster_1-0": {
"bytes_in": {
"type": "COUNT",
"data": 256
},
"bytes_out": {
"type": "COUNT",
"data": 128
},
"percentiles": {
"type": "PERCENTILES",
"data": {
"samples": 42,
"p_50": 1,
"p_90": 2,
"p_99": 10,
"p_99_9": 12,
"p_99_99": 20,
"p_99_999": 22,
"p_100": 30
"backends": [
{
"backend_id": "cluster_1-0",
"metrics": {
"bytes_in": {
"type": "COUNT",
"data": 256
},
"bytes_out": {
"type": "COUNT",
"data": 128
},
"percentiles": {
"type": "PERCENTILES",
"data": {
"samples": 42,
"p_50": 1,
"p_90": 2,
"p_99": 10,
"p_99_9": 12,
"p_99_99": 20,
"p_99_999": 22,
"p_100": 30
}
}
}
}
}
]
}
}
}
Expand Down
71 changes: 32 additions & 39 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,15 @@ pub struct ClusterMetrics {
/// metric name -> metric value
pub cluster: Option<BTreeMap<String, FilteredMetrics>>,
/// backend_id -> (metric name-> metric value)
pub backends: Option<BTreeMap<String, BTreeMap<String, FilteredMetrics>>>,
pub backends: Option<Vec<BackendMetrics>>,
}

/// the metrics of a given backend
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BackendMetrics {
pub backend_id: String,
/// metric name -> metric value
pub metrics: BTreeMap<String, FilteredMetrics>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -611,13 +619,6 @@ pub struct Percentiles {
pub p_100: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BackendMetricsData {
pub bytes_in: usize,
pub bytes_out: usize,
pub percentiles: Percentiles,
}

fn socketaddr_cmp(a: &SocketAddr, b: &SocketAddr) -> Ordering {
a.ip().cmp(&b.ip()).then(a.port().cmp(&b.port()))
}
Expand Down Expand Up @@ -729,40 +730,32 @@ mod tests {
.cloned()
.collect()
),
backends: Some(
[(
String::from("cluster_1-0"),
[
(
String::from("bytes_in"),
FilteredMetrics::Count(256)
),
(
String::from("bytes_out"),
FilteredMetrics::Count(128)
),
(
String::from("percentiles"),
FilteredMetrics::Percentiles(Percentiles {
samples: 42,
p_50: 1,
p_90: 2,
p_99: 10,
p_99_9: 12,
p_99_99: 20,
p_99_999: 22,
p_100: 30,
})
)
]
.iter()
.cloned()
.collect()
)]
backends: Some(vec![BackendMetrics {
backend_id: String::from("cluster_1-0"),
metrics: [
(String::from("bytes_in"), FilteredMetrics::Count(256)),
(
String::from("bytes_out"),
FilteredMetrics::Count(128)
),
(
String::from("percentiles"),
FilteredMetrics::Percentiles(Percentiles {
samples: 42,
p_50: 1,
p_90: 2,
p_99: 10,
p_99_9: 12,
p_99_99: 20,
p_99_999: 22,
p_100: 30,
})
)
]
.iter()
.cloned()
.collect()
),
}]),
}
)]
.iter()
Expand Down
28 changes: 11 additions & 17 deletions lib/src/metrics/local_drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::BTreeMap, str, time::Instant};

use anyhow::Context;
use hdrhistogram::Histogram;
use sozu_command::response::BackendMetrics;

use crate::sozu_command::{
request::{MetricsConfiguration, QueryMetricsOptions},
Expand Down Expand Up @@ -85,12 +86,6 @@ pub fn histogram_to_percentiles(hist: &Histogram<u32>) -> Percentiles {
}
}

#[derive(Clone, Debug)]
pub struct BackendMetrics {
pub cluster_id: String,
pub data: BTreeMap<String, AggregatedMetric>,
}

#[derive(Copy, Clone, Debug, PartialEq)]
enum MetricKind {
Gauge,
Expand Down Expand Up @@ -283,14 +278,14 @@ impl LocalDrain {
.map(|entry| (entry.0.to_owned(), entry.1.to_filtered()))
.collect::<BTreeMap<String, FilteredMetrics>>();

let mut backends = BTreeMap::new();
let mut backends = Vec::new();
for backend_id in self.get_backend_ids(cluster_id) {
let backend_metrics = self
.metrics_of_one_backend(&backend_id, metric_names)
.context(format!(
"Could not retrieve metrics for backend {backend_id} of cluster {cluster_id}"
))?;
backends.insert(backend_id.to_owned(), backend_metrics);
backends.push(backend_metrics);
}
Ok(ClusterMetrics {
cluster: Some(cluster),
Expand All @@ -302,7 +297,7 @@ impl LocalDrain {
&self,
backend_id: &str,
metric_names: &Vec<String>,
) -> anyhow::Result<BTreeMap<String, FilteredMetrics>> {
) -> anyhow::Result<BackendMetrics> {
let backend_metrics = self
.cluster_metrics
.get(backend_id)
Expand All @@ -320,7 +315,10 @@ impl LocalDrain {
.map(|entry| (entry.0.to_owned(), entry.1.to_filtered()))
.collect::<BTreeMap<String, FilteredMetrics>>();

Ok(filtered_backend_metrics)
Ok(BackendMetrics {
backend_id: backend_id.to_owned(),
metrics: filtered_backend_metrics,
})
}

fn query_clusters(
Expand Down Expand Up @@ -359,18 +357,14 @@ impl LocalDrain {
.context(format!("No metrics found for backend with id {backend_id}"))?
.to_owned();

let mut backend_map: BTreeMap<String, BTreeMap<String, FilteredMetrics>> =
BTreeMap::new();
backend_map.insert(
backend_id.to_owned(),
self.metrics_of_one_backend(backend_id, metric_names)?,
);
let mut backend_vec = Vec::new();
backend_vec.push(self.metrics_of_one_backend(backend_id, metric_names)?);

clusters.insert(
cluster_id,
ClusterMetrics {
cluster: None,
backends: Some(backend_map),
backends: Some(backend_vec),
},
);
}
Expand Down

0 comments on commit 5f18b71

Please sign in to comment.