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): drop-rule rate limit not enforcing the configured rate (#66981)

Frank Hamand230701d5b9cc
diff --git a/nodejs/src/common/services/keyed-rate-limiter.service.test.ts b/nodejs/src/common/services/keyed-rate-limiter.service.test.ts
index ff89f7d3986d..fb46c3306c83 100644
--- a/nodejs/src/common/services/keyed-rate-limiter.service.test.ts
+++ b/nodejs/src/common/services/keyed-rate-limiter.service.test.ts
@@ -278,6 +278,52 @@ describe('KeyedRateLimiterService', () => {
})
})
+ describe('rateLimitMany (scriptVersion v3 — floor-drain on overdraft)', () => {
+ const buildV3Limiter = (name: string, overrides: Partial<{ bucketSize: number; refillRate: number }> = {}) =>
+ new KeyedRateLimiterService(
+ {
+ name,
+ bucketSize: overrides.bucketSize ?? 100,
+ refillRate: overrides.refillRate ?? 10,
+ ttlSeconds: 60 * 60 * 24,
+ scriptVersion: 'v3',
+ },
+ redis
+ )
+
+ it('floor-drains the available tokens on overdraft instead of preserving the balance', async () => {
+ const limiter = buildV3Limiter('test-v3-drain')
+ await deleteKeysWithPrefix(redis, limiter.getKeyPrefix())
+
+ await limiter.rateLimitMany([{ id: 'team-1', cost: 100 }])
+ advanceTime(1000)
+ const res = await limiter.rateLimitMany([{ id: 'team-1', cost: 1000 }])
+
+ expect(res[0][1].isRateLimited).toBe(true)
+ expect(res[0][1].tokensBefore).toBe(10)
+ const bucket = await readBucket(`${limiter.getKeyPrefix()}/team-1`)
+ expect(Number(bucket.pool)).toBe(0)
+ })
+
+ it('caps sustained-overload admission at refillRate per batch (drop-rule regression)', async () => {
+ const limiter = buildV3Limiter('test-v3-overload')
+ await deleteKeysWithPrefix(redis, limiter.getKeyPrefix())
+
+ await limiter.rateLimitMany([{ id: 'team-1', cost: 100 }])
+
+ const budgets: number[] = []
+ for (let i = 0; i < 10; i++) {
+ advanceTime(1000)
+ const res = await limiter.rateLimitMany([{ id: 'team-1', cost: 1000 }])
+ budgets.push(res[0][1].tokensBefore)
+ }
+
+ for (const budget of budgets) {
+ expect(budget).toBeLessThanOrEqual(10)
+ }
+ })
+ })
+
describe('rateLimitGrouped (V3 + coalesced + per-input fan-out)', () => {
it('returns one decision per input request (parallel to input order)', async () => {
const limiter = buildLimiter('grouped-shape')
diff --git a/nodejs/src/common/services/keyed-rate-limiter.service.ts b/nodejs/src/common/services/keyed-rate-limiter.service.ts
index 196c28eec56d..26a29516cf68 100644
--- a/nodejs/src/common/services/keyed-rate-limiter.service.ts
+++ b/nodejs/src/common/services/keyed-rate-limiter.service.ts
@@ -26,6 +26,7 @@ export interface KeyedRateLimiterConfig {
ttlSeconds?: number
/** When false, throw if the Redis pipeline returns null instead of fail-open allowing all. Default: true (fail open). */
failOpen?: boolean
+ scriptVersion?: 'v2' | 'v3'
}
export interface KeyedRateLimitRequest {
@@ -105,11 +106,17 @@ export class KeyedRateLimiterService implements KeyedRateLimiter {
return bucketSize
})
+ const useV3 = this.config.scriptVersion === 'v3'
const res = await this.redis.usePipeline(
{ name: `keyed-rate-limiter:${this.config.name}`, failOpen: true },
(pipeline) => {
requests.forEach((req) => {
- pipeline.checkRateLimitV2(...this.rateLimitArgs(req))
+ const args = this.rateLimitArgs(req)
+ if (useV3) {
+ pipeline.checkRateLimitV3(...args)
+ } else {
+ pipeline.checkRateLimitV2(...args)
+ }
})
}
)
diff --git a/nodejs/src/logs/sampling/logs-sampling.service.test.ts b/nodejs/src/logs/sampling/logs-sampling.service.test.ts
index 8761ddb94746..7b1f995e3202 100644
--- a/nodejs/src/logs/sampling/logs-sampling.service.test.ts
+++ b/nodejs/src/logs/sampling/logs-sampling.service.test.ts
@@ -72,7 +72,7 @@ describe('LogsSamplingService', () => {
const mockRedis: RedisV2 = {
useClient: jest.fn(() => Promise.resolve(null)),
usePipeline: jest.fn((_opts, cb) => {
- const pipeline = { checkRateLimitV2: jest.fn() } as unknown as RedisClientPipeline
+ const pipeline = { checkRateLimitV3: jest.fn() } as unknown as RedisClientPipeline
cb(pipeline)
const pipelineResult: [Error | null, any][] = [[null, [2, 0] as const]]
return Promise.resolve(pipelineResult)
@@ -115,7 +115,7 @@ describe('LogsSamplingService', () => {
const mockRedis: RedisV2 = {
useClient: jest.fn(() => Promise.resolve(null)),
usePipeline: jest.fn((_opts, cb) => {
- const pipeline = { checkRateLimitV2: jest.fn() } as unknown as RedisClientPipeline
+ const pipeline = { checkRateLimitV3: jest.fn() } as unknown as RedisClientPipeline
cb(pipeline)
const pipelineResult: [Error | null, any][] = [[null, [1, 0] as const]]
return Promise.resolve(pipelineResult)
@@ -154,7 +154,7 @@ describe('LogsSamplingService', () => {
const mockRedis: RedisV2 = {
useClient: jest.fn(() => Promise.resolve(null)),
usePipeline: jest.fn((_opts, cb) => {
- const pipeline = { checkRateLimitV2: jest.fn() } as unknown as RedisClientPipeline
+ const pipeline = { checkRateLimitV3: jest.fn() } as unknown as RedisClientPipeline
cb(pipeline)
const pipelineResult: [Error | null, any][] = [[null, [1024, 0] as const]]
return Promise.resolve(pipelineResult)
@@ -200,7 +200,7 @@ describe('LogsSamplingService', () => {
const mockRedis: RedisV2 = {
useClient: jest.fn(() => Promise.resolve(null)),
usePipeline: jest.fn((_opts, cb) => {
- const pipeline = { checkRateLimitV2: jest.fn() } as unknown as RedisClientPipeline
+ const pipeline = { checkRateLimitV3: jest.fn() } as unknown as RedisClientPipeline
cb(pipeline)
const pipelineResult: [Error | null, any][] = [[null, [1024, 0] as const]]
return Promise.resolve(pipelineResult)
diff --git a/nodejs/src/logs/sampling/logs-sampling.service.ts b/nodejs/src/logs/sampling/logs-sampling.service.ts
index 282d01c41cc5..d1658e2da83e 100644
--- a/nodejs/src/logs/sampling/logs-sampling.service.ts
+++ b/nodejs/src/logs/sampling/logs-sampling.service.ts
@@ -98,7 +98,10 @@ export class LogsSamplingService {
private rateLimiter: KeyedRateLimiterService
constructor(redis: RedisV2, ttlSeconds: number) {
- this.rateLimiter = new KeyedRateLimiterService({ name: 'logs-sampling-rate', ttlSeconds }, redis)
+ this.rateLimiter = new KeyedRateLimiterService(
+ { name: 'logs-sampling-rate', ttlSeconds, scriptVersion: 'v3' },
+ redis
+ )
}
@instrumented({