Skip to content

Commit 7d30948

Browse files
committed
Add rate limit telemetry plugin
Is not enabled as it is not wired up to anything yet
1 parent 95bf9c5 commit 7d30948

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/api/rateLimitTelemetry.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {Octokit} from "@octokit/core";
2+
import {EventEmitter} from "vscode";
3+
4+
// Exported variables to track rate limits
5+
export interface RateLimit {
6+
remaining: number | null;
7+
limit: number | null;
8+
reset: Date | null;
9+
used: number | null;
10+
}
11+
12+
export const rateLimit: RateLimit = {
13+
remaining: null,
14+
limit: null,
15+
reset: null,
16+
used: null
17+
};
18+
19+
const rateLimitUpdatedEvent = new EventEmitter<RateLimit>();
20+
export const onRateLimitUpdated = rateLimitUpdatedEvent.event;
21+
22+
/**
23+
* Octokit plugin to track GitHub API rate limits
24+
*/
25+
export const rateLimitTelemetryPlugin = (octokit: Octokit) => {
26+
octokit.hook.wrap("request", async (request, options) => {
27+
const response = await request(options);
28+
29+
// Extract rate limit information from headers
30+
if (response.headers) {
31+
const remaining = response.headers["x-ratelimit-remaining"];
32+
const limit = response.headers["x-ratelimit-limit"];
33+
const reset = response.headers["x-ratelimit-reset"];
34+
const used = response.headers["x-ratelimit-used"];
35+
36+
// Update exported variables if headers are present
37+
if (remaining) rateLimit.remaining = parseInt(String(remaining));
38+
if (limit) rateLimit.limit = parseInt(String(limit));
39+
if (reset) rateLimit.reset = new Date(parseInt(String(reset)) * 1000);
40+
if (used) rateLimit.used = parseInt(String(used));
41+
}
42+
rateLimitUpdatedEvent.fire(rateLimit);
43+
44+
return response;
45+
});
46+
47+
return {
48+
ratelimit: rateLimit
49+
};
50+
};

0 commit comments

Comments
 (0)