Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/dd_sketch_averages_from_histograms.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improved accuracy of ddsketch averages by using histogram's sum / count.

authors: tony-resendiz
18 changes: 17 additions & 1 deletion lib/vector-core/src/metrics/ddsketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,8 @@ impl AgentDDSketch {
/// ## Errors
///
/// Returns an error if a bucket size is greater that `u32::MAX`.
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_precision_loss)]
pub fn transform_to_sketch(mut metric: Metric) -> Result<Metric, &'static str> {
let sketch = match metric.data_mut().value_mut() {
MetricValue::Distribution { samples, .. } => {
Expand All @@ -788,10 +790,24 @@ impl AgentDDSketch {
}
Some(sketch)
}
MetricValue::AggregatedHistogram { buckets, .. } => {
MetricValue::AggregatedHistogram {
buckets,
sum,
count,
..
} => {
let delta_buckets = mem::take(buckets);
let mut sketch = AgentDDSketch::with_agent_defaults();
sketch.insert_interpolate_buckets(delta_buckets)?;

let orig_sum = *sum;
let orig_count = *count;
if orig_count > 0 {
sketch.sum = orig_sum;
sketch.count = orig_count as u32;
sketch.avg = orig_sum / orig_count as f64;
}

Some(sketch)
}
// We can't convert from any other metric value.
Expand Down
Loading