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.

2.8 KB
Raw Blame
import { KafkaProducerRegistry } from '~/common/outputs/kafka-producer-registry'
import { KafkaProducerRegistryBuilder } from '~/common/outputs/kafka-producer-registry-builder'

import {
    INGESTION_BLACKHOLE_PRODUCER,
    INGESTION_DOWNSTREAM_PRODUCER,
    INGESTION_DOWNSTREAM_PRODUCER_CONFIG_MAP,
    INGESTION_UPSTREAM_PRODUCER,
    INGESTION_UPSTREAM_PRODUCER_CONFIG_MAP,
    ProducerName,
} from './producers'

/**
 * The consolidated UPSTREAM/DOWNSTREAM producer slots, for the analytics-family servers
 * (general, error-tracking, ingestion-api). Each of these must wire both slots' brokers in
 * charts — an unconfigured slot falls through to the schema-default broker (`kafka:9092`),
 * which is reachable in dev/hobby but not in prod, so a missing wire fails fast there.
 * The BLACKHOLE slot needs no wiring. Session replay composes its own set instead.
 *
 * Lives in its own module (not producers.ts) so that importing the producer slot
 * names/maps — which config files do at module-eval time — does not transitively pull in
 * the Kafka producer machinery (and the logger → defaultConfig cycle behind it).
 */
function createIngestionProducerRegistry(kafkaClientRack: string | undefined) {
    return new KafkaProducerRegistryBuilder(kafkaClientRack)
        .register(INGESTION_UPSTREAM_PRODUCER, INGESTION_UPSTREAM_PRODUCER_CONFIG_MAP)
        .register(INGESTION_DOWNSTREAM_PRODUCER, INGESTION_DOWNSTREAM_PRODUCER_CONFIG_MAP)
        .registerBlackhole(INGESTION_BLACKHOLE_PRODUCER)
}

type ProducerRegistryConfig = Parameters<ReturnType<typeof createIngestionProducerRegistry>['build']>[0] & {
    INGESTION_OUTPUTS_DISABLED?: boolean
}

export async function buildIngestionProducerRegistry(
    kafkaClientRack: string | undefined,
    config: ProducerRegistryConfig
): Promise<KafkaProducerRegistry<ProducerName>> {
    const builder = createIngestionProducerRegistry(kafkaClientRack)
    return config.INGESTION_OUTPUTS_DISABLED ? builder.buildBlackhole() : await builder.build(config)
}

/**
 * Lifecycle owner for the shared Kafka producer registry. `start()`
 * builds the registry with `buildIngestionProducerRegistry`; `stop()` disconnects it.
 *
 * Builds the ingestion registry (the UPSTREAM/DOWNSTREAM slots), since its
 * only owners are the analytics-family servers and the client-warnings
 * consumer that runs under their scope.
 */
export class KafkaProducerRegistryComponent {
    constructor(
        private readonly kafkaClientRack: string | undefined,
        private readonly config: ProducerRegistryConfig
    ) {}

    async start(): Promise<{ value: KafkaProducerRegistry<ProducerName>; stop: () => Promise<void> }> {
        const registry = await buildIngestionProducerRegistry(this.kafkaClientRack, this.config)
        return {
            value: registry,
            stop: () => registry.disconnectAll(),
        }
    }
}