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: Add multi-topic support to livestream (#61706)

Jordan Mryyan33e1e188594b
diff --git a/livestream/events/kafka.go b/livestream/events/kafka.go
index 717c224423f5..77aacfc8d4f1 100644
--- a/livestream/events/kafka.go
+++ b/livestream/events/kafka.go
@@ -7,6 +7,7 @@ import (
"fmt"
"log"
"strconv"
+ "strings"
"sync/atomic"
"time"
@@ -187,8 +188,9 @@ func (c *PostHogKafkaConsumer) Consume(ctx context.Context) {
return nil
}
- if err := c.consumer.SubscribeTopics([]string{c.topic}, rebalanceCallback); err != nil {
- log.Fatalf("Failed to subscribe to topic: %v", err)
+ topics := splitTopics(c.topic)
+ if err := c.consumer.SubscribeTopics(topics, rebalanceCallback); err != nil {
+ log.Fatalf("Failed to subscribe to topics: %v", err)
}
for i := 0; i < c.parallel; i++ {
@@ -399,3 +401,13 @@ func applyKafkaConfigOverrides(config *kafka.ConfigMap, consumerConfig configs.C
_ = config.SetKey("max.poll.interval.ms", consumerConfig.MaxPollIntervalMs)
}
}
+
+func splitTopics(topic string) []string {
+ out := make([]string, 0, 2)
+ for p := range strings.SplitSeq(topic, ",") {
+ if t := strings.TrimSpace(p); t != "" {
+ out = append(out, t)
+ }
+ }
+ return out
+}
diff --git a/livestream/events/kafka_test.go b/livestream/events/kafka_test.go
index 2ad6b101245f..8ee3eeefc7dc 100644
--- a/livestream/events/kafka_test.go
+++ b/livestream/events/kafka_test.go
@@ -247,3 +247,43 @@ func TestParse_InnerTimestampOverridesWrapper(t *testing.T) {
assert.Equal(t, "2026-01-09T02:00:00.000Z", got.Timestamp)
assert.Equal(t, "$pageview", got.Event)
}
+
+func TestSplitTopics(t *testing.T) {
+ tests := []struct {
+ name string
+ input string
+ expected []string
+ }{
+ {
+ name: "single topic",
+ input: "events_topic",
+ expected: []string{"events_topic"},
+ },
+ {
+ name: "two topics",
+ input: "topic-1,topic-2",
+ expected: []string{"topic-1", "topic-2"},
+ },
+ {
+ name: "surrounding whitespace",
+ input: " a , b ",
+ expected: []string{"a", "b"},
+ },
+ {
+ name: "empty and trailing segments",
+ input: "a,,b,",
+ expected: []string{"a", "b"},
+ },
+ {
+ name: "empty string",
+ input: "",
+ expected: []string{},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equal(t, tt.expected, splitTopics(tt.input))
+ })
+ }
+}
diff --git a/livestream/events/notification_consumer.go b/livestream/events/notification_consumer.go
index 071cf105a931..fccf5fb907f9 100644
--- a/livestream/events/notification_consumer.go
+++ b/livestream/events/notification_consumer.go
@@ -46,7 +46,7 @@ func NewNotificationKafkaConsumer(
}
func (c *NotificationKafkaConsumer) Consume(ctx context.Context) {
- if err := c.consumer.SubscribeTopics([]string{c.topic}, nil); err != nil {
+ if err := c.consumer.SubscribeTopics(splitTopics(c.topic), nil); err != nil {
log.Printf("Failed to subscribe to notification topic: %v", err)
return
}
diff --git a/livestream/events/session_recording_consumer.go b/livestream/events/session_recording_consumer.go
index 6be775968f7f..4898d700b375 100644
--- a/livestream/events/session_recording_consumer.go
+++ b/livestream/events/session_recording_consumer.go
@@ -53,7 +53,7 @@ func NewSessionRecordingKafkaConsumer(
}
func (c *SessionRecordingKafkaConsumer) Consume(ctx context.Context) {
- if err := c.consumer.SubscribeTopics([]string{c.topic}, nil); err != nil {
+ if err := c.consumer.SubscribeTopics(splitTopics(c.topic), nil); err != nil {
log.Fatalf("Failed to subscribe to session recording topic: %v", err)
}
log.Printf("Session recording consumer subscribed to topic: %s", c.topic)