: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
feat(ingestion): log the message headers when a pipeline step crashes (#106607)
Paweł Ledwońb29a3b90c77b
diff --git a/nodejs/src/ingestion/framework/helpers.ts b/nodejs/src/ingestion/framework/helpers.tsindex 3a10834ad66b..87020509c716 100644--- a/nodejs/src/ingestion/framework/helpers.ts+++ b/nodejs/src/ingestion/framework/helpers.ts@@ -1,4 +1,4 @@-import { Message } from 'node-rdkafka'+import { Message, MessageHeader } from 'node-rdkafka'import { ChunkPipelineBuilder, newChunkPipelineBuilder } from './builders'import { ChunkPipelineUnwrapper } from './chunk-pipeline-unwrapper'@@ -14,12 +14,43 @@ import { PipelineResult, ok } from './results'import { StartPipeline } from './start-pipeline'/** Loggable origin of a Kafka message, stored on the context at feed time. */-export type KafkaDebugContext = { topic?: string; partition: number; offset: number }+export type KafkaDebugContext = {+ topic?: string+ partition: number+ offset: number+ headers?: Record<string, string>+}++// The headers a responder needs to write an event restriction for a poison+// message. distinct_id and session_id are customer-controlled and stay out.+const LOGGED_HEADERS = new Set(['token', 'event', 'uuid'])export type DefaultContext = { message: Message; debugContext?: KafkaDebugContext }export function createKafkaDebugContext(message: Message): KafkaDebugContext {- return { topic: message.topic, partition: message.partition, offset: message.offset }+ const { topic, partition, offset, headers } = message+ return {+ topic,+ partition,+ offset,+ // Built for every message, read only when a step crashes: render the+ // headers on read so the happy path does no string work.+ get headers() {+ return headers === undefined ? undefined : formatKafkaHeaders(headers)+ },+ }+}++function formatKafkaHeaders(headers: MessageHeader[]): Record<string, string> {+ const result: Record<string, string> = {}+ for (const header of headers) {+ for (const key of Object.keys(header)) {+ if (LOGGED_HEADERS.has(key)) {+ result[key] = header[key].toString()+ }+ }+ }+ return result}/**diff --git a/nodejs/src/ingestion/framework/step-pipeline.test.ts b/nodejs/src/ingestion/framework/step-pipeline.test.tsindex ccbba96c8a21..40963fb1fb4b 100644--- a/nodejs/src/ingestion/framework/step-pipeline.test.ts+++ b/nodejs/src/ingestion/framework/step-pipeline.test.ts@@ -1,8 +1,8 @@-import { Message } from 'node-rdkafka'+import { Message, MessageHeader } from 'node-rdkafka'import { logger } from '~/common/utils/logger'-import { createContext, createOkContext } from './helpers'+import { createContext, createKafkaDebugContext, createOkContext } from './helpers'import { isOkResult, ok } from './results'import { StartPipeline } from './start-pipeline'import { StepPipeline } from './step-pipeline'@@ -38,16 +38,26 @@ describe('StepPipeline', () => {})it('should handle step errors and log the debug context', async () => {- const message: Message = { value: Buffer.from('test'), topic: 'test', partition: 0, offset: 1 } as Message+ const headers: MessageHeader[] = [+ { token: Buffer.from('phc_token') },+ { uuid: '0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b' },+ { event: '$pageview' },+ { distinct_id: '[email protected]' },+ { session_id: '0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5c' },+ ]+ const message: Message = {+ value: Buffer.from('test'),+ topic: 'test',+ partition: 0,+ offset: 1,+ headers,+ } as Messageconst step = jest.fn().mockRejectedValue(new Error('Step failed'))const previous = new StartPipeline<{ data: string }, unknown>()const pipeline = new StepPipeline(step, previous)- const input = createOkContext(- { data: 'test' },- { message, debugContext: { topic: 'test', partition: 0, offset: 1 } }- )+ const input = createOkContext({ data: 'test' }, { message, debugContext: createKafkaDebugContext(message) })await expect(pipeline.process(input)).rejects.toThrow('Step failed')expect(logger.error).toHaveBeenCalledWith(@@ -55,9 +65,35 @@ describe('StepPipeline', () => {expect.stringContaining('threw'),expect.objectContaining({error: 'Step failed',- debugContext: { topic: 'test', partition: 0, offset: 1 },+ debugContext: expect.objectContaining({+ topic: 'test',+ partition: 0,+ offset: 1,+ headers: {+ token: 'phc_token',+ uuid: '0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b',+ event: '$pageview',+ },+ }),}))+ const logged = (logger.error as jest.Mock).mock.calls.at(-1)?.[2].debugContext.headers+ expect(logged).not.toHaveProperty('distinct_id')+ expect(logged).not.toHaveProperty('session_id')+ })++ it('should log no headers when the message has none', async () => {+ const message: Message = { value: Buffer.from('test'), topic: 'test', partition: 0, offset: 1 } as Message++ const step = jest.fn().mockRejectedValue(new Error('Step failed'))+ const previous = new StartPipeline<{ data: string }, unknown>()++ const pipeline = new StepPipeline(step, previous)+ const input = createOkContext({ data: 'test' }, { message, debugContext: createKafkaDebugContext(message) })++ await expect(pipeline.process(input)).rejects.toThrow('Step failed')+ const call = (logger.error as jest.Mock).mock.calls.at(-1)+ expect(call?.[2].debugContext.headers).toBeUndefined()})})