: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { DateTime } from 'luxon'
import { register } from 'prom-client'
import { APP_METRICS_OUTPUT, LOG_ENTRIES_OUTPUT } from '~/common/outputs'
import { IngestionOutputs } from '~/common/outputs/ingestion-outputs'
import { parseJSON } from '~/common/utils/json-parse'
import { internalFetch } from '~/common/utils/request'
import { InternalLogsProducer } from '~/logs/internal-logs-producer'
import { LogEntry, MetricLogSource, MinimalAppMetric } from '../../types'
import {
HogFunctionMonitoringService,
MonitoringOutput,
createTransformationsMonitoringService,
} from './hog-function-monitoring.service'
jest.mock('~/common/utils/request', () => ({ internalFetch: jest.fn() }))
describe('HogFunctionMonitoringService', () => {
let service: HogFunctionMonitoringService
let queueMessagesMock: jest.Mock<Promise<void>, [any[]]>
let outputs: IngestionOutputs<MonitoringOutput>
let produceMock: jest.Mock
beforeEach(() => {
queueMessagesMock = jest.fn().mockResolvedValue(undefined)
produceMock = jest.fn().mockResolvedValue(undefined)
const fakeOutput = {
produce: produceMock,
queueMessages: queueMessagesMock,
checkHealth: jest.fn().mockResolvedValue(undefined),
checkTopicExists: jest.fn().mockResolvedValue(undefined),
} as any
outputs = new IngestionOutputs<MonitoringOutput>({
[APP_METRICS_OUTPUT]: fakeOutput,
[LOG_ENTRIES_OUTPUT]: fakeOutput,
})
service = new HogFunctionMonitoringService(outputs)
})
async function flushedRows(): Promise<Record<string, unknown>[]> {
await service.flush()
return (queueMessagesMock.mock.calls[0]?.[0] ?? []).map(
(m: { value: Buffer }) => parseJSON(m.value.toString()) as Record<string, unknown>
)
}
function metric(overrides: Partial<MinimalAppMetric> = {}): MinimalAppMetric {
return {
team_id: 1,
app_source_id: 'flow-1',
instance_id: 'action-1',
metric_kind: 'success',
metric_name: 'succeeded',
count: 1,
...overrides,
}
}
it('mirrors a versioned hog flow metric under hog_flow_version, keyed by version', async () => {
service.queueAppMetric(metric({ app_source_version: { id: 'flow-1', version: 3 } }), 'hog_flow')
const rows = await flushedRows()
expect(rows).toEqual([
expect.objectContaining({ app_source: 'hog_flow', app_source_id: 'flow-1', instance_id: 'action-1' }),
expect.objectContaining({
app_source: 'hog_flow_version',
app_source_id: 'flow-1/3',
instance_id: 'action-1',
}),
])
// The version is a routing hint, not a column — a stray field breaks the app_metrics2 Kafka table.
expect(rows.every((row) => !('app_source_version' in row))).toBe(true)
})
const unmirroredCases: { name: string; source: MetricLogSource; overrides: Partial<MinimalAppMetric> }[] = [
{ name: 'no version', source: 'hog_flow', overrides: {} },
{ name: 'an undefined version', source: 'hog_flow', overrides: { app_source_version: undefined } },
{
// A flow loaded without its `version` column: the id is there but the version isn't.
name: 'a version-less flow',
source: 'hog_flow',
overrides: { app_source_version: { id: 'flow-1', version: undefined as unknown as number } },
},
{
name: 'a versioned hog function',
source: 'hog_function',
overrides: { app_source_version: { id: 'flow-1', version: 3 } },
},
]
it.each(unmirroredCases)('writes only the version-agnostic row for $name', async ({ source, overrides }) => {
service.queueAppMetric(metric(overrides), source)
const rows = await flushedRows()
expect(rows).toHaveLength(1)
expect(rows[0].app_source).toBe(source)
})
it('keeps versions apart when the same metric is counted on either side of a publish', async () => {
service.queueAppMetric(metric({ app_source_version: { id: 'flow-1', version: 2 }, count: 5 }), 'hog_flow')
service.queueAppMetric(metric({ app_source_version: { id: 'flow-1', version: 3 }, count: 2 }), 'hog_flow')
const rows = await flushedRows()
const byId = Object.fromEntries(rows.map((row) => [`${row.app_source}:${row.app_source_id}`, row.count]))
// The version-agnostic row aggregates both, so per-version reads and total reads agree.
expect(byId).toEqual({
'hog_flow:flow-1': 7,
'hog_flow_version:flow-1/2': 5,
'hog_flow_version:flow-1/3': 2,
})
})
it('keys the versioned row by the flow, not the batch run its metrics are grouped under', async () => {
// Batch-triggered runs put the run id in `app_source_id` so per-run views group correctly.
// Keying the mirror off that would mint a fresh id every run, so a broadcast's versions would
// never aggregate across its runs and the documented `<flow id>/<version>` read would miss them.
service.queueAppMetric(
metric({ app_source_id: 'batch-run-1', app_source_version: { id: 'flow-1', version: 3 } }),
'hog_flow'
)
service.queueAppMetric(
metric({ app_source_id: 'batch-run-2', app_source_version: { id: 'flow-1', version: 3 } }),
'hog_flow'
)
const rows = await flushedRows()
expect(rows.filter((row) => row.app_source === 'hog_flow_version')).toEqual([
expect.objectContaining({ app_source_id: 'flow-1/3', count: 2 }),
])
})
const producerConfig = {
LOGS_INTERNAL_INGESTION_SIGNING_KEYS: 'key',
LOGS_INTERNAL_PRODUCER_URL: 'http://capture-logs.test/i/v1/logs',
LOGS_INTERNAL_PRODUCER_TEAMS: '2',
LOGS_INTERNAL_PRODUCER_MAX_IN_FLIGHT: 20,
LOGS_INTERNAL_PRODUCER_MAX_BUFFERED_BYTES: 1024 * 1024,
LOGS_INTERNAL_PRODUCER_RETRY_TIMEOUT_MS: 1_000,
}
const teamManager = {
getTeam: (teamId: number) => Promise.resolve({ id: teamId, api_token: `phc_${teamId}` } as any),
}
const internalLogsProducer = (): InternalLogsProducer => new InternalLogsProducer(producerConfig, teamManager)
const log = (teamId: number): LogEntry => ({
team_id: teamId,
log_source: 'hog_flow',
log_source_id: 'flow-1',
instance_id: 'run-1',
timestamp: DateTime.fromISO('2026-10-27T12:00:00Z'),
level: 'info',
message: `team ${teamId}`,
})
it('sends accepted logs to the logs product with the log entry timestamps, and drains them on stop', async () => {
const fetchMock = jest.mocked(internalFetch).mockResolvedValue({ status: 200, dump: jest.fn() } as any)
service = new HogFunctionMonitoringService(outputs, internalLogsProducer(), new Set(['hog_flow']))
service.queueLogs([log(2), log(2), log(3)], 'hog_flow')
service.queueLogs([log(2)], 'hog_function')
await service.flush()
await new Promise((resolve) => setImmediate(resolve))
expect(produceMock).toHaveBeenCalledTimes(4)
expect(fetchMock).toHaveBeenCalledTimes(1)
const [, options] = fetchMock.mock.calls[0]
const body = parseJSON(options!.body as string) as any
const records = body.resourceLogs[0].scopeLogs[0].logRecords
expect(records.map((r: any) => r.body.stringValue)).toEqual(['team 2', 'team 2'])
const entryMillis = produceMock.mock.calls
.map(([message]) => parseJSON(message.value.toString()) as Record<string, unknown>)
.filter((row) => row.team_id === 2 && row.log_source === 'hog_flow')
.map((row) => DateTime.fromSQL(row.timestamp as string, { zone: 'utc' }).toMillis())
expect(records.map((r: any) => Number(BigInt(r.timeUnixNano) / 1_000_000n))).toEqual(entryMillis)
service.queueLogs([log(2)], 'hog_flow')
await service.stop()
expect(fetchMock).toHaveBeenCalledTimes(2)
})
it('keeps logs about internal logs out of the logs product, writes them to log_entries, and counts them', async () => {
const fetchMock = jest.mocked(internalFetch).mockResolvedValue({ status: 200, dump: jest.fn() } as any)
const loopGuarded = async (): Promise<number> =>
(await register.getSingleMetric('logs_internal_producer_records_total')!.get()).values.find(
(v) => v.labels.outcome === 'loop_guard'
)?.value ?? 0
const before = await loopGuarded()
service = new HogFunctionMonitoringService(outputs, internalLogsProducer(), new Set(['hog_flow']))
service.queueLogs([log(2), log(2), log(3)], 'hog_flow', { aboutInternalLogs: true })
await service.flush()
await service.stop()
expect(produceMock).toHaveBeenCalledTimes(3)
expect(fetchMock).not.toHaveBeenCalled()
expect((await loopGuarded()) - before).toBe(2)
})
it('sends nothing to the logs product for a source not in CDP_INTERNAL_LOGS_SOURCES', async () => {
const fetchMock = jest.mocked(internalFetch).mockResolvedValue({ status: 200, dump: jest.fn() } as any)
service = new HogFunctionMonitoringService(outputs, internalLogsProducer(), new Set(['hog_function']))
service.queueLogs([log(2)], 'hog_flow')
await service.flush()
await service.stop()
expect(produceMock).toHaveBeenCalledTimes(1)
expect(fetchMock).not.toHaveBeenCalled()
})
it.each([
['hog_flow,hog_function', 0],
['hog_flow,hog_function_transformations', 1],
])('sends transformation logs only when the transformations switch is on (%s)', async (sources, sends) => {
const fetchMock = jest.mocked(internalFetch).mockResolvedValue({ status: 200, dump: jest.fn() } as any)
service = createTransformationsMonitoringService(
outputs,
{ ...producerConfig, CDP_INTERNAL_LOGS_SOURCES: sources },
teamManager
)
service.queueLogs([{ ...log(2), log_source: 'hog_function' }], 'hog_function')
await service.flush()
await service.stop()
expect(produceMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledTimes(sends)
})
})