Skip to content

Commit a735911

Browse files
authored
chore(metrics): properly detect rate limits (#3499)
* chore: detect more error codes * chore: add delay between successful calls * chore: set max timeout for the workflow * fix: modified modules was incorrect for usage-metrics
1 parent c31cec0 commit a735911

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

.github/workflows/usage-metrics.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ env:
2020
jobs:
2121
collect-metrics:
2222
runs-on: ubuntu-latest
23+
timeout-minutes: 120
2324
steps:
2425
- name: Checkout repository
2526
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

scripts/changed-modules.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ for file in $modified_files; do
170170
fi
171171
elif [[ $file == modulegen/* ]]; then
172172
modified_modules+=("\"modulegen\"")
173+
elif [[ $file == usage-metrics/* ]]; then
174+
modified_modules+=("\"usage-metrics\"")
173175
else
174176
# a file from the core module is modified, so include all modules in the list and stop the loop
175177
# check if the file is in one of the excluded modules

usage-metrics/collect-metrics.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ func collectMetrics(versions []string, csvPath string) error {
7878

7979
metrics = append(metrics, metric)
8080
fmt.Printf("Successfully queried: %s has %d usages on %s\n", version, count, metric.Date)
81+
82+
// Add delay to avoid rate limiting (30 requests/minute = 2 seconds between requests)
83+
if len(metrics) < len(versions) {
84+
log.Printf("Waiting 2 seconds before next query to avoid rate limiting...")
85+
time.Sleep(2 * time.Second)
86+
}
8187
}
8288

8389
// Sort metrics by version
@@ -127,7 +133,9 @@ func queryGitHubUsageWithRetry(version string) (int, error) {
127133
lastErr = err
128134

129135
// Check if it's a rate limit error
130-
if strings.Contains(err.Error(), "rate limit") || strings.Contains(err.Error(), "403") {
136+
if strings.Contains(err.Error(), "rate limit") ||
137+
strings.Contains(err.Error(), "403") ||
138+
strings.Contains(err.Error(), "429") {
131139
log.Printf("Rate limit hit for version %s, will retry with backoff", version)
132140
continue
133141
}

0 commit comments

Comments
 (0)