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.

fix(logs): stop a large log attribute from stalling ingestion (#92334)

Frank Hamand89fc0ed76c40
diff --git a/nodejs/src/logs/log-pii-scrub.test.ts b/nodejs/src/logs/log-pii-scrub.test.ts
index 3716e3f8b1c4..e799522e858f 100644
--- a/nodejs/src/logs/log-pii-scrub.test.ts
+++ b/nodejs/src/logs/log-pii-scrub.test.ts
@@ -83,6 +83,22 @@ describe('log-pii-scrub', () => {
] as const)('$_label', ({ input, expected }) => {
expect(scrubPlainStringWithStats(input)).toEqual(expected)
})
+
+ it('scrubs a multi-megabyte value with thousands of matches without a quadratic stall', () => {
+ const EMAIL_COUNT = 8000
+ const MAX_DURATION_MS = 1000
+ const filler = 'x'.repeat(240)
+ const input = Array.from({ length: EMAIL_COUNT }, (_, i) => `user${i}@example.com ${filler}`).join(' ')
+
+ const startedAt = performance.now()
+ const { output, piiReplacements } = scrubPlainStringWithStats(input)
+ const durationMs = performance.now() - startedAt
+
+ expect(piiReplacements).toBe(EMAIL_COUNT)
+ expect(output).not.toContain('@example.com')
+ expect(output.split(PII_REDACTED)).toHaveLength(EMAIL_COUNT + 1)
+ expect(durationMs).toBeLessThan(MAX_DURATION_MS)
+ })
})
describe('scrubLogRecord', () => {
diff --git a/nodejs/src/logs/log-pii-scrub.ts b/nodejs/src/logs/log-pii-scrub.ts
index 9eb77a2d8a83..1b8cc2896032 100644
--- a/nodejs/src/logs/log-pii-scrub.ts
+++ b/nodejs/src/logs/log-pii-scrub.ts
@@ -6,8 +6,15 @@
* **Not guaranteed:** secrets only discoverable by JSON object keys inside `body` (no tree walk), values only in
* JSON number/boolean leaves. PAN-like digit runs are not redacted.
*
- * One alternated pattern with a replace callback (vs three global passes) for fewer full-string scans. Uses
- * `createTrackedRE2` for linear-time matching; ASCII-explicit classes for stable behavior under node-re2 Unicode mode.
+ * One alternated pattern (vs three global passes) for fewer full-string scans. Uses `createTrackedRE2` for
+ * linear-time matching; ASCII-explicit classes for stable behavior under node-re2 Unicode mode.
+ *
+ * Redaction runs as an explicit `exec` loop, not `String.prototype.replace`. RE2 matching is linear, but
+ * node-re2's `Symbol.replace` re-walks the subject once per match, so `.replace()` costs the string length
+ * times the match count. Scrubbing is synchronous and blocks the ingestion consumer's event loop, so on a
+ * multi-megabyte attribute value with tens of thousands of matches that quadratic term is long enough to
+ * exceed the consumer's Kafka poll interval and stall the partition. Do not collapse this back into
+ * `.replace()`.
*/
import { createTrackedRE2 } from '~/common/utils/tracked-re2'
@@ -38,25 +45,40 @@ const PII_COMBINED_RE = createTrackedRE2(
/** One regex pass; `piiReplacements` counts only successful redactions (a matched capture group). */
export function scrubPlainStringWithStats(input: string): { output: string; piiReplacements: number } {
let piiReplacements = 0
- const output = input.replace(
- PII_COMBINED_RE,
- (match: string, bearer: string | undefined, stripe: string | undefined, email: string | undefined) => {
- if (bearer !== undefined) {
- piiReplacements += 1
- return `Bearer ${PII_REDACTED}`
- }
- if (stripe !== undefined) {
- piiReplacements += 1
- return PII_REDACTED
- }
- if (email !== undefined) {
- piiReplacements += 1
- return PII_REDACTED
- }
- return match
+ let pieces: string[] | null = null
+ let copiedUpTo = 0
+
+ PII_COMBINED_RE.lastIndex = 0
+ let match: RegExpExecArray | null
+ while ((match = PII_COMBINED_RE.exec(input)) !== null) {
+ const [matched, bearer, stripe, email] = match
+ if (matched.length === 0) {
+ PII_COMBINED_RE.lastIndex += 1
+ continue
+ }
+
+ let replacement: string
+ if (bearer !== undefined) {
+ replacement = `Bearer ${PII_REDACTED}`
+ } else if (stripe !== undefined) {
+ replacement = PII_REDACTED
+ } else if (email !== undefined) {
+ replacement = PII_REDACTED
+ } else {
+ continue
}
- )
- return { output, piiReplacements }
+ piiReplacements += 1
+
+ pieces ??= []
+ pieces.push(input.slice(copiedUpTo, match.index), replacement)
+ copiedUpTo = match.index + matched.length
+ }
+
+ if (pieces === null) {
+ return { output: input, piiReplacements }
+ }
+ pieces.push(input.slice(copiedUpTo))
+ return { output: pieces.join(''), piiReplacements }
}
/** Apply regex-based redaction to a single string (e.g. log body). */