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(nodejs): count Kafka producer brokers per librdkafka state (#104398)

Paweł Ledwoń989cd1093562
diff --git a/nodejs/src/common/kafka/kafka-producer-metrics.test.ts b/nodejs/src/common/kafka/kafka-producer-metrics.test.ts
index c4e67c8d0699..bdaf75c926fd 100644
--- a/nodejs/src/common/kafka/kafka-producer-metrics.test.ts
+++ b/nodejs/src/common/kafka/kafka-producer-metrics.test.ts
@@ -2,7 +2,7 @@ import { Gauge, register } from 'prom-client'
import {
ProducerStatsTracker,
- kafkaProducerAnyBrokersDown,
+ kafkaProducerBrokers,
kafkaProducerCallbackQueueDepth,
kafkaProducerQueueBytes,
kafkaProducerQueueMaxBytes,
@@ -48,31 +48,49 @@ describe('ProducerStatsTracker', () => {
expect(await gaugeValue(kafkaProducerCallbackQueueDepth, labels)).toBe(7)
})
- it('flags any broker not UP via kafkaProducerAnyBrokersDown', async () => {
+ it('counts brokers per librdkafka state', async () => {
const tracker = new ProducerStatsTracker('DEFAULT')
tracker.track(
makeStats({
brokers: {
- 'kafka-1:9092/1': { state: 'UP' },
- 'kafka-2:9092/2': { state: 'DOWN' },
+ 'kafka-1:9092/1': { nodeid: 1, state: 'UP' },
+ 'kafka-2:9092/2': { nodeid: 2, state: 'UP' },
+ 'kafka-3:9092/3': { nodeid: 3, state: 'DOWN' },
+ 'kafka-4:9092/4': { nodeid: 4, state: 'INIT' },
},
})
)
- expect(await gaugeValue(kafkaProducerAnyBrokersDown, { producer_name: 'DEFAULT' })).toBe(1)
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'UP' })).toBe(2)
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'DOWN' })).toBe(1)
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'INIT' })).toBe(1)
})
- it('reports zero when all known brokers are UP', async () => {
+ it('excludes bootstrap entries from broker counts', async () => {
const tracker = new ProducerStatsTracker('DEFAULT')
tracker.track(
makeStats({
- brokers: { 'kafka-1:9092/1': { state: 'UP' }, 'kafka-2:9092/2': { state: 'UP' } },
+ brokers: {
+ 'kafka-1:9092/bootstrap': { nodeid: -1, state: 'DOWN' },
+ 'kafka-1:9092/1': { nodeid: 1, state: 'UP' },
+ },
})
)
- expect(await gaugeValue(kafkaProducerAnyBrokersDown, { producer_name: 'DEFAULT' })).toBe(0)
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'UP' })).toBe(1)
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'DOWN' })).toBeUndefined()
+ })
+
+ it('resets a state to zero once no broker is in it any more', async () => {
+ const tracker = new ProducerStatsTracker('DEFAULT')
+
+ tracker.track(makeStats({ brokers: { 'kafka-1:9092/1': { nodeid: 1, state: 'DOWN' } } }))
+ tracker.track(makeStats({ brokers: { 'kafka-1:9092/1': { nodeid: 1, state: 'UP' } } }))
+
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'DOWN' })).toBe(0)
+ expect(await gaugeValue(kafkaProducerBrokers, { producer_name: 'DEFAULT', state: 'UP' })).toBe(1)
})
it('sets per-topic batching gauges', async () => {
diff --git a/nodejs/src/common/kafka/kafka-producer-metrics.ts b/nodejs/src/common/kafka/kafka-producer-metrics.ts
index ecfe6740cc8c..de4a4ed8d28a 100644
--- a/nodejs/src/common/kafka/kafka-producer-metrics.ts
+++ b/nodejs/src/common/kafka/kafka-producer-metrics.ts
@@ -34,10 +34,10 @@ export const kafkaProducerCallbackQueueDepth = new Gauge({
labelNames: ['producer_name'],
})
-export const kafkaProducerAnyBrokersDown = new Gauge({
- name: 'kafka_producer_any_brokers_down',
- help: '1 if any broker the producer knows about is not in the UP state, 0 otherwise.',
- labelNames: ['producer_name'],
+export const kafkaProducerBrokers = new Gauge({
+ name: 'kafka_producer_brokers',
+ help: 'Number of brokers the producer knows about, by librdkafka connection state (INIT, DOWN, UP, ...). Bootstrap entries are excluded.',
+ labelNames: ['producer_name', 'state'],
})
export const kafkaProducerTopicBatchSizeBytesAvg = new Gauge({
@@ -61,6 +61,8 @@ export const kafkaProducerTopicBatchCountAvg = new Gauge({
*/
export class ProducerStatsTracker {
private producerName: string
+ /** States reported at least once, so a state no broker is in any more is reset to zero rather than left stale. */
+ private reportedBrokerStates = new Set<string>()
constructor(producerName: string) {
this.producerName = producerName
@@ -107,10 +109,23 @@ export class ProducerStatsTracker {
}
if (stats.brokers) {
- const brokers = Object.values(stats.brokers)
- if (brokers.length > 0) {
- const anyDown = brokers.some((b) => b.state !== 'UP')
- kafkaProducerAnyBrokersDown.set(labels, anyDown ? 1 : 0)
+ const countsByState = new Map<string, number>()
+ for (const broker of Object.values(stats.brokers)) {
+ // librdkafka lists configured bootstrap servers with nodeid -1 next to the learned brokers.
+ const isBootstrap = broker.nodeid !== undefined && broker.nodeid < 0
+ if (broker.state === undefined || isBootstrap) {
+ continue
+ }
+ countsByState.set(broker.state, (countsByState.get(broker.state) ?? 0) + 1)
+ }
+ for (const state of this.reportedBrokerStates) {
+ if (!countsByState.has(state)) {
+ kafkaProducerBrokers.set({ ...labels, state }, 0)
+ }
+ }
+ for (const [state, count] of countsByState) {
+ kafkaProducerBrokers.set({ ...labels, state }, count)
+ this.reportedBrokerStates.add(state)
}
}
diff --git a/nodejs/src/common/kafka/kafka-producer-stats-schema.ts b/nodejs/src/common/kafka/kafka-producer-stats-schema.ts
index 331eeae6de98..fd1e430f00ad 100644
--- a/nodejs/src/common/kafka/kafka-producer-stats-schema.ts
+++ b/nodejs/src/common/kafka/kafka-producer-stats-schema.ts
@@ -13,6 +13,7 @@ import { z } from 'zod'
*/
const brokerStatsSchema = z.object({
+ nodeid: z.number().optional(),
state: z.string().optional(),
})