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.

3.8 KB
Raw Blame
import { instrumentFn, isTracingActive } from '~/common/tracing/tracing-utils'
import { logger } from '~/common/utils/logger'

import { pipelineStepDurationHistogram, recordBudgetCheckpoint } from './metrics'
import { OkResultWithContext, Pipeline, PipelineResultWithContext } from './pipeline.interface'
import { PipelineResult, PipelineResultType, isOkResult, timeout } from './results'
import { eventSpanAttributes } from './span-attributes'
import { ProcessingStep } from './steps'

export class StepPipeline<TInput, TIntermediate, TOutput, C, RPrev extends string = never, RStep extends string = never>
    implements Pipeline<TInput, TOutput, C, RPrev | RStep>
{
    private stepName: string

    constructor(
        private currentStep: (value: TIntermediate) => Promise<PipelineResult<TOutput, RStep>>,
        private previousPipeline: Pipeline<TInput, TIntermediate, C, RPrev>
    ) {
        this.stepName = currentStep.name || 'anonymousStep'
    }

    pipe<U, R2 extends string = never>(
        step: ProcessingStep<TOutput, U, R2>
    ): StepPipeline<TInput, TOutput, U, C, RPrev | RStep, R2> {
        return new StepPipeline(step, this)
    }

    async process(
        input: OkResultWithContext<TInput, C>
    ): Promise<PipelineResultWithContext<TOutput, C, RPrev | RStep>> {
        const previousResultWithContext = await this.previousPipeline.process(input)

        const previousResult = previousResultWithContext.result
        if (!isOkResult(previousResult)) {
            return {
                result: previousResult,
                context: previousResultWithContext.context,
            }
        }

        // lastStep keeps the step that spent the time; the reason names the step that never started.
        const budget = previousResultWithContext.context.budget
        if (budget?.exhausted) {
            recordBudgetCheckpoint('step', this.stepName)
            return {
                result: timeout(`budget exceeded before ${this.stepName}`),
                context: previousResultWithContext.context,
            }
        }

        const end = pipelineStepDurationHistogram.startTimer({ step_name: this.stepName, step_type: 'element' })
        const traceContext = previousResultWithContext.context.traceContext
        let currentResult: PipelineResult<TOutput, RStep>
        try {
            currentResult = await instrumentFn(
                {
                    key: this.stepName,
                    sendException: false,
                    measureTime: false,
                    summary: false,
                    attributes: isTracingActive(traceContext) ? eventSpanAttributes(previousResult.value) : undefined,
                    parentContext: traceContext,
                },
                () => this.currentStep(previousResult.value)
            )
            end({ result: PipelineResultType[currentResult.type].toLowerCase() })
        } catch (e) {
            end({ result: 'exception' })
            // The exception propagates and crashes the process; log the input's
            // origin (set at the pipeline boundary) while it is still in scope.
            logger.error('🔥', `Step ${this.stepName} threw`, {
                error: e instanceof Error ? e.message : String(e),
                stack: e instanceof Error ? e.stack : undefined,
                debugContext: previousResultWithContext.context.debugContext,
            })
            throw e
        }
        return {
            result: currentResult,
            context: {
                ...previousResultWithContext.context,
                lastStep: this.stepName,
                sideEffects: [...previousResultWithContext.context.sideEffects, ...currentResult.sideEffects],
                warnings: [...previousResultWithContext.context.warnings, ...currentResult.warnings],
            },
        }
    }
}