Skip to content

Commit 479e10c

Browse files
committed
don't modify the sketch if count is bigger than u32
1 parent 70399f7 commit 479e10c

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/vector-core/src/metrics/ddsketch.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,6 @@ impl AgentDDSketch {
779779
/// ## Errors
780780
///
781781
/// Returns an error if a bucket size is greater that `u32::MAX`.
782-
#[allow(clippy::cast_possible_truncation)]
783-
#[allow(clippy::cast_precision_loss)]
784782
pub fn transform_to_sketch(mut metric: Metric) -> Result<Metric, &'static str> {
785783
let sketch = match metric.data_mut().value_mut() {
786784
MetricValue::Distribution { samples, .. } => {
@@ -794,18 +792,18 @@ impl AgentDDSketch {
794792
buckets,
795793
sum,
796794
count,
797-
..
798795
} => {
799796
let delta_buckets = mem::take(buckets);
800797
let mut sketch = AgentDDSketch::with_agent_defaults();
801798
sketch.insert_interpolate_buckets(delta_buckets)?;
802799

803-
let orig_sum = *sum;
804-
let orig_count = *count;
805-
if orig_count > 0 {
800+
if let Ok(count) = u32::try_from(*count)
801+
&& count > 0
802+
{
803+
let orig_sum = *sum;
804+
sketch.count = count;
806805
sketch.sum = orig_sum;
807-
sketch.count = orig_count as u32;
808-
sketch.avg = orig_sum / orig_count as f64;
806+
sketch.avg = orig_sum / f64::from(count);
809807
}
810808

811809
Some(sketch)

0 commit comments

Comments
 (0)