Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

refactor(api): log rate limiter Redis failures as structured lines

spacesdrivee148c36b9097
diff --git a/backend/lib/rateLimit/limiter.ts b/backend/lib/rateLimit/limiter.ts
index 9d5336b..fc56125 100644
--- a/backend/lib/rateLimit/limiter.ts
+++ b/backend/lib/rateLimit/limiter.ts
@@ -2,6 +2,7 @@ import type { Redis } from '@upstash/redis';
import { limitOf } from './algorithms.ts';
import type { Decision, LimitConfig } from './algorithms.ts';
import { MemoryStore, RedisStore } from './stores.ts';
+import { createLogger, errorFields } from '../logger.ts';
export interface RateLimitRule {
name: string;
@@ -66,7 +67,7 @@ export async function checkRateLimit(
decision = await new RedisStore(redis).check(key, rule.config, now);
} catch (err) {
redisDownUntil = now + REDIS_RETRY_AFTER_MS;
- console.error(`Rate limit ${rule.name}: Redis check failed, counting in memory for 30 seconds:`, (err as Error).message);
+ createLogger({ rule: rule.name }).error('rate limit Redis check failed; counting in memory for 30 seconds', errorFields(err));
decision = await memoryStore.check(key, rule.config, now);
}
} else {
diff --git a/backend/tests/lib/rateLimit/limiter.test.ts b/backend/tests/lib/rateLimit/limiter.test.ts
index 3191473..03aab16 100644
--- a/backend/tests/lib/rateLimit/limiter.test.ts
+++ b/backend/tests/lib/rateLimit/limiter.test.ts
@@ -90,7 +90,9 @@ test('a Redis failure falls back to counting in memory instead of failing the re
// one failure opens the circuit, so the next checks skip Redis instead of waiting on it again
assert.equal(calls.length, 1);
assert.equal(logged.length, 1);
- assert.match(String(logged[0][0]), /Rate limit test: Redis check failed/);
+ assert.deepEqual(JSON.parse(String(logged[0][0])), {
+ level: 'error', message: 'rate limit Redis check failed; counting in memory for 30 seconds', rule: 'test', error: 'connection refused', errorName: 'Error',
+ });
await checkRateLimit(redis, rule, 'user-2', 100 + 30_000);
assert.equal(calls.length, 2);