redup_bootstrap/telemetry/
metrics_config.rs1use redup_config::{Config, Report};
4
5use crate::telemetry::histogram_config::HistogramConfig;
6use crate::telemetry::load_weights::LoadWeights;
7
8#[derive(Debug, Clone, Default, Config)]
10#[config(validate)]
11pub struct MetricsConfig {
12 optimal_average_load: Option<f64>,
15 load: LoadWeights,
17 histogram: HistogramConfig,
19}
20
21impl MetricsConfig {
22 pub fn optimal_average_load(&self) -> Option<f64> {
24 self.optimal_average_load
25 }
26
27 pub fn load(&self) -> LoadWeights {
29 self.load
30 }
31
32 pub fn histogram(&self) -> &HistogramConfig {
34 &self.histogram
35 }
36
37 fn validate(&self, report: &mut Report) {
38 if self.load.denominator() == 0.0 {
39 report.error(
40 "load",
41 "The denominator of the composite load must not be zero.",
42 );
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use redup_config::{Source, load};
50
51 use super::MetricsConfig;
52
53 #[test]
54 fn defaults_apply_without_a_section() {
55 let config = MetricsConfig::default();
56 assert_eq!(config.optimal_average_load(), None);
57 assert_eq!(config.load().incoming_weight(), 1.0);
58 assert_eq!(config.histogram().seconds().len(), 13);
59 }
60
61 #[test]
62 fn section_is_read_from_the_common_file() {
63 let directory = tempfile::tempdir().expect("a temporary directory");
64 std::fs::write(
65 directory.path().join("config.yaml"),
66 "metrics:\n\
67 \x20 optimalAverageLoad: 0.7\n\
68 \x20 load:\n\
69 \x20 outgoingWeight: 0.5\n\
70 \x20 denominator: 2\n\
71 \x20 histogram:\n\
72 \x20 bucketsByUnit:\n\
73 \x20 bytes: [1, 1ki, 1Mi]\n\
74 \x20 bucketsBySubstring:\n\
75 \x20 pack_ratio: [1, 5, 25]\n",
76 )
77 .expect("the configuration file");
78 let loaded = load::<MetricsConfig>(&Source::directory(directory.path()))
79 .expect("the configuration is accepted");
80 let value = loaded.value;
81 assert_eq!(value.optimal_average_load(), Some(0.7));
82 assert_eq!(value.load().outgoing_weight(), 0.5);
83 assert_eq!(value.load().denominator(), 2.0);
84 assert_eq!(value.histogram().bytes(), &[1, 1024, 1_048_576]);
85 assert_eq!(
86 value.histogram().buckets_by_substring()["pack_ratio"],
87 &[1.0, 5.0, 25.0]
88 );
89 }
90
91 #[test]
92 fn zero_denominator_is_rejected() {
93 let directory = tempfile::tempdir().expect("a temporary directory");
94 std::fs::write(
95 directory.path().join("config.yaml"),
96 "metrics:\n load:\n denominator: 0\n",
97 )
98 .expect("the configuration file");
99 let problems = load::<MetricsConfig>(&Source::directory(directory.path()))
100 .expect_err("the configuration is rejected");
101 assert!(problems.to_string().contains("denominator"), "{problems}");
102 }
103}