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.

5.2 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import api from 'lib/api'

import { ErrorTrackingStackFrame, ErrorTrackingStackFrameRecord, ErrorTrackingSymbolSet } from '../types'

export type KeyedStackFrameRecords = Record<ErrorTrackingStackFrameRecord['raw_id'], ErrorTrackingStackFrameRecord>

function mapStackFrameRecords(
    newRecords: ErrorTrackingStackFrameRecord[],
    initialRecords: KeyedStackFrameRecords
): KeyedStackFrameRecords {
    return newRecords.reduce(
        (frames, record) => {
            frames[record.raw_id] = record
            return frames
        },
        { ...initialRecords }
    )
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface stackFrameLogicValues {
    stackFrameRecords: KeyedStackFrameRecords
    stackFrameRecordsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface stackFrameLogicActions {
    loadForSymbolSet: (symbolSetId: ErrorTrackingSymbolSet['id']) => {
        symbolSetId: string
    }
    loadForSymbolSetFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadForSymbolSetSuccess: (
        stackFrameRecords: KeyedStackFrameRecords,
        payload?: {
            symbolSetId: string
        }
    ) => {
        stackFrameRecords: KeyedStackFrameRecords
        payload?: {
            symbolSetId: string
        }
    }
    loadFromRawIds: (rawIds: ErrorTrackingStackFrame['raw_id'][]) => {
        rawIds: string[]
    }
    loadFromRawIdsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadFromRawIdsSuccess: (
        stackFrameRecords: KeyedStackFrameRecords,
        payload?: {
            rawIds: string[]
        }
    ) => {
        stackFrameRecords: KeyedStackFrameRecords
        payload?: {
            rawIds: string[]
        }
    }
}

export type stackFrameLogicType = MakeLogicType<stackFrameLogicValues, stackFrameLogicActions>

export const stackFrameLogic = kea<stackFrameLogicType>([
    path(['components', 'Errors', 'stackFrameLogic']),

    actions({
        loadFromRawIds: (rawIds: ErrorTrackingStackFrame['raw_id'][]) => ({ rawIds }),
        loadForSymbolSet: (symbolSetId: ErrorTrackingSymbolSet['id']) => ({ symbolSetId }),
    }),

    loaders(({ values }) => ({
        stackFrameRecords: [
            {} as KeyedStackFrameRecords,
            {
                loadFromRawIds: async ({ rawIds }: { rawIds: ErrorTrackingStackFrame['raw_id'][] }) => {
                    const loadedRawIds = Object.keys(values.stackFrameRecords)
                    rawIds = rawIds.filter((rawId) => !loadedRawIds.includes(rawId))
                    if (rawIds.length === 0) {
                        return values.stackFrameRecords
                    }
                    const { results } = await api.errorTracking.stackFrames(rawIds)

                    return mapStackFrameRecords(results, values.stackFrameRecords)
                },
                loadForSymbolSet: async ({ symbolSetId }) => {
                    const { results } = await api.errorTracking.symbolSetStackFrames(symbolSetId)
                    return mapStackFrameRecords(results, values.stackFrameRecords)
                },
            },
        ],
    })),

    listeners(({ cache }) => ({
        loadFromRawIds: () => {
            const loadStartedAt = performance.now()
            cache.currentLoadStartedAt = loadStartedAt
            cache.currentLoadType = cache.hasLoadedStackFrames ? 'subsequent' : 'initial'
        },
        loadFromRawIdsSuccess: ({ stackFrameRecords }) => {
            const loadType = cache.currentLoadType ?? 'initial'
            const loadStartedAt = cache.currentLoadStartedAt
            const requestDurationMs =
                loadStartedAt !== null && loadStartedAt !== undefined
                    ? Math.round(performance.now() - loadStartedAt)
                    : null
            const mountDurationMs =
                loadType === 'initial' && cache.mountedAt ? Math.round(performance.now() - cache.mountedAt) : null
            cache.currentLoadStartedAt = null
            cache.currentLoadType = null

            const recordsWithContext = Object.values(stackFrameRecords).filter((record) => record.context)
            if (recordsWithContext.length > 0) {
                cache.hasLoadedStackFrames = true
            }

            posthog.capture('error_tracking_stack_trace_loaded', {
                load_type: loadType,
                duration_ms: requestDurationMs,
                duration_since_mount_ms: mountDurationMs,
                frame_count: recordsWithContext.length,
            })
        },
        loadFromRawIdsFailure: () => {
            cache.currentLoadStartedAt = null
            cache.currentLoadType = null
        },
    })),
    afterMount(({ cache }) => {
        cache.mountedAt = performance.now()
        cache.hasLoadedStackFrames = false
        cache.currentLoadStartedAt = null as number | null
        cache.currentLoadType = null as 'initial' | 'subsequent' | null
    }),
])