feat(telemetry): tidy ups for stats calcs (#25391)

- moved num samples for cpu/mem to be usize from u64
- generic float function to round to 2 decimal places added
- removed unnecessary cast of f32 to f64
praveen/authorization-change
praveen-influx 2024-09-25 14:13:00 +01:00 committed by GitHub
parent 29daa8332d
commit c4514bf401
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 20 deletions

View File

@ -1,6 +1,6 @@
use std::{sync::Arc, time::Duration};
use observability_deps::tracing::{debug, error};
use observability_deps::tracing::debug;
use crate::store::{TelemetryPayload, TelemetryStore};
use crate::{Result, TelemetryError};
@ -45,7 +45,9 @@ pub(crate) async fn send_telemetry_in_background(
interval.tick().await;
let telemetry = store.snapshot();
if let Err(e) = telem_sender.try_sending(&telemetry).await {
error!(error = ?e, "Cannot send telemetry");
// TODO: change to error! - until endpoint is decided keep
// this as debug log
debug!(error = ?e, "Cannot send telemetry");
}
// if we tried sending and failed, we currently still reset the
// metrics, it is ok to miss few samples

View File

@ -4,7 +4,7 @@ pub(crate) fn stats<T: Num + Copy + NumCast + PartialOrd>(
current_min: T,
current_max: T,
current_avg: T,
current_num_samples: u64,
current_num_samples: usize,
new_value: T,
) -> Option<(T, T, T)> {
let min = min(current_min, new_value);
@ -18,7 +18,7 @@ pub(crate) fn stats<T: Num + Copy + NumCast + PartialOrd>(
/// is fine as we don't really need it to be a precise average.
/// For example, memory consumed measured in MB can be rounded as u64
pub(crate) fn avg<T: Num + Copy + NumCast>(
current_num_samples: u64,
current_num_samples: usize,
current_avg: T,
new_value: T,
) -> Option<T> {
@ -90,14 +90,14 @@ mod tests {
#[test_log::test(test)]
fn avg_float_test_max() {
let avg_floats = avg(u64::MAX, 2.0, 4.0);
let avg_floats = avg(usize::MAX, 2.0, 4.0);
info!(avg = ?avg_floats, "average float");
assert_eq!(None, avg_floats);
}
#[test_log::test(test)]
fn avg_num_test_max() {
let avg_nums = avg(u64::MAX, 2, 4);
let avg_nums = avg(usize::MAX, 2, 4);
assert_eq!(None, avg_nums);
}
@ -116,7 +116,7 @@ mod tests {
min in 0u64..10000,
max in 0u64..10000,
curr_avg in 0u64..10000,
num_samples in 0u64..10000,
num_samples in 0usize..10000,
new_value in 0u64..100000,
) {
stats(min, max, curr_avg, num_samples, new_value);
@ -127,7 +127,7 @@ mod tests {
min in 0.0f32..10000.0,
max in 0.0f32..10000.0,
curr_avg in 0.0f32..10000.0,
num_samples in 0u64..10000,
num_samples in 0usize..10000,
new_value in 0.0f32..100000.0,
) {
stats(min, max, curr_avg, num_samples, new_value);
@ -138,7 +138,7 @@ mod tests {
min in 0.0f64..10000.0,
max in 0.0f64..10000.0,
curr_avg in 0.0f64..10000.0,
num_samples in 0u64..10000,
num_samples in 0usize..10000,
new_value in 0.0f64..100000.0,
) {
stats(min, max, curr_avg, num_samples, new_value);

View File

@ -1,5 +1,6 @@
use std::{sync::Arc, time::Duration};
use num::Float;
use observability_deps::tracing::{debug, warn};
use serde::Serialize;
@ -71,15 +72,15 @@ struct TelemetryStoreInner {
storage_type: Arc<str>,
cores: usize,
cpu_utilization_percent_min: f64,
cpu_utilization_percent_max: f64,
cpu_utilization_percent_avg: f64,
cpu_utilization_percent_min: f32,
cpu_utilization_percent_max: f32,
cpu_utilization_percent_avg: f32,
memory_used_mb_min: u64,
memory_used_mb_max: u64,
memory_used_mb_avg: u64,
num_samples_cpu_mem: u64,
num_samples_cpu_mem: usize,
}
impl TelemetryStoreInner {
@ -169,8 +170,7 @@ impl TelemetryStoreInner {
Some(())
}
fn add_cpu_utilization(&mut self, value: f32) -> Option<()> {
let cpu_used: f64 = value.into();
fn add_cpu_utilization(&mut self, cpu_used: f32) -> Option<()> {
let (min, max, avg) = if self.num_samples_cpu_mem == 0 {
(cpu_used, cpu_used, cpu_used)
} else {
@ -189,8 +189,9 @@ impl TelemetryStoreInner {
}
}
fn to_2_decimal_places(avg: f64) -> f64 {
(avg * 100.0).round() / 100.0
fn to_2_decimal_places<T: Float>(avg: T) -> T {
let hundred_float = num::cast(100.0).unwrap();
(avg * hundred_float).round() / hundred_float
}
#[derive(Serialize, Debug)]
@ -202,9 +203,9 @@ pub(crate) struct TelemetryPayload {
pub cores: usize,
pub product_type: &'static str,
// cpu
pub cpu_utilization_percent_min: f64,
pub cpu_utilization_percent_max: f64,
pub cpu_utilization_percent_avg: f64,
pub cpu_utilization_percent_min: f32,
pub cpu_utilization_percent_max: f32,
pub cpu_utilization_percent_avg: f32,
// mem
pub memory_used_mb_min: u64,
pub memory_used_mb_max: u64,