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.

1.5 KB
Raw Blame
import { PipelineWarning } from '~/ingestion/framework/pipeline.interface'
import { drop, ok } from '~/ingestion/framework/results'
import { ProcessingStep } from '~/ingestion/framework/steps'
import { EventHeaders } from '~/types'

const MAX_DISTINCT_ID_LENGTH = 400

type ValidateEventMetadataError = {
    error: true
    cause: 'distinct_id_too_long'
    warning: PipelineWarning
}
type ValidateEventMetadataSuccess = { error: false }
type ValidateEventMetadataResult = ValidateEventMetadataSuccess | ValidateEventMetadataError

function validateEventMetadata(headers: EventHeaders): ValidateEventMetadataResult {
    const { distinct_id } = headers

    if (distinct_id && distinct_id.length > MAX_DISTINCT_ID_LENGTH) {
        return {
            error: true,
            cause: 'distinct_id_too_long',
            warning: {
                type: 'skipping_event_invalid_distinct_id',
                details: {
                    distinctId: distinct_id.substring(0, 100),
                    distinctIdLength: distinct_id.length,
                    maxLength: MAX_DISTINCT_ID_LENGTH,
                },
            },
        }
    }

    return { error: false }
}

export function createValidateEventMetadataStep<T extends { headers: EventHeaders }>(): ProcessingStep<T, T> {
    return async function validateEventMetadataStep(input) {
        const { headers } = input
        const result = validateEventMetadata(headers)
        if (result.error) {
            return drop(result.cause, [], [result.warning])
        }
        return Promise.resolve(ok(input))
    }
}