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-ingestion): honor filter_group on rate_limit drop rules (#59533)

Daniel Visca5f22b90a2095
diff --git a/nodejs/src/logs-ingestion/sampling/compile-rules.ts b/nodejs/src/logs-ingestion/sampling/compile-rules.ts
index 3c801d825741..f7ee8b693aa5 100644
--- a/nodejs/src/logs-ingestion/sampling/compile-rules.ts
+++ b/nodejs/src/logs-ingestion/sampling/compile-rules.ts
@@ -241,6 +241,11 @@ export function compileRuleSet(rows: SamplingRuleRow[]): CompiledRuleSet {
if (rateLimit) {
hasRateLimitRules = true
}
+ // rate_limit rules also accept config.filter_group as a universal scope (the
+ // drop-rule UI now writes service-scoping there instead of scope_service).
+ // The path_drop branch above already parsed any filter_group on path_drop rows;
+ // do the same parse for rate_limit so the evaluator can honor it.
+ filterGroup = parseFilterGroup(row.config?.filter_group)
}
rules.push({
id: row.id,
diff --git a/nodejs/src/logs-ingestion/sampling/evaluate.test.ts b/nodejs/src/logs-ingestion/sampling/evaluate.test.ts
index 8b392fdc118d..6229da06c4a8 100644
--- a/nodejs/src/logs-ingestion/sampling/evaluate.test.ts
+++ b/nodejs/src/logs-ingestion/sampling/evaluate.test.ts
@@ -207,6 +207,87 @@ describe('evaluateLogRecord', () => {
expect(classifySamplingRecord(rules, rec).kind).toBe('resolved')
})
+ describe('rate_limit with config.filter_group', () => {
+ // Same outer envelope shape the drop-rules UI emits, matching the path_drop tests below.
+ const wrap = (inner: object): object => ({ type: 'AND', values: [inner] })
+
+ it('classifySamplingRecord returns rate_limit when filter_group matches', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-fg',
+ rule_type: 'rate_limit',
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ logs_per_second: 10,
+ filter_group: wrap({
+ type: 'AND',
+ values: [{ key: 'service.name', operator: 'exact', value: 'api' }],
+ }),
+ },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'api'
+ expect(classifySamplingRecord(rules, rec)).toEqual({ kind: 'rate_limit', ruleId: 'rl-fg' })
+ })
+
+ it('classifySamplingRecord skips rate_limit when filter_group does not match', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-fg',
+ rule_type: 'rate_limit',
+ scope_service: null,
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ logs_per_second: 10,
+ filter_group: wrap({
+ type: 'AND',
+ values: [{ key: 'service.name', operator: 'exact', value: 'api' }],
+ }),
+ },
+ },
+ ])
+ const rec = baseRecord()
+ rec.service_name = 'other'
+ // Rule has no other scoping, so without filter_group honor, this would return rate_limit
+ // and rate-limit every log on the team. With the fix, it falls through to keep.
+ expect(classifySamplingRecord(rules, rec).kind).toBe('resolved')
+ })
+
+ it('classifySamplingRecord still requires scope_service AND filter_group when both set', () => {
+ const rules = compileRuleSet([
+ {
+ id: 'rl-both',
+ rule_type: 'rate_limit',
+ scope_service: 'api',
+ scope_path_pattern: null,
+ scope_attribute_filters: [],
+ config: {
+ logs_per_second: 10,
+ filter_group: wrap({
+ type: 'AND',
+ values: [{ key: 'severity_text', operator: 'exact', value: 'error' }],
+ }),
+ },
+ },
+ ])
+ const apiInfo = baseRecord()
+ apiInfo.service_name = 'api'
+ apiInfo.severity_text = 'info'
+ // scope_service matches but filter_group doesn't → skip.
+ expect(classifySamplingRecord(rules, apiInfo).kind).toBe('resolved')
+
+ const apiError = baseRecord()
+ apiError.service_name = 'api'
+ apiError.severity_text = 'error'
+ // Both match → rate_limit.
+ expect(classifySamplingRecord(rules, apiError)).toEqual({ kind: 'rate_limit', ruleId: 'rl-both' })
+ })
+ })
+
it('path_drop match runs before rate_limit in rule order', () => {
const rules = compileRuleSet([
{
diff --git a/nodejs/src/logs-ingestion/sampling/evaluate.ts b/nodejs/src/logs-ingestion/sampling/evaluate.ts
index 9abb30a5e6fc..139946e07c9d 100644
--- a/nodejs/src/logs-ingestion/sampling/evaluate.ts
+++ b/nodejs/src/logs-ingestion/sampling/evaluate.ts
@@ -231,10 +231,16 @@ export function classifySamplingRecord(teamRuleSet: CompiledRuleSet | null, reco
return { kind: 'resolved', decision: SAMPLING_DECISION_SAMPLE_DROPPED, ruleId: rule.id }
}
if (rule.ruleType === 'rate_limit') {
- if (rule.rateLimit) {
- return { kind: 'rate_limit', ruleId: rule.id }
+ if (!rule.rateLimit) {
+ continue
}
- continue
+ // Mirror path_drop: filter_group must match if set. scope_service was
+ // already checked by matchesScope; this adds the modern scoping that
+ // the drop-rule UI now writes for rate_limit rules.
+ if (rule.filterGroup && !matchFilterGroup(rule.filterGroup, record)) {
+ continue
+ }
+ return { kind: 'rate_limit', ruleId: rule.id }
}
}
return { kind: 'resolved', decision: SAMPLING_DECISION_KEEP, ruleId: null }