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.5 KB
Raw Blame
import { DateTime } from 'luxon'

import { APP_METRICS_OUTPUT, AppMetricsOutput } from '~/common/outputs'
import { IngestionOutputs } from '~/common/outputs/ingestion-outputs'
import { parseJSON } from '~/common/utils/json-parse'

import { AppMetricInput, AppMetricsAggregator } from './app-metrics-aggregator'

function makeOutputs(): {
    outputs: IngestionOutputs<AppMetricsOutput>
    queueMessagesMock: jest.Mock<Promise<void>, [any[]]>
} {
    const queueMessagesMock = jest.fn().mockResolvedValue(undefined)
    const fakeOutput = {
        produce: jest.fn().mockResolvedValue(undefined),
        queueMessages: queueMessagesMock,
        checkHealth: jest.fn().mockResolvedValue(undefined),
        checkTopicExists: jest.fn().mockResolvedValue(undefined),
    } as unknown as ConstructorParameters<typeof IngestionOutputs<AppMetricsOutput>>[0][AppMetricsOutput]
    return {
        outputs: new IngestionOutputs<AppMetricsOutput>({ [APP_METRICS_OUTPUT]: fakeOutput }),
        queueMessagesMock,
    }
}

function input(overrides: Partial<AppMetricInput> = {}): AppMetricInput {
    return {
        team_id: 1,
        app_source: 'hog_function',
        app_source_id: 'fn-1',
        instance_id: 'inst-1',
        metric_kind: 'success',
        metric_name: 'succeeded',
        count: 1,
        ...overrides,
    }
}

describe('AppMetricsAggregator', () => {
    let outputs: IngestionOutputs<AppMetricsOutput>
    let queueMessagesMock: jest.Mock<Promise<void>, [any[]]>

    beforeEach(() => {
        ;({ outputs, queueMessagesMock } = makeOutputs())
    })

    function getRows(callIndex = 0): Record<string, unknown>[] {
        const messages = queueMessagesMock.mock.calls[callIndex][0]
        return messages.map((m: { value: Buffer }) => parseJSON(m.value.toString()) as Record<string, unknown>)
    }

    it('flush is a no-op when nothing was queued', async () => {
        const agg = new AppMetricsAggregator(outputs)
        await agg.flush()
        expect(queueMessagesMock).not.toHaveBeenCalled()
    })

    it('serializes the v2 schema with all six identity fields, count, and a timestamp', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input())
        await agg.flush()

        const row = getRows()[0]
        expect(row).toMatchObject({
            team_id: 1,
            app_source: 'hog_function',
            app_source_id: 'fn-1',
            instance_id: 'inst-1',
            metric_kind: 'success',
            metric_name: 'succeeded',
            count: 1,
        })
        expect(row.timestamp).toEqual(expect.any(String))
    })

    it('sums counts for entries sharing the six identity fields', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input({ count: 1 }))
        agg.queue(input({ count: 2 }))
        agg.queue(input({ count: 4 }))
        await agg.flush()

        const rows = getRows()
        expect(rows).toHaveLength(1)
        expect(rows[0].count).toBe(7)
    })

    it.each([
        ['team_id', { team_id: 2 }],
        ['app_source', { app_source: 'hog_flow' }],
        ['app_source_id', { app_source_id: 'fn-2' }],
        ['instance_id', { instance_id: 'inst-2' }],
        ['metric_kind', { metric_kind: 'failure' }],
        ['metric_name', { metric_name: 'failed' }],
    ])('keeps %s separate', async (_field, override) => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input())
        agg.queue(input(override as Partial<AppMetricInput>))
        await agg.flush()

        expect(getRows()).toHaveLength(2)
    })

    it('treats undefined instance_id as ""', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input({ instance_id: undefined }))
        agg.queue(input({ instance_id: '' }))
        await agg.flush()

        const rows = getRows()
        expect(rows).toHaveLength(1)
        expect(rows[0].instance_id).toBe('')
        expect(rows[0].count).toBe(2)
    })

    it('produces with key=null', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input())
        await agg.flush()

        const messages = queueMessagesMock.mock.calls[0][0]
        expect(messages[0].key).toBeNull()
    })

    it('clears the buffer after flush', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input())
        await agg.flush()
        queueMessagesMock.mockClear()

        await agg.flush()
        expect(queueMessagesMock).not.toHaveBeenCalled()
    })

    it('writes an explicit timestamp truncated to the hour', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input({ timestamp: DateTime.fromISO('2027-03-04T05:06:07.890Z') }))
        await agg.flush()

        expect(getRows()[0].timestamp).toBe('2027-03-04 05:00:00.000')
    })

    it('sums entries in the same hour and keeps different hours apart', async () => {
        const agg = new AppMetricsAggregator(outputs)
        agg.queue(input({ count: 1, timestamp: DateTime.fromISO('2027-03-04T05:10:00Z') }))
        agg.queue(input({ count: 2, timestamp: DateTime.fromISO('2027-03-04T05:50:00Z') }))
        agg.queue(input({ count: 4, timestamp: DateTime.fromISO('2027-04-03T05:10:00Z') }))
        agg.queue(input({ count: 8 }))
        await agg.flush()

        const countsByTimestamp = Object.fromEntries(getRows().map((r) => [r.timestamp, r.count]))
        expect(Object.keys(countsByTimestamp)).toHaveLength(3)
        expect(countsByTimestamp['2027-03-04 05:00:00.000']).toBe(3)
        expect(countsByTimestamp['2027-04-03 05:00:00.000']).toBe(4)
    })
})