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.

6.6 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import posthog from 'posthog-js'

import { FEATURE_FLAGS } from 'lib/constants'
import { type FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'

import type { TraceViewSurface } from './components/TraceView/traceViewLogic'
import {
    DEFAULT_SPAN_COLUMNS,
    normalizeSpanColumns,
    SpanColumnConfig,
} from './components/VirtualizedSpanList/spanColumns'
import { TRACE_VIEW_MODES, type TraceViewMode } from './types'

/** Column widths a person has dragged, keyed by table then column. */
export type TracingColumnWidths = Record<string, Record<string, number>>

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tracingConfigLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    callTreeEnabled: boolean
    columnWidths: TracingColumnWidths
    facetRailCollapsed: boolean
    spanColumns: SpanColumnConfig[]
    storedSpanColumns: SpanColumnConfig[]
    storedTraceViewMode: string
    traceViewMode: TraceViewMode
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tracingConfigLogicActions {
    resetColumnWidth: (
        tableKey: string,
        columnKey: string
    ) => {
        columnKey: string
        tableKey: string
    }
    setColumnWidth: (
        tableKey: string,
        columnKey: string,
        width: number
    ) => {
        columnKey: string
        tableKey: string
        width: number
    }
    setFacetRailCollapsed: (facetRailCollapsed: boolean) => {
        facetRailCollapsed: boolean
    }
    setSpanColumns: (columns: SpanColumnConfig[]) => {
        columns: SpanColumnConfig[]
    }
    setTraceViewMode: (
        traceViewMode: TraceViewMode,
        surface: TraceViewSurface
    ) => {
        surface: TraceViewSurface
        traceViewMode: 'calltree' | 'waterfall'
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tracingConfigLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        spanColumns: (storedSpanColumns: SpanColumnConfig[]) => SpanColumnConfig[]
        callTreeEnabled: (featureFlags: FeatureFlagsSet) => boolean
        traceViewMode: (storedTraceViewMode: string, callTreeEnabled: boolean) => TraceViewMode
    }
}

export type tracingConfigLogicType = MakeLogicType<
    tracingConfigLogicValues,
    tracingConfigLogicActions,
    Record<string, any>,
    tracingConfigLogicMeta
>

/**
 * Display-only preferences for tracing, never part of a query. The trace views read them too, also in the trace dialog
 * outside the tracing scene, so a preference holds for every trace a person opens.
 */
export const tracingConfigLogic = kea<tracingConfigLogicType>([
    path(['products', 'tracing', 'frontend', 'tracingConfigLogic']),

    connect(() => ({
        values: [featureFlagLogic, ['featureFlags']],
    })),

    actions({
        setFacetRailCollapsed: (facetRailCollapsed: boolean) => ({ facetRailCollapsed }),
        setColumnWidth: (tableKey: string, columnKey: string, width: number) => ({ tableKey, columnKey, width }),
        resetColumnWidth: (tableKey: string, columnKey: string) => ({ tableKey, columnKey }),
        setSpanColumns: (columns: SpanColumnConfig[]) => ({ columns }),
        setTraceViewMode: (traceViewMode: TraceViewMode, surface: TraceViewSurface) => ({ traceViewMode, surface }),
    }),

    reducers({
        facetRailCollapsed: [
            false,
            { persist: true },
            {
                setFacetRailCollapsed: (_, { facetRailCollapsed }) => facetRailCollapsed,
            },
        ],
        storedSpanColumns: [
            DEFAULT_SPAN_COLUMNS,
            { persist: true },
            {
                setSpanColumns: (_, { columns }) => columns,
            },
        ],
        columnWidths: [
            {} as TracingColumnWidths,
            { persist: true },
            {
                setColumnWidth: (state, { tableKey, columnKey, width }) => ({
                    ...state,
                    [tableKey]: { ...state[tableKey], [columnKey]: width },
                }),
                resetColumnWidth: (state, { tableKey, columnKey }) => {
                    const { [columnKey]: _removed, ...rest } = state[tableKey] ?? {}
                    return { ...state, [tableKey]: rest }
                },
            },
        ],
        storedTraceViewMode: [
            'waterfall' as string,
            { persist: true },
            {
                setTraceViewMode: (_, { traceViewMode }) => traceViewMode,
            },
        ],
    }),

    selectors({
        // Filtered on the way out, because the persisted value outlives the code that wrote it.
        spanColumns: [
            (s) => [s.storedSpanColumns],
            // Inline with an explicit return type, because kea-typegen cannot infer the value type
            // from a bare function reference and drops the selector from the values interface.
            (storedSpanColumns: SpanColumnConfig[]): SpanColumnConfig[] => normalizeSpanColumns(storedSpanColumns),
        ],
        callTreeEnabled: [
            (s) => [s.featureFlags],
            (featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.TRACING_TRACE_CALL_TREE],
        ],
        // The mode on screen, which the view, the span keys and the events all read. Checked on the way out for the
        // same reason, so an unknown value never reaches them. With the flag off it is the waterfall, also for a
        // person who picked the call tree while the flag was on.
        traceViewMode: [
            (s) => [s.storedTraceViewMode, s.callTreeEnabled],
            (storedTraceViewMode: string, callTreeEnabled: boolean): TraceViewMode =>
                callTreeEnabled
                    ? (TRACE_VIEW_MODES.find((mode) => mode === storedTraceViewMode) ?? 'waterfall')
                    : 'waterfall',
        ],
    }),

    listeners(({ values, selectors }) => ({
        setTraceViewMode: ({ surface }, _, __, previousState) => {
            // A click on the option that is already selected calls onChange too, and it changes nothing. With the
            // flag off the mode on screen stays the waterfall, so nothing changes either.
            if (values.traceViewMode === selectors.traceViewMode(previousState)) {
                return
            }
            // pinned: analytics event and property names, so renaming them breaks the insights built on them
            posthog.capture('tracing trace view changed', { view: values.traceViewMode, surface })
        },
    })),
])