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.

perf(ingestion-consumer): share each message's topic name as Arc<str> (#112467)

Paweł Ledwoń3ec1d43a1ee9
diff --git a/rust/ingestion-consumer/Cargo.toml b/rust/ingestion-consumer/Cargo.toml
index 5364e6633e4d..294da186dd23 100644
--- a/rust/ingestion-consumer/Cargo.toml
+++ b/rust/ingestion-consumer/Cargo.toml
@@ -27,7 +27,7 @@ reqwest = { version = "0.12", default-features = false, features = ["json", "rus
# main.rs): kube's HTTPS client uses rustls 0.23, which can't auto-pick a provider
# with both aws-lc-rs and ring in the tree. Matches personhog-router/cymbal.
rustls = { version = "0.23", default-features = false, features = ["ring", "std"] }
-serde = { workspace = true }
+serde = { workspace = true, features = ["rc"] }
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
diff --git a/rust/ingestion-consumer/src/batcher.rs b/rust/ingestion-consumer/src/batcher.rs
index 47b35cea6b94..fff5742498dd 100644
--- a/rust/ingestion-consumer/src/batcher.rs
+++ b/rust/ingestion-consumer/src/batcher.rs
@@ -647,7 +647,7 @@ mod tests {
fn message(partition: i32, offset: i64, key: Option<&str>) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition,
offset,
timestamp: 0,
diff --git a/rust/ingestion-consumer/src/consumer.rs b/rust/ingestion-consumer/src/consumer.rs
index 4b84d56e9506..c8d116ce2f33 100644
--- a/rust/ingestion-consumer/src/consumer.rs
+++ b/rust/ingestion-consumer/src/consumer.rs
@@ -765,6 +765,7 @@ impl IngestionConsumer {
let batch_start_ms = current_time_ms();
let mut stream = self.consumer.stream();
+ let mut current_topic: Option<Arc<str>> = None;
loop {
if accumulator.message_count() >= self.batch_size {
@@ -784,7 +785,10 @@ impl IngestionConsumer {
let poll_wait = remaining.min(Duration::from_secs(10));
match tokio::time::timeout(poll_wait, stream.next()).await {
Ok(Some(Ok(borrowed_message))) => {
- let topic = borrowed_message.topic().to_string();
+ if current_topic.as_deref() != Some(borrowed_message.topic()) {
+ current_topic = Some(Arc::from(borrowed_message.topic()));
+ }
+ let topic = Arc::clone(current_topic.as_ref().expect("set above"));
let partition = borrowed_message.partition();
let offset = borrowed_message.offset();
let kafka_ts = borrowed_message.timestamp().to_millis().unwrap_or(0);
@@ -818,7 +822,7 @@ impl IngestionConsumer {
kafka_ts,
lag_ms,
};
- let key = TopicPartition::new(topic.clone(), partition);
+ let key = TopicPartition::new(topic.to_string(), partition);
let generations_version = self.topic_offset_ledger.generations_version();
match partitions.get_mut(&key) {
Some(deliveries) => deliveries.record(
diff --git a/rust/ingestion-consumer/src/dispatcher.rs b/rust/ingestion-consumer/src/dispatcher.rs
index 4e6c4231569e..512c8c6f2594 100644
--- a/rust/ingestion-consumer/src/dispatcher.rs
+++ b/rust/ingestion-consumer/src/dispatcher.rs
@@ -1079,7 +1079,7 @@ mod tests {
fn make_msg(key: &str) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition: 0,
offset: 0,
timestamp: 0,
@@ -1102,7 +1102,7 @@ mod tests {
fn make_unkeyed_msg() -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition: 7,
offset: 42,
timestamp: 0,
diff --git a/rust/ingestion-consumer/src/grpc_transport.rs b/rust/ingestion-consumer/src/grpc_transport.rs
index d3c4be0b147c..737893f53f8e 100644
--- a/rust/ingestion-consumer/src/grpc_transport.rs
+++ b/rust/ingestion-consumer/src/grpc_transport.rs
@@ -849,7 +849,7 @@ enum StreamEnd {
fn to_proto_message(message: &SerializedKafkaMessage) -> KafkaMessage {
KafkaMessage {
- topic: message.topic.clone(),
+ topic: message.topic.to_string(),
partition: message.partition,
offset: message.offset,
timestamp: message.timestamp,
diff --git a/rust/ingestion-consumer/src/key_table.rs b/rust/ingestion-consumer/src/key_table.rs
index 5a477788e022..0260191be215 100644
--- a/rust/ingestion-consumer/src/key_table.rs
+++ b/rust/ingestion-consumer/src/key_table.rs
@@ -181,7 +181,7 @@ impl KeyTable {
.revoked_while_outstanding
.iter()
.any(|(topic, partition)| {
- *topic == message.topic && *partition == message.partition
+ *topic == *message.topic && *partition == message.partition
})
});
}
@@ -316,8 +316,7 @@ impl KeyTable {
}
let before = state.queue.len();
state.queue.retain(|queued| {
- let keep =
- !revoked.contains(&(queued.message.topic.as_str(), queued.message.partition));
+ let keep = !revoked.contains(&(&*queued.message.topic, queued.message.partition));
if !keep {
purged_bytes += queued.message.payload_bytes();
}
@@ -614,7 +613,7 @@ mod tests {
fn msg(key: &str, offset: i64) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition: 0,
offset,
timestamp: 0,
@@ -1138,7 +1137,7 @@ mod tests {
let mixed_run = || {
let mut mixed = run("t:a", &[1, 2, 3, 4]);
mixed.messages[1].partition = 1;
- mixed.messages[2].topic = "other".to_string();
+ mixed.messages[2].topic = "other".into();
mixed.messages[3].partition = 2;
mixed
};
diff --git a/rust/ingestion-consumer/src/order_sentinel.rs b/rust/ingestion-consumer/src/order_sentinel.rs
index d6bc52016bd6..b7ee97fc2ed9 100644
--- a/rust/ingestion-consumer/src/order_sentinel.rs
+++ b/rust/ingestion-consumer/src/order_sentinel.rs
@@ -485,7 +485,7 @@ mod tests {
fn msg_at(partition: i32, offset: i64) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition,
offset,
timestamp: 0,
diff --git a/rust/ingestion-consumer/src/scheduler.rs b/rust/ingestion-consumer/src/scheduler.rs
index 4537c33e47db..01b5047dbeca 100644
--- a/rust/ingestion-consumer/src/scheduler.rs
+++ b/rust/ingestion-consumer/src/scheduler.rs
@@ -686,7 +686,7 @@ mod tests {
fn msg(key: &str) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition: 0,
offset: 0,
timestamp: 0,
diff --git a/rust/ingestion-consumer/src/stash.rs b/rust/ingestion-consumer/src/stash.rs
index 7d331fa881a2..6e06e156a109 100644
--- a/rust/ingestion-consumer/src/stash.rs
+++ b/rust/ingestion-consumer/src/stash.rs
@@ -220,7 +220,7 @@ mod tests {
fn msg(key: &str) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition: 0,
offset: 0,
timestamp: 0,
diff --git a/rust/ingestion-consumer/src/transport.rs b/rust/ingestion-consumer/src/transport.rs
index c9c414254918..4d3d0f203dcc 100644
--- a/rust/ingestion-consumer/src/transport.rs
+++ b/rust/ingestion-consumer/src/transport.rs
@@ -141,7 +141,7 @@ mod tests {
fn message_with_value(offset: i64, value_len: usize) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "t".to_string(),
+ topic: "t".into(),
partition: 0,
offset,
timestamp: 0,
diff --git a/rust/ingestion-consumer/src/types.rs b/rust/ingestion-consumer/src/types.rs
index 1942b17768ac..948d747581a2 100644
--- a/rust/ingestion-consumer/src/types.rs
+++ b/rust/ingestion-consumer/src/types.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
+use std::sync::Arc;
use common_kafka_consumer::{Offset, PolledMessage};
use serde::{Deserialize, Serialize};
@@ -7,7 +8,7 @@ use serde::{Deserialize, Serialize};
/// Values are raw UTF-8 strings (PostHog Kafka messages are always JSON text).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedKafkaMessage {
- pub topic: String,
+ pub topic: Arc<str>,
pub partition: i32,
pub offset: i64,
pub timestamp: i64,
diff --git a/rust/ingestion-consumer/tests/dispatcher_integration_test.rs b/rust/ingestion-consumer/tests/dispatcher_integration_test.rs
index 70fc20844ae0..25cd693b7288 100644
--- a/rust/ingestion-consumer/tests/dispatcher_integration_test.rs
+++ b/rust/ingestion-consumer/tests/dispatcher_integration_test.rs
@@ -98,7 +98,7 @@ fn fast_config() -> WorkerRegistryConfig {
fn make_msg(key: &str) -> SerializedKafkaMessage {
SerializedKafkaMessage {
- topic: "test".to_string(),
+ topic: "test".into(),
partition: 0,
offset: 0,
timestamp: 0,
diff --git a/rust/ingestion-consumer/tests/e2e_integration_test.rs b/rust/ingestion-consumer/tests/e2e_integration_test.rs
index fcc8492096d3..1aab407e424e 100644
--- a/rust/ingestion-consumer/tests/e2e_integration_test.rs
+++ b/rust/ingestion-consumer/tests/e2e_integration_test.rs
@@ -1204,7 +1204,7 @@ fn worker_msg(distinct_id: &str, seq: usize) -> SerializedKafkaMessage {
headers.insert("token".to_string(), "tok".to_string());
headers.insert("distinct_id".to_string(), distinct_id.to_string());
SerializedKafkaMessage {
- topic: "t".to_string(),
+ topic: "t".into(),
partition: 0,
offset: seq as i64,
timestamp: 0,
diff --git a/rust/ingestion-consumer/tests/grpc_transport_test.rs b/rust/ingestion-consumer/tests/grpc_transport_test.rs
index bb1c3acd090b..4e1ec5855917 100644
--- a/rust/ingestion-consumer/tests/grpc_transport_test.rs
+++ b/rust/ingestion-consumer/tests/grpc_transport_test.rs
@@ -238,7 +238,7 @@ fn msg(distinct_id: &str, offset: i64) -> SerializedKafkaMessage {
headers.insert("token".to_string(), "tok".to_string());
headers.insert("distinct_id".to_string(), distinct_id.to_string());
SerializedKafkaMessage {
- topic: "events".to_string(),
+ topic: "events".into(),
partition: 0,
offset,
timestamp: 0,