Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(capture-logs): fold trailing +Inf histogram bounds on OTLP ingest (#130943)

Frank Hamande3246529276b
diff --git a/rust/capture-logs/src/metric_record.rs b/rust/capture-logs/src/metric_record.rs
index bf75b9a16688..bb0117182a9a 100644
--- a/rust/capture-logs/src/metric_record.rs
+++ b/rust/capture-logs/src/metric_record.rs
@@ -153,8 +153,10 @@ pub fn flatten_metric(
for dp in hist.data_points {
let value = dp.sum.unwrap_or(0.0);
let count = dp.count as i64;
- let bounds = dp.explicit_bounds.clone();
- let counts: Vec<i64> = dp.bucket_counts.iter().map(|c| *c as i64).collect();
+ let (bounds, counts) = drop_trailing_infinite_bounds(
+ dp.explicit_bounds.clone(),
+ dp.bucket_counts.iter().map(|c| *c as i64).collect(),
+ );
let (mut row, overridden) = build_number_row(
&metric_name,
@@ -409,6 +411,22 @@ fn hash_sorted_map(hasher: &mut SipHasher13, map: &HashMap<String, String>) {
}
}
+/// prom-client histograms with an explicit `Infinity` bucket send `+Inf` as their last
+/// bound, followed by an empty overflow bucket. Readers reject the non-finite bound.
+fn drop_trailing_infinite_bounds(
+ mut bounds: Vec<f64>,
+ mut counts: Vec<i64>,
+) -> (Vec<f64>, Vec<i64>) {
+ if counts.len() != bounds.len() + 1 {
+ return (bounds, counts);
+ }
+ while bounds.last() == Some(&f64::INFINITY) && counts.last() == Some(&0) {
+ bounds.pop();
+ counts.pop();
+ }
+ (bounds, counts)
+}
+
/// Flatten exponential histogram buckets into explicit bounds and counts.
fn flatten_exponential_buckets(
scale: i32,
@@ -760,6 +778,88 @@ mod tests {
assert_eq!(rows[0].span_id, BASE64_STANDARD.encode(VALID_SPAN_BYTES));
}
+ #[test]
+ fn test_histogram_drops_trailing_empty_infinite_bound() {
+ struct Case {
+ name: &'static str,
+ bounds: Vec<f64>,
+ counts: Vec<u64>,
+ want_bounds: Vec<f64>,
+ want_counts: Vec<i64>,
+ }
+ let cases = vec![
+ Case {
+ name: "prom-client explicit Infinity",
+ bounds: vec![16.0, 32.0, f64::INFINITY],
+ counts: vec![1, 2, 3, 0],
+ want_bounds: vec![16.0, 32.0],
+ want_counts: vec![1, 2, 3],
+ },
+ Case {
+ name: "repeated Infinity",
+ bounds: vec![16.0, f64::INFINITY, f64::INFINITY],
+ counts: vec![1, 2, 0, 0],
+ want_bounds: vec![16.0],
+ want_counts: vec![1, 2],
+ },
+ Case {
+ name: "count above Infinity unchanged",
+ bounds: vec![16.0, f64::INFINITY],
+ counts: vec![1, 2, 5],
+ want_bounds: vec![16.0, f64::INFINITY],
+ want_counts: vec![1, 2, 5],
+ },
+ Case {
+ name: "finite bounds unchanged",
+ bounds: vec![16.0, 32.0],
+ counts: vec![1, 2, 3],
+ want_bounds: vec![16.0, 32.0],
+ want_counts: vec![1, 2, 3],
+ },
+ Case {
+ name: "length mismatch unchanged",
+ bounds: vec![16.0, f64::INFINITY],
+ counts: vec![1, 2],
+ want_bounds: vec![16.0, f64::INFINITY],
+ want_counts: vec![1, 2],
+ },
+ ];
+ for Case {
+ name,
+ bounds,
+ counts,
+ want_bounds,
+ want_counts,
+ } in cases
+ {
+ let metric = Metric {
+ name: "test.histogram".to_string(),
+ description: String::new(),
+ unit: String::new(),
+ metadata: vec![],
+ data: Some(Data::Histogram(Histogram {
+ aggregation_temporality: 2,
+ data_points: vec![HistogramDataPoint {
+ attributes: vec![],
+ start_time_unix_nano: 1_700_000_000_000_000_000,
+ time_unix_nano: 1_700_000_000_000_000_000,
+ count: counts.iter().sum(),
+ sum: Some(1.0),
+ bucket_counts: counts,
+ explicit_bounds: bounds,
+ exemplars: vec![],
+ flags: 0,
+ min: None,
+ max: None,
+ }],
+ })),
+ };
+ let (rows, _) = flatten_metric(metric, None, None).expect("flatten_metric ok");
+ assert_eq!(rows[0].histogram_bounds, want_bounds, "{name}");
+ assert_eq!(rows[0].histogram_counts, want_counts, "{name}");
+ }
+ }
+
#[test]
fn test_gauge_with_no_exemplar_path_unchanged() {
// Regression guard: rows without exemplars must still set empty trace/span ids