: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(capture-logs): add bytes_uncompressed_records kafka header (#63116)
Daniel Viscab44c9284cd42
diff --git a/rust/capture-logs/src/kafka.rs b/rust/capture-logs/src/kafka.rsindex d392ace4731f..4e9d61992f48 100644--- a/rust/capture-logs/src/kafka.rs+++ b/rust/capture-logs/src/kafka.rs@@ -1,5 +1,5 @@use crate::avro_schema::AVRO_SCHEMA;-use crate::log_record::KafkaLogRow;+use crate::log_record::{sum_kafka_log_row_bytes, KafkaLogRow};use crate::metric_record::KafkaMetricRow;use crate::metrics_avro_schema::METRICS_AVRO_SCHEMA;use crate::trace_record::KafkaTraceRow;@@ -342,6 +342,7 @@ impl KafkaSink {token: &str,rows: &[T],uncompressed_bytes: u64,+ records_uncompressed_bytes: Option<u64>,timestamps_overridden: u64,) -> Result<(), anyhow::Error> {let schema = Schema::parse_str(avro_schema_str)?;@@ -365,7 +366,7 @@ impl KafkaSink {timestamp: None,headers: Some({let created_at = Utc::now().to_rfc3339();- OwnedHeaders::new()+ let mut headers = OwnedHeaders::new().insert(Header {key: "token",value: Some(&token.to_string()),@@ -373,7 +374,16 @@ impl KafkaSink {.insert(Header {key: "bytes_uncompressed",value: Some(&uncompressed_bytes.to_string()),- })+ });+ // Records-based size next to the payload-based `bytes_uncompressed`, so+ // billing can compare the two before switching to the records-based value.+ if let Some(records_bytes) = records_uncompressed_bytes {+ headers = headers.insert(Header {+ key: "bytes_uncompressed_records",+ value: Some(&records_bytes.to_string()),+ });+ }+ headers.insert(Header {key: "bytes_compressed",value: Some(&payload.len().to_string()),@@ -420,6 +430,15 @@ impl KafkaSink {counter!("capture_logs_timestamps_overridden").increment(timestamps_overridden);}+ let records_uncompressed_bytes = sum_kafka_log_row_bytes(&rows);+ counter!("capture_logs_bytes_uncompressed_payload").increment(uncompressed_bytes);+ counter!("capture_logs_bytes_uncompressed_records").increment(records_uncompressed_bytes);+ if records_uncompressed_bytes > uncompressed_bytes {+ // Records sum should stay below the payload size (it excludes transport overhead);+ // billing can only switch to it if that invariant holds.+ counter!("capture_logs_records_bytes_exceed_payload").increment(1);+ }+self.write_avro_batch(&self.logs_producer,&self.logs_topic,@@ -427,6 +446,7 @@ impl KafkaSink {token,&rows,uncompressed_bytes,+ Some(records_uncompressed_bytes),timestamps_overridden,).await?;@@ -456,6 +476,7 @@ impl KafkaSink {token,&rows,uncompressed_bytes,+ None,timestamps_overridden,).await?;@@ -485,6 +506,7 @@ impl KafkaSink {token,&rows,uncompressed_bytes,+ None,timestamps_overridden,).await?;diff --git a/rust/capture-logs/src/log_record.rs b/rust/capture-logs/src/log_record.rsindex fa18f163a84a..8dfed3639952 100644--- a/rust/capture-logs/src/log_record.rs+++ b/rust/capture-logs/src/log_record.rs@@ -64,6 +64,16 @@ pub fn compute_kafka_log_row_bytes(row: &KafkaLogRow) -> i64 {i64::try_from(total).unwrap_or(i64::MAX)}+/// Sum of per-row `bytes_uncompressed` across a batch; rows without the field count as zero.+/// Feeds the `bytes_uncompressed_records` Kafka header — the records-based counterpart to the+/// payload-sized `bytes_uncompressed` header — so the two can be compared before billing+/// switches to the records-based value.+pub fn sum_kafka_log_row_bytes(rows: &[KafkaLogRow]) -> u64 {+ rows.iter()+ .map(|row| row.bytes_uncompressed.unwrap_or(0).max(0) as u64)+ .sum()+}+impl KafkaLogRow {/// Set `bytes_uncompressed` from the row's variable-length content. Consuming/// builder; the `mut self` is encapsulated and never escapes.@@ -389,6 +399,37 @@ mod tests {assert_eq!(compute_kafka_log_row_bytes(&row), 57);}+ fn sample_row_bytes() -> u64 {+ sample_row()+ .with_computed_bytes()+ .bytes_uncompressed+ .unwrap() as u64+ }++ #[test]+ fn test_sum_kafka_log_row_bytes_empty_slice_is_zero() {+ assert_eq!(sum_kafka_log_row_bytes(&[]), 0);+ }++ #[test]+ fn test_sum_kafka_log_row_bytes_treats_missing_field_as_zero() {+ let with_bytes = sample_row().with_computed_bytes();+ let without_bytes = sample_row(); // bytes_uncompressed: None+ assert_eq!(+ sum_kafka_log_row_bytes(&[with_bytes, without_bytes]),+ sample_row_bytes()+ );+ }++ #[test]+ fn test_sum_kafka_log_row_bytes_sums_all_rows() {+ let rows = [+ sample_row().with_computed_bytes(),+ sample_row().with_computed_bytes(),+ ];+ assert_eq!(sum_kafka_log_row_bytes(&rows), sample_row_bytes() * 2);+ }+#[test]fn test_compute_kafka_log_row_bytes_excludes_fixed_width_fields() {// Two rows differing only in fixed-width numeric/timestamp fields should compute