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.4 KB
Raw Blame
import {
    BudgetCheckpoint,
    batchBudgetCheckpointCounter,
    batchBudgetExhaustedCounter,
    pipelineRetryAttemptsHistogram,
} from './metrics'

export async function getBudgetCheckpoints(checkpoint: BudgetCheckpoint, stepName: string): Promise<number> {
    const metric = await batchBudgetCheckpointCounter.get()
    return metric.values.find((v) => v.labels.checkpoint === checkpoint && v.labels.step_name === stepName)?.value ?? 0
}

export async function getBudgetsExhausted(): Promise<number> {
    const metric = await batchBudgetExhaustedCounter.get()
    return metric.values[0]?.value ?? 0
}

/**
 * Reads the {_count, _sum} of the retry-attempts histogram for a given name/outcome.
 * _sum holds the observed attempt count (a single observe per call), _count the number of calls.
 */
export async function getRetryAttempts(name: string, outcome: string): Promise<{ count: number; sum: number } | null> {
    const metric = await pipelineRetryAttemptsHistogram.get()
    const find = (suffix: string): number | undefined =>
        metric.values.find(
            (v) =>
                v.metricName === `ingestion_pipeline_retry_attempts${suffix}` &&
                v.labels.name === name &&
                v.labels.outcome === outcome
        )?.value
    const count = find('_count')
    const sum = find('_sum')
    return count === undefined || sum === undefined ? null : { count, sum }
}