Skip to content

Commit e54f808

Browse files
committed
fix: allow valid API rate limit metrics bucket sizes
1 parent daee8bb commit e54f808

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎apps/webapp/app/env.server.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,6 @@ const EnvironmentSchema = z
779779
.number()
780780
.int()
781781
.positive()
782-
.multipleOf(10)
783782
.refine((seconds) => 60 % seconds === 0 || seconds % 60 === 0, {
784783
message: "must divide or be a multiple of 60 so buckets align to minute boundaries",
785784
})

‎apps/webapp/test/env.server.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,41 @@ describe("webapp environment secrets", () => {
116116
expect(env.GITHUB_APP_CLIENT_SECRET).toBe("client-secret");
117117
});
118118
});
119+
120+
describe("API_RATE_LIMIT_METRICS_BUCKET_SECONDS", () => {
121+
afterEach(() => {
122+
process.env = originalEnv;
123+
vi.resetModules();
124+
});
125+
126+
it("defaults to 10 when unset", async () => {
127+
process.env = { ...requiredEnv };
128+
delete process.env.API_RATE_LIMIT_METRICS_BUCKET_SECONDS;
129+
130+
const { env } = await import("../app/env.server");
131+
132+
expect(env.API_RATE_LIMIT_METRICS_BUCKET_SECONDS).toBe(10);
133+
});
134+
135+
it.each(["10", "15", "20", "30", "60", "120"])(
136+
"accepts valid divisor or multiple of 60: %s",
137+
async (value) => {
138+
process.env = { ...requiredEnv, API_RATE_LIMIT_METRICS_BUCKET_SECONDS: value };
139+
140+
const { env } = await import("../app/env.server");
141+
142+
expect(env.API_RATE_LIMIT_METRICS_BUCKET_SECONDS).toBe(Number(value));
143+
}
144+
);
145+
146+
it.each(["7", "0", "-10", "15.5", "not-a-number"])(
147+
"rejects invalid bucket seconds: %s",
148+
async (value) => {
149+
process.env = { ...requiredEnv, API_RATE_LIMIT_METRICS_BUCKET_SECONDS: value };
150+
151+
await expect(import("../app/env.server")).rejects.toThrow(
152+
"API_RATE_LIMIT_METRICS_BUCKET_SECONDS"
153+
);
154+
}
155+
);
156+
});

‎docs/self-hosting/env/webapp.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mode: "wide"
7979
| `API_RATE_LIMIT_JWT_WINDOW` | No | 1m | API rate limit JWT window. |
8080
| `API_RATE_LIMIT_JWT_TOKENS` | No | 60 | API rate limit JWT tokens. |
8181
| `API_RATE_LIMIT_METRICS_ENABLED` | No | 0 | Record API rate limit usage into the `metrics` table: `1` for everyone, `allowlist` for flagged organizations. |
82-
| `API_RATE_LIMIT_METRICS_BUCKET_SECONDS` | No | 10 | Aggregation bucket width for API rate limit metrics: 10, 20, 30 or a multiple of 60. |
82+
| `API_RATE_LIMIT_METRICS_BUCKET_SECONDS` | No | 10 | Aggregation bucket width for API rate limit metrics: any divisor of 60 (e.g. 10, 15, 20, 30) or a multiple of 60. |
8383
| `API_RATE_LIMIT_METRICS_FLUSH_INTERVAL_MS` | No | 10000 | How often aggregated API rate limit metrics are flushed to ClickHouse. |
8484
| `API_RATE_LIMIT_METRICS_MAX_ENTRIES` | No | 10000 | Cap on distinct (environment, bucket) entries held between flushes. |
8585
| `API_RATE_LIMIT_METRICS_WAIT_FOR_ASYNC_INSERT` | No | 0 | Wait for ClickHouse to write each async insert of API rate limit metrics, surfacing write failures. |

0 commit comments

Comments
 (0)