: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
fix(logs): use decimal KB and guard non-array patterns in rule compile (#67597)
Daniel Viscacd4db0638d66
diff --git a/nodejs/src/logs/sampling/compile-rules.ts b/nodejs/src/logs/sampling/compile-rules.tsindex 482b84ebc9b7..4ea4b81a0a15 100644--- a/nodejs/src/logs/sampling/compile-rules.ts+++ b/nodejs/src/logs/sampling/compile-rules.ts@@ -61,7 +61,11 @@ const MAX_LOGS_PER_SECOND = 1_000_000const MAX_BURST_LOGS = 10_000_000const MAX_KB_PER_SECOND = 1_000_000const MAX_BURST_KB = 10_000_000-const BYTES_PER_KB = 1024+// Decimal (SI) KB: the drop-rule form (UNIT_TO_KB_PER_S), the sparkline preview's+// threshold line (KB/s × 1000), and the API validator ("1000000 = 1 GB/s") all use+// 1 KB = 1000 bytes. The bucket must charge in the same unit, otherwise every cap+// silently enforces ~2.4% above its label.+const BYTES_PER_KB = 1000function parseRateLimitFromConfig(config: Record<string, unknown>@@ -230,7 +234,9 @@ export function compileRuleSet(rows: SamplingRuleRow[]): CompiledRuleSet {let pathDropMatchAttributeKey: string | null = nulllet filterGroup: FilterGroupNode | null = nullif (row.rule_type === 'path_drop') {- const patterns = (row.config.patterns as unknown[]) || []+ // Array.isArray, not truthiness: a corrupt string value would otherwise be+ // iterated per character into single-char regexes that match nearly everything.+ const patterns = Array.isArray(row.config.patterns) ? (row.config.patterns as unknown[]) : []pathDropPatterns = []for (const p of patterns) {if (typeof p !== 'string') {diff --git a/nodejs/src/logs/sampling/evaluate.test.ts b/nodejs/src/logs/sampling/evaluate.test.tsindex f8a8b7c38df9..45e7e3c58420 100644--- a/nodejs/src/logs/sampling/evaluate.test.ts+++ b/nodejs/src/logs/sampling/evaluate.test.ts@@ -161,6 +161,41 @@ describe('evaluateLogRecord', () => {expect(elapsed).toBeLessThan(10_000)})+ it('compileRuleSet converts kb_per_second to bytes using decimal KB', () => {+ const rs = compileRuleSet([+ {+ id: 'rl-kb',+ rule_type: 'rate_limit',+ scope_service: null,+ scope_path_pattern: null,+ scope_attribute_filters: [],+ config: { kb_per_second: 1, burst_kb: 2 },+ },+ ])+ // The drop-rule UI (UNIT_TO_KB_PER_S), the preview threshold line (KB/s × 1000),+ // and the API validator ("1000000 = 1 GB/s") all treat KB as decimal — the+ // bucket must enforce the same unit or every cap runs above its label.+ expect(rs.rules[0]?.rateLimit).toEqual({ refillPerSecond: 1000, poolMax: 2000, costUnit: 'bytes' })+ })++ it('path_drop with non-array patterns config matches nothing', () => {+ const rules = compileRuleSet([+ {+ id: 'p-bad',+ rule_type: 'path_drop',+ scope_service: null,+ scope_path_pattern: null,+ scope_attribute_filters: [],+ config: { patterns: 'abc' },+ },+ ])+ // A corrupt non-array value must not be iterated per character into+ // single-char regexes, each of which would match nearly every path.+ const rec = baseRecord()+ rec.attributes = { 'http.route': '/api/cart' }+ expect(evaluateLogRecord(rules, rec).decision).not.toBe(SAMPLING_DECISION_DROP)+ })+it('compileRuleSet marks hasRateLimitRules for valid rate_limit config', () => {const rs = compileRuleSet([{