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.

chore(logs): name the buffer processing modes in the log pipeline (#87787)

Jon McCallum03e4223ff0ca
diff --git a/nodejs/src/logs/log-record-avro.test.ts b/nodejs/src/logs/log-record-avro.test.ts
index 46df7cae8888..913de50db75b 100644
--- a/nodejs/src/logs/log-record-avro.test.ts
+++ b/nodejs/src/logs/log-record-avro.test.ts
@@ -6,6 +6,7 @@ import * as logBodyParse from './log-body-parse'
import { PII_REDACTED, encodeAttributeCell } from './log-pii-scrub'
import {
LogRecord,
+ bufferProcessingMode,
decodeLogRecords,
encodeLogRecords,
enrichLogRecordWithJsonAttributes,
@@ -522,6 +523,53 @@ describe('log-record-avro', () => {
expect(pii).toEqual({ piiReplacements: 0 })
})
+ it('forwards the original buffer when only a visitor ran', async () => {
+ const records: LogRecord[] = [
+ {
+ uuid: 'test-uuid',
+ trace_id: null,
+ span_id: null,
+ trace_flags: null,
+ timestamp: null,
+ observed_timestamp: null,
+ body: JSON.stringify({ level: 'info', message: 'test' }),
+ severity_text: null,
+ severity_number: null,
+ service_name: null,
+ resource_attributes: null,
+ instrumentation_scope: null,
+ event_name: null,
+ attributes: null,
+ bytes_uncompressed: null,
+ },
+ ]
+
+ const inputBuffer = await encodeLogRecords(LOG_RECORD_SCHEMA, 'zstandard', records)
+ const onRecordsDecoded = jest.fn()
+ const { value: out } = await processLogMessageBuffer(
+ inputBuffer,
+ { json_parse_logs: false, pii_scrub_logs: false },
+ { onRecordsDecoded }
+ )
+
+ // The visitor forces a decode, but nothing mutated the records, so the buffer is returned by
+ // identity rather than re-encoded.
+ expect(onRecordsDecoded).toHaveBeenCalledTimes(1)
+ expect(onRecordsDecoded.mock.calls[0][0]).toHaveLength(1)
+ expect(out).toBe(inputBuffer)
+ })
+
+ it.each([
+ ['everything off', {}, 0, false, 'passthrough'],
+ ['json parse on', { json_parse_logs: true }, 0, false, 'decode_and_reencode'],
+ ['pii scrub on', { pii_scrub_logs: true }, 0, false, 'decode_and_reencode'],
+ ['a stage present', {}, 1, false, 'decode_and_reencode'],
+ ['a decoded-records visitor present', {}, 0, true, 'decode_only'],
+ ['a visitor and a stage', {}, 1, true, 'decode_and_reencode'],
+ ])('bufferProcessingMode: %s', (_name, settings, stageCount, hasVisitor, expected) => {
+ expect(bufferProcessingMode(settings, stageCount, hasVisitor)).toEqual(expected)
+ })
+
it('decodes and scrubs only when PII scrub is on without JSON parse', async () => {
const records: LogRecord[] = [
{
diff --git a/nodejs/src/logs/log-record-avro.ts b/nodejs/src/logs/log-record-avro.ts
index ea10d3d66b7a..4b676527fe17 100644
--- a/nodejs/src/logs/log-record-avro.ts
+++ b/nodejs/src/logs/log-record-avro.ts
@@ -316,6 +316,30 @@ export type ProcessLogMessageBufferResult = {
drops: DropStats
}
+/**
+ * How much work `processLogMessageBuffer` does with a buffer, cheapest first: `passthrough` forwards
+ * it untouched, `decode_only` decodes for a visitor then forwards the original, `decode_and_reencode`
+ * decodes, transforms and encodes again.
+ */
+export type BufferProcessingMode = 'passthrough' | 'decode_only' | 'decode_and_reencode'
+
+/**
+ * Give the buffer's current stage count, not the count it would have. A tier below
+ * `decode_and_reencode` names the work one more stage adds: `decode_only` adds an encode,
+ * `passthrough` adds a decode and an encode.
+ */
+export function bufferProcessingMode(
+ settings: LogsSettings,
+ stageCount: number,
+ hasVisitor: boolean
+): BufferProcessingMode {
+ const normalizeActive = (settings.json_parse_logs ?? false) || (settings.pii_scrub_logs ?? false)
+ if (normalizeActive || stageCount > 0) {
+ return 'decode_and_reencode'
+ }
+ return hasVisitor ? 'decode_only' : 'passthrough'
+}
+
/**
* The single decode → transform → encode path for a log message buffer.
* Passthrough (no decode) when json_parse_logs and pii_scrub_logs are off, there are no `stages`, and
@@ -338,16 +362,16 @@ export const processLogMessageBuffer = instrumented({
options: ProcessLogMessageBufferOptions = {}
): Promise<ProcessLogMessageBufferResult> {
const { onRecordsDecoded, stages = [] } = options
- const jsonParse = settings.json_parse_logs ?? false
- const piiScrub = settings.pii_scrub_logs ?? false
- const normalizeActive = jsonParse || piiScrub
- const mustReencode = normalizeActive || stages.length > 0
+ const processingMode = bufferProcessingMode(settings, stages.length, Boolean(onRecordsDecoded))
- if (!mustReencode && !onRecordsDecoded) {
+ if (processingMode === 'passthrough') {
// Passthrough: nothing mutates or drops and no visitor needs the records — forward untouched.
return { value: buffer, pii: EMPTY_PII, drops: EMPTY_DROP_STATS() }
}
+ // Read only by the duration labels in the `finally`, which the passthrough return never reaches.
+ const jsonParse = settings.json_parse_logs ?? false
+ const piiScrub = settings.pii_scrub_logs ?? false
const startTime = Date.now()
let codec = 'unknown'
@@ -372,7 +396,7 @@ export const processLogMessageBuffer = instrumented({
// already-empty batch so the caller can attribute it correctly.
return { value: null, pii, drops: stats }
}
- if (!mustReencode) {
+ if (processingMode === 'decode_only') {
// Only a visitor ran and records survive — the buffer is unchanged, so forward it without
// re-encoding.
return { value: buffer, pii, drops: stats }