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): deterministic drops outrank rate_limit regardless of priority (#59547)

Daniel Visca9f6caac5d5e3
diff --git a/nodejs/src/logs-ingestion/sampling/evaluate.test.ts b/nodejs/src/logs-ingestion/sampling/evaluate.test.ts
index c9ab42eab52f..2ba4757e2b06 100644
--- a/nodejs/src/logs-ingestion/sampling/evaluate.test.ts
+++ b/nodejs/src/logs-ingestion/sampling/evaluate.test.ts
@@ -453,4 +453,225 @@ describe('evaluateLogRecord', () => {
})
})
})
+
+ describe('deterministic drops outrank rate_limit regardless of priority', () => {
+ // A rate-limit bucket is a finite shared resource; if a record was going to be
+ // dropped anyway by a deterministic rule, charging the bucket for it just
+ // starves legitimate traffic for no reason. So path_drop / severity_sampling
+ // ALWAYS resolve a record before rate_limit can claim it, even when rate_limit
+ // appears first in the priority-ordered rule list.
+
+ it.each([
+ {
+ label: 'path_drop wins when rate_limit appears earlier in rule order',
+ extraRule: {
+ id: 'pd-second',
+ rule_type: 'path_drop' as const,
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { patterns: ['/healthz'] },
+ },
+ patchRecord: (_rec: LogRecord) => {},
+ expected: { kind: 'resolved', decision: SAMPLING_DECISION_DROP, ruleId: 'pd-second' },
+ },
+ {
+ label: 'severity_sampling drop wins when rate_limit appears earlier in rule order',
+ extraRule: {
+ id: 'ss-second',
+ rule_type: 'severity_sampling' as const,
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ actions: {
+ DEBUG: { type: 'keep' },
+ INFO: { type: 'drop' },
+ WARN: { type: 'keep' },
+ ERROR: { type: 'keep' },
+ },
+ },
+ },
+ patchRecord: (rec: LogRecord) => {
+ rec.severity_text = 'info'
+ },
+ expected: { kind: 'resolved', decision: SAMPLING_DECISION_DROP, ruleId: 'ss-second' },
+ },
+ ])('$label', ({ extraRule, patchRecord, expected }) => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-first',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 10 },
+ },
+ extraRule,
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ patchRecord(rec)
+ expect(classifySamplingRecord(rules, rec)).toEqual(expected)
+ })
+
+ it('severity_sampling drop wins when rate_limit appears earlier in rule order', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-first',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 10 },
+ },
+ {
+ id: 'ss-second',
+ rule_type: 'severity_sampling',
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ actions: {
+ DEBUG: { type: 'keep' },
+ INFO: { type: 'drop' },
+ WARN: { type: 'keep' },
+ ERROR: { type: 'keep' },
+ },
+ },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ rec.severity_text = 'info'
+ expect(classifySamplingRecord(rules, rec)).toEqual({
+ kind: 'resolved',
+ decision: SAMPLING_DECISION_DROP,
+ ruleId: 'ss-second',
+ })
+ })
+
+ it('severity_sampling keep wins over later rate_limit (record is preserved, bucket not charged)', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-first',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 10 },
+ },
+ {
+ id: 'ss-second',
+ rule_type: 'severity_sampling',
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ actions: {
+ DEBUG: { type: 'keep' },
+ INFO: { type: 'keep' },
+ WARN: { type: 'keep' },
+ ERROR: { type: 'keep' },
+ },
+ },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ expect(classifySamplingRecord(rules, rec)).toEqual({
+ kind: 'resolved',
+ decision: 'keep',
+ ruleId: 'ss-second',
+ })
+ })
+
+ it('rate_limit still wins when no deterministic rule resolves the record', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-first',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 10 },
+ },
+ {
+ id: 'pd-noop',
+ rule_type: 'path_drop',
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { patterns: ['/never-matches'] },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ expect(classifySamplingRecord(rules, rec)).toEqual({ kind: 'rate_limit', ruleId: 'rl-first' })
+ })
+
+ it('first matching rate_limit rule wins when multiple rate_limit rules apply and no deterministic rule resolves', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-a',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 10 },
+ },
+ {
+ id: 'rl-b',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 100 },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ expect(classifySamplingRecord(rules, rec)).toEqual({ kind: 'rate_limit', ruleId: 'rl-a' })
+ })
+
+ it('alwaysKeep on a later rule still pre-empts an earlier rate_limit match', () => {
+ // alwaysKeep already short-circuits to KEEP today; this guards against a
+ // future refactor accidentally letting rate_limit win over an explicit
+ // alwaysKeep that sits after it in the priority order.
+ const rules = compileRuleSet([
+ {
+ id: 'rl-first',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: { logs_per_second: 10 },
+ },
+ {
+ id: 'keep-errors',
+ rule_type: 'severity_sampling',
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ actions: {
+ DEBUG: { type: 'drop' },
+ INFO: { type: 'drop' },
+ WARN: { type: 'keep' },
+ ERROR: { type: 'keep' },
+ },
+ always_keep: { status_gte: 500 },
+ },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ rec.attributes = { 'http.status_code': '503' }
+ expect(classifySamplingRecord(rules, rec)).toEqual({
+ kind: 'resolved',
+ decision: 'keep',
+ ruleId: 'keep-errors',
+ })
+ })
+ })
})
diff --git a/nodejs/src/logs-ingestion/sampling/evaluate.ts b/nodejs/src/logs-ingestion/sampling/evaluate.ts
index 7c2b0aa9dfee..bc6e0cca056e 100644
--- a/nodejs/src/logs-ingestion/sampling/evaluate.ts
+++ b/nodejs/src/logs-ingestion/sampling/evaluate.ts
@@ -199,14 +199,18 @@ export type SamplingClassifyResult =
export type RateLimitPendingByRule = Map<string, number[]>
/**
- * Stateless rule walk for ingestion. When the first applicable rule is `rate_limit` with a compiled
- * bucket, returns `rate_limit` so the caller can batch Redis checks. Otherwise matches `evaluateLogRecord`.
+ * Stateless rule walk for ingestion. Deterministic rules (`path_drop`, `severity_sampling`)
+ * always win over `rate_limit` regardless of priority order: a record that any deterministic
+ * rule resolves never charges the rate-limit bucket. The first applicable `rate_limit` match
+ * is remembered while the walk continues; it is only returned when no deterministic rule
+ * resolves the record. Otherwise matches `evaluateLogRecord`.
*/
export function classifySamplingRecord(teamRuleSet: CompiledRuleSet | null, record: LogRecord): SamplingClassifyResult {
if (!teamRuleSet || teamRuleSet.rules.length === 0) {
return { kind: 'resolved', decision: SAMPLING_DECISION_KEEP, ruleId: null }
}
const ord = severityOrdinalFromRecord(record)
+ let pendingRateLimitRuleId: string | null = null
for (const rule of teamRuleSet.rules) {
if (!matchesScope(rule, record)) {
continue
@@ -244,9 +248,15 @@ export function classifySamplingRecord(teamRuleSet: CompiledRuleSet | null, reco
if (rule.filterGroup && !matchFilterGroup(rule.filterGroup, record)) {
continue
}
- return { kind: 'rate_limit', ruleId: rule.id }
+ if (pendingRateLimitRuleId === null) {
+ pendingRateLimitRuleId = rule.id
+ }
+ continue
}
}
+ if (pendingRateLimitRuleId !== null) {
+ return { kind: 'rate_limit', ruleId: pendingRateLimitRuleId }
+ }
return { kind: 'resolved', decision: SAMPLING_DECISION_KEEP, ruleId: null }
}