Skip to content

Commit

Permalink
controller: disable metric processor memory
Browse files Browse the repository at this point in the history
This memory was changes in gauges to become stale and not be properly
represented.
  • Loading branch information
cbgbt committed Aug 9, 2023
1 parent d72f68e commit 936448d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
3 changes: 3 additions & 0 deletions controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ snafu = "0.7"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }
tracing = "0.1"
validator = { version = "0.16", features = ["derive"] }

[dev-dependencies]
maplit = "1"
2 changes: 1 addition & 1 deletion controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn main() -> Result<()> {
selectors::simple::histogram([1.0, 2.0, 5.0, 10.0, 20.0, 50.0]),
aggregation::cumulative_temporality_selector(),
)
.with_memory(true),
.with_memory(false),
)
.build();

Expand Down
91 changes: 90 additions & 1 deletion controller/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ impl BrupopHostsData {
hosts_state_count,
})
}

/// Marks all current gauges at 0, then writes the new metrics into the store.
fn update_counters(&mut self, other: &BrupopHostsData) {
update_counter(&mut self.hosts_version_count, &other.hosts_version_count);
update_counter(&mut self.hosts_state_count, &other.hosts_state_count);
}
}

/// Updates a population counter from a stateless input.
///
/// All current state in the counter is set to 0, then new counts are copied from the incoming state.
fn update_counter(base: &mut HashMap<String, u64>, other: &HashMap<String, u64>) {
base.iter_mut().for_each(|(_k, v)| *v = 0);

other.iter().for_each(|(k, v)| {
*base.entry(k.clone()).or_default() = *v;
});
}

impl BrupopControllerMetrics {
Expand Down Expand Up @@ -88,7 +105,7 @@ impl BrupopControllerMetrics {
/// Update shared mut ref to trigger ValueRecorder observe data.
pub fn emit_metrics(&self, data: BrupopHostsData) {
if let Ok(mut host_data) = self.brupop_shared_hosts_data.try_lock() {
*host_data = data;
host_data.update_counters(&data);
}
}
}
Expand All @@ -103,3 +120,75 @@ pub mod error {
SerializeState { source: serde_plain::Error },
}
}

#[cfg(test)]
mod test {
use std::collections::HashMap;

use maplit::hashmap;

use crate::metrics::update_counter;

#[test]
fn test_update_counter() {
let test_cases = vec![
(
hashmap! {
"a" => 5,
"b" => 10,
"c" => 15,
},
hashmap! {
"a" => 11,

},
hashmap! {
"a" => 11,
"b" => 0,
"c" => 0,
},
),
(
hashmap! {
"a" => 1,
},
hashmap! {
"b" => 11,
"c" => 12,
},
hashmap! {
"a" => 0,
"b" => 11,
"c" => 12,
},
),
(
hashmap! {
"a" => 1,
},
hashmap! {
"a" => 2,
},
hashmap! {
"a" => 2,
},
),
];

fn stringify(hashmap: HashMap<&str, u64>) -> HashMap<String, u64> {
hashmap
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect()
}

for (base, other, expected) in test_cases.into_iter() {
let mut base = stringify(base);
let other = stringify(other);
let expected = stringify(expected);

update_counter(&mut base, &other);
assert_eq!(&base, &expected);
}
}
}

0 comments on commit 936448d

Please sign in to comment.