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.

4.0 KB
Raw Blame
import { Counter } from 'prom-client'

import { OVERFLOW_OUTPUT, OverflowOutput } from '~/common/outputs'
import { EventIngestionRestrictionManager, RestrictionType } from '~/common/utils/event-ingestion-restrictions'
import { IngestionOverflowMode } from '~/ingestion/config'
import { dlq, drop, ok, redirect } from '~/ingestion/framework/results'
import { ProcessingStep } from '~/ingestion/framework/steps'
import { EventHeaders } from '~/types'

export const ingestionOverflowingMessagesTotal = new Counter({
    name: 'ingestion_overflowing_messages_total',
    help: 'Count of messages rerouted to the overflow topic.',
})

export type RoutingConfig = {
    overflowMode: IngestionOverflowMode
    preservePartitionLocality: boolean
    /**
     * Whether this pipeline writes persons (creates or updates rows), as
     * opposed to only reading them or not touching persons at all.
     *
     * True keeps the partition key on a force-overflow redirect while person
     * processing is on, because the overflow consumer would otherwise write
     * the same person row from several partitions at once. False lets those
     * redirects follow `preservePartitionLocality`, so the overflow lane can
     * spread the load.
     *
     * Required, so that a new pipeline has to answer it rather than inherit
     * a default. The answer is the repository the pipeline holds: a
     * `PersonRepository` writes, a `PersonReadRepository` does not, and a
     * pipeline that takes neither does not touch persons at all.
     */
    pipelineWritesPersons: boolean
}

/**
 * Drop + DLQ token restrictions only, no overflow handling. For pipelines
 * that don't have an overflow topic (e.g. client warnings). Type signature
 * has no side outputs, so callers don't need to include `OverflowOutput` in
 * their pipeline's outputs union just to satisfy the step.
 */
export function createApplyBasicEventRestrictionsStep<T extends { headers: EventHeaders }>(
    manager: EventIngestionRestrictionManager
): ProcessingStep<T, T> {
    return function applyBasicEventRestrictionsStep(input) {
        const { headers } = input

        const restrictions = manager.getAppliedRestrictions(headers.token, headers)

        if (restrictions.has(RestrictionType.DROP_EVENT)) {
            return Promise.resolve(drop('blocked_token'))
        }

        if (restrictions.has(RestrictionType.REDIRECT_TO_DLQ)) {
            return Promise.resolve(dlq('restricted_to_dlq'))
        }

        return Promise.resolve(ok(input))
    }
}

export function createApplyEventRestrictionsStep<T extends { headers: EventHeaders }>(
    manager: EventIngestionRestrictionManager,
    routingConfig: RoutingConfig
): ProcessingStep<T, T, OverflowOutput> {
    return async function applyEventRestrictionsStep(input) {
        const { headers } = input

        const restrictions = manager.getAppliedRestrictions(headers.token, headers)

        if (restrictions.size === 0) {
            return Promise.resolve(ok(input))
        }

        // Priority 1: Drop
        if (restrictions.has(RestrictionType.DROP_EVENT)) {
            return drop('blocked_token')
        }

        // Priority 2: DLQ
        if (restrictions.has(RestrictionType.REDIRECT_TO_DLQ)) {
            return dlq('restricted_to_dlq')
        }

        // Priority 3: Overflow
        if (routingConfig.overflowMode === 'redirect' && restrictions.has(RestrictionType.FORCE_OVERFLOW)) {
            ingestionOverflowingMessagesTotal.inc()
            const shouldProcessPerson = !restrictions.has(RestrictionType.SKIP_PERSON_PROCESSING)
            const preservePartitionLocality =
                shouldProcessPerson && routingConfig.pipelineWritesPersons
                    ? true
                    : routingConfig.preservePartitionLocality
            return redirect(
                'Event redirected to overflow due to force overflow restrictions',
                OVERFLOW_OUTPUT,
                preservePartitionLocality,
                false
            )
        }

        return Promise.resolve(ok(input))
    }
}