: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
fix(cdp): apply auto.offset.reset to the v2 consumer topic config (#62085)
Daniel Marchuk79f042831808
diff --git a/nodejs/src/kafka/consumer/consumer-v2.test.ts b/nodejs/src/kafka/consumer/consumer-v2.test.tsindex a101ae1b29fe..df1bd9e11f5a 100644--- a/nodejs/src/kafka/consumer/consumer-v2.test.ts+++ b/nodejs/src/kafka/consumer/consumer-v2.test.ts@@ -177,6 +177,22 @@ describe('KafkaConsumerV2', () => {await delay(2)}+ // auto.offset.reset is a topic-level librdkafka property — a value only in the global config+ // is ignored, so the override must be mirrored into the topic config (2nd ctor arg).+ it.each([+ [+ 'mirrors an auto.offset.reset override into the topic config',+ { 'auto.offset.reset': 'latest' } as any,+ 'latest',+ ],+ ['defaults the topic-config auto.offset.reset to earliest with no override', undefined, 'earliest'],+ ])('%s', (_label, overrides, expected) => {+ const RdKafkaCtor = jest.mocked(RdKafkaConsumer)+ new KafkaConsumerV2({ groupId: 'g', topic: 't' }, overrides)+ const topicConfig = RdKafkaCtor.mock.calls.at(-1)![1] as any+ expect(topicConfig['auto.offset.reset']).toBe(expected)+ })+it('Smoke: connect → consume → eachBatch → offsets stored', async () => {const eachBatch = jest.fn(() => Promise.resolve({}))await startConsuming(eachBatch)diff --git a/nodejs/src/kafka/consumer/consumer-v2.ts b/nodejs/src/kafka/consumer/consumer-v2.tsindex fb2515669de1..1e6b65031b53 100644--- a/nodejs/src/kafka/consumer/consumer-v2.ts+++ b/nodejs/src/kafka/consumer/consumer-v2.ts@@ -38,6 +38,10 @@ import {const DEFAULT_BATCH_TIMEOUT_MS = 500const STATISTICS_INTERVAL_MS = 5000const LOOP_STALL_THRESHOLD_MS_DEFAULT = 60_000+// auto.offset.reset is a topic-level property and which config form librdkafka honors varies by+// version (see node-rdkafka #984), so it's applied to both the global config and the explicit+// topic config. This is the default when a consumer doesn't opt into an override.+const DEFAULT_AUTO_OFFSET_RESET = 'earliest'export type KafkaConsumerV2Config = {groupId: string@@ -142,10 +146,9 @@ export class KafkaConsumerV2 {'socket.timeout.ms': 30_000,'enable.partition.eof': this.config.enablePartitionEof ?? true,'statistics.interval.ms': STATISTICS_INTERVAL_MS,- // Newer librdkafka versions require this in the global config (the topic-config- // form passed to RdKafkaConsumer's second arg is silently ignored for assign-based- // consumers in some versions). See node-rdkafka issue #984.- ['auto.offset.reset' as keyof ConsumerGlobalConfig]: 'earliest' as never,+ // Global/default-topic-conf fallback; the resolved value (incl. any override) is+ // also applied to the explicit topic config in createConsumer.+ ['auto.offset.reset' as keyof ConsumerGlobalConfig]: DEFAULT_AUTO_OFFSET_RESET as never,...getKafkaConfigFromEnv('CONSUMER'),...rdKafkaOverrides,// Settings we don't allow callers to override.@@ -533,7 +536,14 @@ export class KafkaConsumerV2 {// === RdKafkaConsumer construction + event wiring ===private createConsumer(): RdKafkaConsumer {- const consumer = new RdKafkaConsumer(this.rdKafkaConfig, { 'auto.offset.reset': 'earliest' })+ // Mirror the resolved auto.offset.reset (incl. any override) into the explicit topic+ // config — the form librdkafka honors on our version. See DEFAULT_AUTO_OFFSET_RESET.+ const autoOffsetReset =+ (this.rdKafkaConfig['auto.offset.reset' as keyof ConsumerGlobalConfig] as+ | 'earliest'+ | 'latest'+ | undefined) ?? DEFAULT_AUTO_OFFSET_RESET+ const consumer = new RdKafkaConsumer(this.rdKafkaConfig, { 'auto.offset.reset': autoOffsetReset })consumer.on('event.log', (log) => logger.info('📝', 'kafka_consumer_v2_librdkafka_log', { log }))consumer.on('event.error', (error: LibrdKafkaError) => {