: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
perf(logs): skip the json parse for bodies that cannot be json (#92714)
Jon McCallum0df71afd9b71
diff --git a/nodejs/src/logs/log-body-parse.test.ts b/nodejs/src/logs/log-body-parse.test.tsindex 3df5b2afae00..51680e306ab0 100644--- a/nodejs/src/logs/log-body-parse.test.ts+++ b/nodejs/src/logs/log-body-parse.test.ts@@ -30,4 +30,31 @@ describe('log-body-parse', () => {const raw = 'not json {'expect(parseLogBodyForIngestion(raw)).toEqual({ kind: 'invalid_json', raw })})++ // A body that cannot start a JSON document never reaches `parseJSON`. Each row here starts a+ // valid document, so a narrower skip would misreport it as `invalid_json`.+ it.each([+ ['object', '{"a":1}', { kind: 'json_object_or_array', value: { a: 1 } }],+ ['object behind leading whitespace', ' \r\n\t{"a":1}', { kind: 'json_object_or_array', value: { a: 1 } }],+ ['array behind leading whitespace', '\n[1,2]', { kind: 'json_object_or_array', value: [1, 2] }],+ ['string', '"hi"', { kind: 'json_string', value: 'hi' }],+ ['negative number', '-7', { kind: 'json_primitive', parsed: -7 }],+ ['true', 'true', { kind: 'json_primitive', parsed: true }],+ ['false', 'false', { kind: 'json_primitive', parsed: false }],+ ['null', 'null', { kind: 'json_primitive', parsed: null }],+ ])('decodes a body that starts with %s', (_name, body, expected) => {+ expect(parseLogBodyForIngestion(body)).toEqual(expected)+ })++ // The skip is a pre-filter, not a verdict. These bodies pass it and still fail the parse.+ it.each([+ ['null pointer at line 4'],+ ['true and false both reported'],+ ['-- person processing disabled'],+ ['404 not found'],+ [' '],+ [''],+ ])('returns invalid_json for %p', (raw) => {+ expect(parseLogBodyForIngestion(raw)).toEqual({ kind: 'invalid_json', raw })+ })})diff --git a/nodejs/src/logs/log-body-parse.ts b/nodejs/src/logs/log-body-parse.tsindex 5876ec6e2c5d..82d444c6a586 100644--- a/nodejs/src/logs/log-body-parse.ts+++ b/nodejs/src/logs/log-body-parse.ts@@ -11,10 +11,45 @@ export type LogBodyParseResult =| { kind: 'json_string'; value: string }| { kind: 'json_primitive'; parsed: number | boolean | null }+// JSON allows only these four characters between the start of the document and the value.+const isJsonWhitespace = (code: number): boolean => code === 0x20 || code === 0x09 || code === 0x0a || code === 0x0d++/**+ * True when the first non-whitespace character is one a JSON document can begin with:+ * `{`, `[`, `"`, `-`, a digit, or the first letter of `true`, `false`, or `null`.+ *+ * The set holds all eight starts on purpose. A `{`/`[` check would send top-level JSON strings+ * and bare scalars down the `invalid_json` branch, which changes the pattern they produce.+ */+function canStartJsonDocument(body: string): boolean {+ for (let index = 0; index < body.length; index++) {+ const code = body.charCodeAt(index)+ if (isJsonWhitespace(code)) {+ continue+ }+ return (+ code === 0x7b || // {+ code === 0x5b || // [+ code === 0x22 || // "+ code === 0x2d || // -+ (code >= 0x30 && code <= 0x39) || // 0-9+ code === 0x74 || // t, for true+ code === 0x66 || // f, for false+ code === 0x6e // n, for null+ )+ }+ return false+}+export function parseLogBodyForIngestion(body: string | null): LogBodyParseResult {if (body === null) {return { kind: 'empty' }}+ // Plaintext log lines are a large share of the traffic and every one of them makes `parseJSON`+ // build and throw an error. The scan reaches the same result without paying for the throw.+ if (!canStartJsonDocument(body)) {+ return { kind: 'invalid_json', raw: body }+ }try {const parsed = parseJSON(body)if (parsed !== null && typeof parsed === 'object') {diff --git a/nodejs/src/logs/log-pattern-mask.test.ts b/nodejs/src/logs/log-pattern-mask.test.tsindex e676dfdf1b53..51b520835934 100644--- a/nodejs/src/logs/log-pattern-mask.test.ts+++ b/nodejs/src/logs/log-pattern-mask.test.ts@@ -172,6 +172,8 @@ describe('log-pattern-mask', () => {['json string', '"quoted 7"', 'json_string', 'quoted <N>'],['json number primitive', '42', 'primitive', '<N>'],['prose body', 'plain text 3', 'plaintext', 'plain text <N>'],+ ['json object behind leading whitespace', ' \n\t{"msg":"hi 9"}', 'json_object_or_array', 'hi <N>'],+ ['prose body that opens like JSON', 'null pointer at line 4', 'plaintext', 'null pointer at line <N>'],])('body kind %s', (_name, body, expectedKind, expectedPattern) => {const result = computeLogPattern(body)expect(result.bodyKind).toEqual(expectedKind)