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.

feat(logs): stamp the masked pattern onto the log record (#89359)

Jon McCallum0622d0aab1d8
diff --git a/nodejs/src/logs/log-pattern-stage.test.ts b/nodejs/src/logs/log-pattern-stage.test.ts
index 93694d59bdd7..115c6f2735be 100644
--- a/nodejs/src/logs/log-pattern-stage.test.ts
+++ b/nodejs/src/logs/log-pattern-stage.test.ts
@@ -1,6 +1,7 @@
import type { Counter } from 'prom-client'
import { DEFAULT_PATTERN_MAX_INPUT_CHARS } from './config'
+import { PATTERN_VERSION } from './log-pattern-mask'
import {
logsPatternBodyKindCounter,
logsPatternInputCappedCounter,
@@ -40,7 +41,7 @@ describe('log-pattern-stage', () => {
return Object.fromEntries(metric.values.map((v) => [Object.values(v.labels)[0] ?? '', v.value]))
}
- it('tallies body kinds, rule fires, and input caps across a batch without touching the records', async () => {
+ it('tallies body kinds, rule fires, and input caps across a batch without touching the body', async () => {
const stage = makePatternMaskingStage(20, 1024)
const records = [
makeRecord(null),
@@ -62,6 +63,23 @@ describe('log-pattern-stage', () => {
expect((await logsPatternInputCappedCounter.get()).values[0].value).toEqual(1)
})
+ it.each([
+ [
+ 'the masked body, not the raw one',
+ 'request 0f2d6faf-07e3-4cff-bf47-7efa1024aee2 took 7141ms',
+ 'request <UUID> took <N>ms',
+ ],
+ ['a plain body', 'retry 5', 'retry <N>'],
+ ['an empty body, which must still carry a version or it reads as written before masking', null, ''],
+ ])('stamps pattern and version onto the record: %s', async (_name, body, expected) => {
+ const stage = makePatternMaskingStage(1024, 1024)
+ const record = makeRecord(body)
+
+ await stage.run([record])
+
+ expect(record).toMatchObject({ pattern: expected, pattern_version: PATTERN_VERSION })
+ })
+
it('falls back to the default caps when a configured cap is not a positive integer', async () => {
const stage = makePatternMaskingStage(Number.NaN, -1)
await stage.run([makeRecord('x'.repeat(DEFAULT_PATTERN_MAX_INPUT_CHARS + 1))])
diff --git a/nodejs/src/logs/log-pattern-stage.ts b/nodejs/src/logs/log-pattern-stage.ts
index 3cc56b44e728..4f0d27045e00 100644
--- a/nodejs/src/logs/log-pattern-stage.ts
+++ b/nodejs/src/logs/log-pattern-stage.ts
@@ -2,7 +2,7 @@ import { performance } from 'node:perf_hooks'
import { Counter, Histogram } from 'prom-client'
import { DEFAULT_PATTERN_MAX_INPUT_CHARS, DEFAULT_PATTERN_MAX_OUTPUT_CHARS } from './config'
-import { MASK_RULES, computeLogPattern } from './log-pattern-mask'
+import { MASK_RULES, PATTERN_VERSION, computeLogPattern } from './log-pattern-mask'
import type { LogRecord } from './log-record-avro'
import type { PipelineStage } from './pipeline/log-processing-pipeline'
@@ -49,7 +49,7 @@ export const logsPatternForcedDecodeCounter = new Counter({
export const logsPatternStageErrorCounter = new Counter({
name: 'logs_ingestion_pattern_stage_error_total',
- help: 'Batches where the pattern masking stage threw. The records survive; only these metrics are lost.',
+ help: 'Batches where the pattern masking stage threw. The records survive. Any record the throw came before keeps its stamp, and the rest stay unstamped, which reads as version 0.',
})
const positiveIntOr = (value: number, fallback: number): number =>
@@ -63,7 +63,7 @@ export function makePatternMaskingStage(maxInputChars: number, maxOutputChars: n
name: 'pattern_masking',
run: (records) => {
try {
- measureBatch(records, inputCap, outputCap)
+ stampBatch(records, inputCap, outputCap)
} catch {
logsPatternStageErrorCounter.inc()
}
@@ -71,7 +71,7 @@ export function makePatternMaskingStage(maxInputChars: number, maxOutputChars: n
}
}
-function measureBatch(records: LogRecord[], inputCap: number, outputCap: number): void {
+function stampBatch(records: LogRecord[], inputCap: number, outputCap: number): void {
const kindCounts = new Map<string, number>()
const ruleFires: number[] = new Array(MASK_RULES.length).fill(0)
let inputCapped = 0
@@ -79,6 +79,8 @@ function measureBatch(records: LogRecord[], inputCap: number, outputCap: number)
for (const record of records) {
const start = performance.now()
const result = computeLogPattern(record.body, inputCap, outputCap)
+ record.pattern = result.pattern
+ record.pattern_version = PATTERN_VERSION
logsPatternMaskingDurationHistogram.observe((performance.now() - start) / 1000)
logsPatternMaskedLengthHistogram.observe(result.maskedLength)
diff --git a/nodejs/src/logs/log-record-avro.ts b/nodejs/src/logs/log-record-avro.ts
index 4b676527fe17..e41fabd8e121 100644
--- a/nodejs/src/logs/log-record-avro.ts
+++ b/nodejs/src/logs/log-record-avro.ts
@@ -53,6 +53,12 @@ export interface LogRecord {
/** Per-row retention in days, stamped by the retention stage from team retention rules. Null/undefined
* leaves ClickHouse to fall back to the batch `retention-days` header (the team default). */
retention_days?: number | null
+ /** Masked body, stamped by the pattern masking stage. Identifiers become placeholders, so two
+ * lines differing only by a request id share one pattern. */
+ pattern?: string | null
+ /** Masking rule set that produced `pattern`. Null reads as 0 in ClickHouse, marking a row
+ * written before masking. */
+ pattern_version?: number | null
}
export async function decodeLogRecords(buffer: Buffer): Promise<[avro.Type | undefined, string, LogRecord[]]> {
diff --git a/rust/capture-logs/src/avro_schema.rs b/rust/capture-logs/src/avro_schema.rs
index cdd94d1dc5e3..20f2e9acbd6c 100644
--- a/rust/capture-logs/src/avro_schema.rs
+++ b/rust/capture-logs/src/avro_schema.rs
@@ -96,6 +96,18 @@ pub const AVRO_SCHEMA: &str = r#"
"type": ["null", "int"],
"default": null,
"doc": "Per-row retention in days, stamped by the Node consumer from team retention rules. Null when unset — ClickHouse then falls back to the batch `retention-days` header. capture-logs always writes null; it does not evaluate team rules."
+ },
+ {
+ "name": "pattern",
+ "type": ["null", "string"],
+ "default": null,
+ "doc": "Masked log body, stamped by the Node consumer. Identifiers are replaced with placeholders, so two lines differing only by a request id share one pattern. capture-logs always writes null; it does not mask."
+ },
+ {
+ "name": "pattern_version",
+ "type": ["null", "int"],
+ "default": null,
+ "doc": "Masking rule set that produced `pattern`. A rule change re-masks every line, so the detector compares within a version and treats a change as a cold start. Null reads as version 0 in ClickHouse, marking a row written before masking. capture-logs always writes null."
}
]
}"#;
diff --git a/rust/capture-logs/src/endpoints/datadog.rs b/rust/capture-logs/src/endpoints/datadog.rs
index f3a5ee82e6e3..ba108fa761ae 100644
--- a/rust/capture-logs/src/endpoints/datadog.rs
+++ b/rust/capture-logs/src/endpoints/datadog.rs
@@ -258,6 +258,8 @@ pub fn datadog_log_to_kafka_row(
attributes,
bytes_uncompressed: None,
retention_days: None,
+ pattern: None,
+ pattern_version: None,
}
.with_computed_bytes();
(row, was_overridden)
diff --git a/rust/capture-logs/src/log_record.rs b/rust/capture-logs/src/log_record.rs
index baf95f7224db..2ddda7174409 100644
--- a/rust/capture-logs/src/log_record.rs
+++ b/rust/capture-logs/src/log_record.rs
@@ -42,6 +42,11 @@ pub struct KafkaLogRow {
// Per-row retention in days. capture-logs always writes None — the Node consumer stamps
// this from team retention rules. Null lets ClickHouse fall back to the batch header.
pub retention_days: Option<i32>,
+ // Masked body and the rule set that produced it. capture-logs always writes None — the Node
+ // consumer stamps both. The fields exist here so the schema carries them; a value the writer
+ // schema has no slot for is silently dropped at encode.
+ pub pattern: Option<String>,
+ pub pattern_version: Option<i32>,
}
/// Sum byte lengths of the row's string and map content. Fixed-width fields
@@ -179,6 +184,8 @@ impl KafkaLogRow {
attributes,
bytes_uncompressed: None,
retention_days: None,
+ pattern: None,
+ pattern_version: None,
}
.with_computed_bytes();
debug!("log: {:?}", log_row);
@@ -395,6 +402,8 @@ mod tests {
attributes,
bytes_uncompressed: None,
retention_days: None,
+ pattern: None,
+ pattern_version: None,
}
}