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.

feat(capture-logs): add has_labels and retention_days to the metrics avro record (#95684)

Frank Hamand03c0eb88f900
diff --git a/rust/capture-logs/src/endpoints/prometheus.rs b/rust/capture-logs/src/endpoints/prometheus.rs
index 6d8fb49f4fe4..96f6e0885565 100644
--- a/rust/capture-logs/src/endpoints/prometheus.rs
+++ b/rust/capture-logs/src/endpoints/prometheus.rs
@@ -192,6 +192,8 @@ pub fn write_request_to_kafka_rows(req: WriteRequest) -> (Vec<KafkaMetricRow>, u
instrumentation_scope: String::new(),
attributes: sample_attributes,
series_fingerprint,
+ has_labels: true,
+ retention_days: None,
});
}
}
diff --git a/rust/capture-logs/src/metric_record.rs b/rust/capture-logs/src/metric_record.rs
index 23fbaea2704c..2bc1257b81f2 100644
--- a/rust/capture-logs/src/metric_record.rs
+++ b/rust/capture-logs/src/metric_record.rs
@@ -52,6 +52,8 @@ pub struct KafkaMetricRow {
/// ClickHouse (which never recomputes it). Links a sample to its series at read
/// time. i64 carries the u64 hash bits to fit Avro's `long`.
pub series_fingerprint: i64,
+ pub has_labels: bool,
+ pub retention_days: Option<i32>,
}
/// Flatten an OTEL Metric into one or more KafkaMetricRow records.
@@ -338,6 +340,8 @@ fn build_number_row(
instrumentation_scope: instrumentation_scope.to_string(),
attributes,
series_fingerprint,
+ has_labels: true,
+ retention_days: None,
};
Ok((row, was_overridden))
@@ -622,6 +626,44 @@ mod tests {
row
}
+ #[test]
+ fn test_has_labels_and_retention_days_serialise_into_avro_payload() {
+ use crate::metrics_avro_schema::METRICS_AVRO_SCHEMA;
+ use apache_avro::types::Value;
+ use apache_avro::{Codec, Reader, Schema, Writer};
+
+ let mut row = build_test_row(&[]);
+ row.has_labels = false;
+ row.retention_days = Some(30);
+
+ let schema = Schema::parse_str(METRICS_AVRO_SCHEMA).expect("schema parses");
+ let mut writer = Writer::with_codec(&schema, Vec::new(), Codec::Null);
+ writer.append_ser(&row).expect("append_ser ok");
+ let payload = writer.into_inner().expect("flush ok");
+
+ let reader = Reader::new(payload.as_slice()).expect("reader ok");
+ let mut found_has_labels = None;
+ let mut found_retention_days = None;
+ for value in reader {
+ let Value::Record(fields) = value.expect("decode ok") else {
+ panic!("expected a record");
+ };
+ for (name, field_value) in fields {
+ match (name.as_str(), field_value) {
+ ("has_labels", Value::Boolean(v)) => found_has_labels = Some(v),
+ ("retention_days", Value::Union(_, inner)) => {
+ if let Value::Int(v) = *inner {
+ found_retention_days = Some(v);
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+ assert_eq!(found_has_labels, Some(false));
+ assert_eq!(found_retention_days, Some(30));
+ }
+
#[test]
fn test_exemplar_populates_trace_and_span_ids() {
let exemplar = make_exemplar(VALID_TRACE_BYTES.to_vec(), VALID_SPAN_BYTES.to_vec());
diff --git a/rust/capture-logs/src/metrics_avro_schema.rs b/rust/capture-logs/src/metrics_avro_schema.rs
index ac94392a5ec5..a1579db23aba 100644
--- a/rust/capture-logs/src/metrics_avro_schema.rs
+++ b/rust/capture-logs/src/metrics_avro_schema.rs
@@ -122,6 +122,18 @@ pub const METRICS_AVRO_SCHEMA: &str = r#"
"type": ["null", "long"],
"default": null,
"doc": "Stable per-series identity assigned at ingest (hash of metric_name, metric_type, service_name, and sorted resource/data-point attributes). Stored verbatim by ClickHouse, never recomputed. u64 bits carried as a signed long."
+ },
+ {
+ "name": "has_labels",
+ "type": "boolean",
+ "default": true,
+ "doc": "True when this record carries the series labels (attributes). ClickHouse writes the series and attribute rollups only from records with has_labels set; a record without the field counts as labelled."
+ },
+ {
+ "name": "retention_days",
+ "type": ["null", "int"],
+ "default": null,
+ "doc": "Per-row retention in days. Null when unset - ClickHouse then falls back to the batch `retention-days` header, then to its default. capture-logs always writes null; it does not evaluate team rules."
}
]
}"#;