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.

8.8 KB
Raw Blame
import { expectLogic } from 'kea-test-utils'

import { initKeaTests } from '~/test/init'
import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalFiltersGroup } from '~/types'

import { tracingSpansAttributeBreakdownCreate } from 'products/tracing/frontend/generated/api'
import type { _TracingAttributeBreakdownRowApi } from 'products/tracing/frontend/generated/api.schemas'
import { SpanPropertyTypeEnumApi } from 'products/tracing/frontend/generated/api.schemas'
import { tracingFiltersLogic } from 'products/tracing/frontend/tracingFiltersLogic'

import { facetRailLogic } from './facetRailLogic'
import { FACETS, FacetConfig, buildCustomFacet } from './facets'
import { facetValuesLogic } from './facetValuesLogic'

jest.mock('products/tracing/frontend/generated/api', () => ({
    __esModule: true,
    tracingSpansAttributeBreakdownCreate: jest.fn(),
    tracingSpansAttributesRetrieve: jest.fn(),
}))

const mockBreakdown = tracingSpansAttributeBreakdownCreate as jest.MockedFunction<
    typeof tracingSpansAttributeBreakdownCreate
>

const ID = 'test-viewer'

const facetConfig = (key: string): FacetConfig => FACETS.find((f) => f.key === key)!
const SERVICE = facetConfig('service')
const STATUS = facetConfig('status')

function row(value: string, count: number): _TracingAttributeBreakdownRowApi {
    return { value, count, error_count: 0, p50_duration_nano: 0, p95_duration_nano: 0 }
}

const statusFilterGroup: UniversalFiltersGroup = {
    type: FilterLogicalOperator.And,
    values: [
        {
            type: FilterLogicalOperator.And,
            values: [
                {
                    type: PropertyFilterType.Span,
                    key: 'status_code',
                    operator: PropertyOperator.Exact,
                    value: ['2'],
                },
            ] as UniversalFiltersGroup['values'],
        },
    ],
}

describe('facetValuesLogic', () => {
    let filtersLogic: ReturnType<typeof tracingFiltersLogic.build>
    let railLogic: ReturnType<typeof facetRailLogic.build>
    const mounted: ReturnType<typeof facetValuesLogic.build>[] = []

    const mountFacet = (facet: FacetConfig): ReturnType<typeof facetValuesLogic.build> => {
        const logic = facetValuesLogic({ id: ID, facet })
        logic.mount()
        mounted.push(logic)
        return logic
    }

    beforeEach(() => {
        initKeaTests()
        jest.clearAllMocks()
        mockBreakdown.mockResolvedValue({ results: [row('api', 10)], compare: null })
        filtersLogic = tracingFiltersLogic({ id: ID })
        filtersLogic.mount()
        railLogic = facetRailLogic({ id: ID })
        railLogic.mount()
    })

    afterEach(() => {
        mounted.splice(0).forEach((logic) => logic.unmount())
        railLogic.unmount()
        filtersLogic.unmount()
    })

    // Selecting a value re-scopes the *other* facets: excludeBreakdownFilter strips a facet's own
    // filter from its own breakdown, so refetching it burns a request on an unchanged response.
    it.each<[string, FacetConfig, FacetConfig, () => void]>([
        ['a service selection', SERVICE, STATUS, () => filtersLogic.actions.setServiceNames(['api'])],
        ['a status selection', STATUS, SERVICE, () => filtersLogic.actions.setFilterGroup(statusFilterGroup)],
    ])('%s reloads the other facets but not itself', async (_, selected, other, select) => {
        const selectedLogic = mountFacet(selected)
        const otherLogic = mountFacet(other)
        await expectLogic(selectedLogic).toDispatchActions(['loadFacetValuesSuccess'])
        await expectLogic(otherLogic).toDispatchActions(['loadFacetValuesSuccess'])
        mockBreakdown.mockClear()

        select()
        await expectLogic(otherLogic).toDispatchActions(['loadFacetValues', 'loadFacetValuesSuccess'])

        expect(mockBreakdown).toHaveBeenCalledTimes(1)
        expect(selectedLogic.values.facetValuesLoading).toBe(false)
    })

    it('a type-ahead search refetches this facet with the search term', async () => {
        const logic = mountFacet(SERVICE)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        mockBreakdown.mockClear()

        logic.actions.setFacetSearch('kaf')
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])

        expect(mockBreakdown).toHaveBeenCalledTimes(1)
        expect(mockBreakdown).toHaveBeenCalledWith(
            expect.any(String),
            expect.objectContaining({
                query: expect.objectContaining({ breakdownKey: 'service_name', facetSearch: 'kaf' }),
            })
        )
    })

    // Each source type must route to its own breakdownType — a binary-to-ternary slip (e.g. a
    // plain attribute silently querying as a resource attribute) would misreport values with no
    // type error, since both shapes only differ in this one field.
    it.each<[string, FacetConfig, string, SpanPropertyTypeEnumApi]>([
        ['a column facet', SERVICE, 'service_name', SpanPropertyTypeEnumApi.Span],
        [
            'a resource-attribute facet',
            facetConfig('namespace'),
            'k8s.namespace.name',
            SpanPropertyTypeEnumApi.SpanResourceAttribute,
        ],
        [
            'a custom plain-attribute facet',
            buildCustomFacet('http.status_code', 'attribute'),
            'http.status_code',
            SpanPropertyTypeEnumApi.SpanAttribute,
        ],
    ])('%s requests the matching breakdownType', async (_, facet, expectedKey, expectedType) => {
        const logic = mountFacet(facet)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        expect(mockBreakdown).toHaveBeenCalledWith(
            expect.any(String),
            expect.objectContaining({
                query: expect.objectContaining({ breakdownKey: expectedKey, breakdownType: expectedType }),
            })
        )
    })

    it('a failed breakdown shows in place and a retry refetches the same scope', async () => {
        // One broken breakdown must show on its own facet, not blank the rail.
        mockBreakdown.mockRejectedValueOnce(new Error('breakdown failed'))
        const logic = mountFacet(SERVICE)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesFailure'])
        expect(logic.values.fetchFailed).toBe(true)

        logic.actions.retryFacetValues()
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        expect(logic.values.fetchFailed).toBe(false)
        expect(logic.values.facetValues).toEqual([row('api', 10)])
        expect(mockBreakdown).toHaveBeenCalledTimes(2)
        expect(mockBreakdown.mock.calls[1]).toEqual(mockBreakdown.mock.calls[0])
    })

    it('a collapsed facet defers its fetch until it is expanded, then only if the scope moved', async () => {
        railLogic.actions.toggleFacetCollapsed(SERVICE.key)
        const logic = mountFacet(SERVICE)
        const other = mountFacet(STATUS)
        await expectLogic(other).toDispatchActions(['loadFacetValuesSuccess'])
        expect(mockBreakdown).toHaveBeenCalledTimes(1)

        filtersLogic.actions.setDateRange({ date_from: '-7d' })
        await expectLogic(other).toDispatchActions(['loadFacetValuesSuccess'])
        expect(logic.values.facetValues).toEqual([])

        mockBreakdown.mockClear()
        railLogic.actions.toggleFacetCollapsed(SERVICE.key)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        expect(mockBreakdown).toHaveBeenCalledTimes(1)

        // Collapsing and expanding again with nothing else changed has nothing to fetch.
        mockBreakdown.mockClear()
        railLogic.actions.toggleFacetCollapsed(SERVICE.key)
        railLogic.actions.toggleFacetCollapsed(SERVICE.key)
        await expectLogic(logic).toNotHaveDispatchedActions(['loadFacetValues'])
        expect(mockBreakdown).not.toHaveBeenCalled()
    })

    it('collapsing while a fetch is still debouncing drops it, and expanding fetches again', async () => {
        // The request is debounced, so a facet collapsed within that window would otherwise still
        // hit the endpoint — and, having recorded the signature, look fresh enough to skip the
        // refetch on expand, leaving the stale list it was showing before.
        const logic = mountFacet(SERVICE)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        mockBreakdown.mockClear()

        filtersLogic.actions.setDateRange({ date_from: '-7d' })
        await expectLogic(logic).toDispatchActions(['loadFacetValues'])
        railLogic.actions.toggleFacetCollapsed(SERVICE.key)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        expect(mockBreakdown).not.toHaveBeenCalled()

        railLogic.actions.toggleFacetCollapsed(SERVICE.key)
        await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])
        expect(mockBreakdown).toHaveBeenCalledTimes(1)
    })
})