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-apm): give each folded histogram bound set its own series (#105202)

Frank Hamanda707567336f3
diff --git a/rust/capture-apm-metrics/README.md b/rust/capture-apm-metrics/README.md
index 78bb62d5f00f..f6cd0cfe7a9f 100644
--- a/rust/capture-apm-metrics/README.md
+++ b/rust/capture-apm-metrics/README.md
@@ -34,7 +34,7 @@ The cache is bounded. When the global or the per-token cap is full, a new series
Prometheus sends a classic histogram as one `_bucket` series for each bucket. Each bucket has an `le` label. Prometheus also sends `_count` and `_sum` series.
-The service converts these component samples to one native histogram row when a request contains `_sum` and the `+Inf` bucket for the same label set and timestamp. The native row has `histogram_bounds` and `histogram_counts`. It uses the same series fingerprint as an equivalent OTLP histogram.
+The service converts these component samples to one native histogram row when a request contains `_sum` and the `+Inf` bucket for the same label set and timestamp. The native row has `histogram_bounds` and `histogram_counts`. Its series fingerprint is the label fingerprint of an equivalent OTLP histogram, combined with the bound set. The storage keeps one bound set for each series and hour, so a partial bucket set and the complete bucket set of one histogram get separate series. Queries combine them by label set.
Conversion requires cumulative bucket values that are non-negative integers and do not decrease. If `_count` is present, it must equal the `+Inf` bucket. The service does not convert a family that metadata defines as a summary, counter, or gauge. Other component samples stay as normal rows. This preserves data when a sender divides a histogram across requests. Queries must combine native and normal rows by label set.
diff --git a/rust/capture-apm-metrics/src/histogram_assembly.rs b/rust/capture-apm-metrics/src/histogram_assembly.rs
index 20f53664be9a..7fb9fd6248a3 100644
--- a/rust/capture-apm-metrics/src/histogram_assembly.rs
+++ b/rust/capture-apm-metrics/src/histogram_assembly.rs
@@ -11,14 +11,20 @@
//!
//! The module converts `le` and `quantile` label values to Go's shortest float
//! format. Therefore, `le="1.0"` and `le="1"` identify the same series.
+//!
+//! The series fingerprint of a native row includes its bound set. The storage
+//! keeps one bound set for each series and hour, so a partial bucket set and
+//! the complete bucket set of one histogram must not share a series.
use std::collections::HashMap;
+use std::hash::Hasher;
use capture_logs::metric_record::{compute_series_fingerprint, KafkaMetricRow};
use chrono::{DateTime, Utc};
use metrics::counter;
use prometheus_rw_proto::prometheus::v1::{metric_metadata::MetricType, MetricMetadata};
use serde_json::json;
+use siphasher::sip::SipHasher13;
use uuid::Uuid;
const LE_LABEL: &str = "le";
@@ -94,12 +100,15 @@ pub fn fold_classic_histograms(
let resource_attributes: HashMap<String, String> =
key.resource_attributes.iter().cloned().collect();
let attributes: HashMap<String, String> = key.attributes.iter().cloned().collect();
- let series_fingerprint = compute_series_fingerprint(
- &key.base_name,
- HISTOGRAM_TYPE,
- &key.service_name,
- &resource_attributes,
- &attributes,
+ let series_fingerprint = fingerprint_with_bounds(
+ compute_series_fingerprint(
+ &key.base_name,
+ HISTOGRAM_TYPE,
+ &key.service_name,
+ &resource_attributes,
+ &attributes,
+ ),
+ &histogram.bounds,
);
folded.push(KafkaMetricRow {
uuid: Uuid::now_v7().to_string(),
@@ -146,6 +155,18 @@ pub fn fold_classic_histograms(
folded
}
+/// Combines a label fingerprint with a bound set. Bounds are written as
+/// little-endian bit patterns, so the result is identical on any host.
+pub fn fingerprint_with_bounds(fingerprint: i64, bounds: &[f64]) -> i64 {
+ let mut hasher = SipHasher13::new();
+ hasher.write(&fingerprint.to_le_bytes());
+ hasher.write(&(bounds.len() as u64).to_le_bytes());
+ for bound in bounds {
+ hasher.write(&bound.to_bits().to_le_bytes());
+ }
+ hasher.finish() as i64
+}
+
fn normalize_float_labels(rows: &mut [KafkaMetricRow]) {
for row in rows {
let mut changed = false;
diff --git a/rust/capture-apm-metrics/tests/histogram_assembly_test.rs b/rust/capture-apm-metrics/tests/histogram_assembly_test.rs
index 2c9080ec4763..edac287a69e2 100644
--- a/rust/capture-apm-metrics/tests/histogram_assembly_test.rs
+++ b/rust/capture-apm-metrics/tests/histogram_assembly_test.rs
@@ -1,6 +1,8 @@
use std::collections::HashMap;
-use capture_apm_metrics::histogram_assembly::{fold_classic_histograms, normalize_float_label};
+use capture_apm_metrics::histogram_assembly::{
+ fingerprint_with_bounds, fold_classic_histograms, normalize_float_label,
+};
use capture_logs::endpoints::prometheus::write_request_to_kafka_rows;
use capture_logs::metric_record::{flatten_metric, KafkaMetricRow};
use chrono::{Duration, Utc};
@@ -200,7 +202,14 @@ fn folded_histogram_shares_identity_with_the_otlp_path() {
let (otlp_rows, _) = flatten_metric(metric, Some(&resource), None).expect("flatten_metric ok");
assert_eq!(otlp_rows.len(), 1);
- assert_eq!(rows[0].series_fingerprint, otlp_rows[0].series_fingerprint);
+ assert_eq!(
+ rows[0].series_fingerprint,
+ fingerprint_with_bounds(
+ otlp_rows[0].series_fingerprint,
+ &otlp_rows[0].histogram_bounds
+ ),
+ "a folded row hashes the OTLP label fingerprint with its bound set"
+ );
assert_eq!(rows[0].attributes, otlp_rows[0].attributes);
assert_eq!(
rows[0].resource_attributes,
@@ -271,6 +280,45 @@ fn folds_partial_bucket_sets_when_inf_and_sum_are_present() {
assert_eq!(rows[0].count, 10);
}
+#[test]
+fn bound_sets_get_their_own_series() {
+ let timestamp = now_ms();
+ let route = &[("route", "/cart")];
+ let partial = |timestamp: i64| {
+ fold(WriteRequest {
+ timeseries: vec![
+ bucket("d", route, "0.5", vec![sample(2.0, timestamp)]),
+ bucket("d", route, "+Inf", vec![sample(10.0, timestamp)]),
+ labelled("d_sum", route, vec![sample(12.5, timestamp)]),
+ ],
+ metadata: vec![],
+ })
+ };
+ let complete = fold(WriteRequest {
+ timeseries: vec![
+ bucket("d", route, "0.5", vec![sample(2.0, timestamp)]),
+ bucket("d", route, "1", vec![sample(5.0, timestamp)]),
+ bucket("d", route, "+Inf", vec![sample(10.0, timestamp)]),
+ labelled("d_sum", route, vec![sample(12.5, timestamp)]),
+ ],
+ metadata: vec![],
+ });
+ let first = partial(timestamp);
+ let second = partial(timestamp + 60_000);
+
+ assert_eq!(first[0].histogram_bounds, vec![0.5]);
+ assert_eq!(complete[0].histogram_bounds, vec![0.5, 1.0]);
+ assert_eq!(first[0].attributes, complete[0].attributes);
+ assert_ne!(
+ first[0].series_fingerprint, complete[0].series_fingerprint,
+ "a partial bound set must not share a series with the complete set"
+ );
+ assert_eq!(
+ first[0].series_fingerprint, second[0].series_fingerprint,
+ "the same bound set keeps one series over time"
+ );
+}
+
#[test]
fn incomplete_histograms_stay_plain_rows() {
let timestamp = now_ms();