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(ci): read depot rate limit from response headers (#107940)

Raúl Negrón-Otero4ead3312bc5e
diff --git a/.depot/workflows/monitor-depot-ambient-rate-limit.yml b/.depot/workflows/monitor-depot-ambient-rate-limit.yml
index 6e53863dc6a1..0329a6d573cf 100644
--- a/.depot/workflows/monitor-depot-ambient-rate-limit.yml
+++ b/.depot/workflows/monitor-depot-ambient-rate-limit.yml
@@ -1,19 +1,25 @@
-# Depot CI jobs call the GitHub API with the ambient token of the Depot Code Access app.
-# That installation has one hourly bucket, shared by every Depot CI job in every repository
-# the app is installed on, and GitHub Actions cannot read it. This workflow reads it from a
-# Depot job and emits the same `github_rate_limit_observed` event as
-# .github/workflows/monitor-github-rate-limit.yml, with source `depot_ambient`, so the
-# core-bucket alert in the DevEx project covers it.
+# Depot CI jobs call the GitHub API with an ambient installation token of the Depot Code
+# Access app. Depot hands out tokens from more than one installation, each with its own hourly
+# bucket, shared by every Depot CI job and by Depot's own check reporting, and GitHub Actions
+# cannot read them. This workflow reads them from Depot jobs and emits the same
+# `github_rate_limit_observed` event as .github/workflows/monitor-github-rate-limit.yml, with
+# source `depot_ambient`, so the core-bucket alert in the DevEx project covers it.
+# `installation_repositories` on each event tells the installations apart.
name: Monitor Depot ambient rate limit
on:
schedule:
- - cron: '15,45 * * * *'
+ # A bucket can run out within minutes of its hourly reset, so a slower poll misses it.
+ - cron: '*/10 * * * *'
workflow_dispatch:
permissions:
contents: read
jobs:
poll:
name: Poll the Depot ambient token bucket
+ # Each job draws its token independently, so several samplers per poll reach every installation.
+ strategy:
+ matrix:
+ sampler: [1, 2, 3]
runs-on: depot-ubuntu-24.04
timeout-minutes: 3
steps:
@@ -30,4 +36,4 @@ jobs:
with:
script: |
const script = require('./.github/scripts/monitor-github-rate-limit.js')
- await script({ github, context, core }, { source: 'depot_ambient' })
+ await script({ github, context, core }, { source: 'depot_ambient', probe: 'headers' })
diff --git a/.github/scripts/monitor-github-rate-limit.js b/.github/scripts/monitor-github-rate-limit.js
index 2d15a498d164..381d5072298c 100644
--- a/.github/scripts/monitor-github-rate-limit.js
+++ b/.github/scripts/monitor-github-rate-limit.js
@@ -125,7 +125,35 @@ async function reportMintFailures({ context, core }, { mints, now: _now, fetch:
return failed.map(({ source }) => source)
}
-module.exports = async ({ github, context, core }, { now: _now, fetch: _fetch, source: _source } = {}) => {
+// Depot CI's ambient installation token reads zero usage from /rate_limit, while the
+// x-ratelimit-* headers on any real response carry the bucket's true count. This probe
+// costs one request. Depot hands out tokens from more than one installation, so the
+// installation's repositories tell its buckets apart downstream.
+async function readCoreFromHeaders(github) {
+ let response
+ try {
+ response = await github.request('GET /installation/repositories', { per_page: 100 })
+ } catch (err) {
+ // A spent bucket answers 403 and still sends the headers, which is the reading that matters most.
+ if (!err.response?.headers) {
+ throw err
+ }
+ response = err.response
+ }
+ const header = (name) => Number(response.headers[name])
+ const repositories = (response.data?.repositories || []).map((repository) => repository.full_name).sort()
+ return {
+ snapshot: {
+ limit: header('x-ratelimit-limit'),
+ remaining: header('x-ratelimit-remaining'),
+ used: header('x-ratelimit-used'),
+ reset: header('x-ratelimit-reset'),
+ },
+ installationRepositories: repositories.length ? repositories.join(',') : null,
+ }
+}
+
+module.exports = async ({ github, context, core }, { now: _now, fetch: _fetch, source: _source, probe } = {}) => {
const source = _source || DEFAULT_SOURCE
const fetchImpl = _fetch || fetch
const observedAtDate = _now ? _now() : new Date()
@@ -143,14 +171,25 @@ module.exports = async ({ github, context, core }, { now: _now, fetch: _fetch, s
return
}
- const { data } = await github.rest.rateLimit.get()
- const resources = data?.resources || {}
+ let resources
+ let extra = {}
+ if (probe === 'headers') {
+ const { snapshot, installationRepositories } = await readCoreFromHeaders(github)
+ resources = { core: snapshot }
+ extra = { installation_repositories: installationRepositories }
+ } else {
+ const { data } = await github.rest.rateLimit.get()
+ resources = data?.resources || {}
+ }
let emitted = 0
let failures = 0
for (const [resource, snapshot] of Object.entries(resources)) {
- if (!snapshot || typeof snapshot.limit !== 'number' || typeof snapshot.remaining !== 'number') {continue}
- const properties = buildProperties({ resource, snapshot, observedAt, observedAtSeconds, repo, runId, trigger, source })
+ if (!snapshot || !Number.isFinite(snapshot.limit) || !Number.isFinite(snapshot.remaining)) {continue}
+ const properties = {
+ ...buildProperties({ resource, snapshot, observedAt, observedAtSeconds, repo, runId, trigger, source }),
+ ...extra,
+ }
core.info(`[${source}] ${resource}: ${properties.remaining}/${properties.limit} remaining (resets ${properties.reset_at})`)
try {
await captureEvent({
diff --git a/.github/scripts/monitor-github-rate-limit.test.js b/.github/scripts/monitor-github-rate-limit.test.js
index 5262409334e4..915a3759d1f1 100644
--- a/.github/scripts/monitor-github-rate-limit.test.js
+++ b/.github/scripts/monitor-github-rate-limit.test.js
@@ -112,6 +112,51 @@ describe('monitor-github-rate-limit', () => {
})
})
+ for (const spent of [false, true]) {
+ it(`reads the core bucket from response headers when the bucket is ${spent ? 'spent' : 'healthy'}`, async () => {
+ process.env.POSTHOG_DEVEX_PROJECT_API_TOKEN = 'devex-key'
+ const captured = []
+ const fetchMock = recordingFn((_url, opts) => {
+ captured.push(JSON.parse(opts.body))
+ return fetchOk()
+ })
+ const used = spent ? 15000 : 8030
+ const headers = {
+ 'x-ratelimit-limit': '15000',
+ 'x-ratelimit-remaining': String(15000 - used),
+ 'x-ratelimit-used': String(used),
+ 'x-ratelimit-reset': String(T_BASE_SECONDS + 600),
+ }
+ const response = {
+ headers,
+ data: spent
+ ? { message: 'API rate limit exceeded' }
+ : { repositories: [{ full_name: 'PostHog/posthog-js' }, { full_name: 'PostHog/posthog' }] },
+ }
+ const github = {
+ // /rate_limit reads zero for this token, so the probe must never consult it.
+ rest: { rateLimit: { get: () => Promise.resolve({ data: { resources: { core: snapshot({ remaining: 15000, limit: 15000 }) } } }) } },
+ request: () => (spent ? Promise.reject(Object.assign(new Error('403'), { response })) : Promise.resolve(response)),
+ }
+
+ await monitor(
+ { github, context, core: createCore() },
+ { now: () => T_BASE, fetch: fetchMock, source: 'depot_ambient', probe: 'headers' }
+ )
+
+ assert.equal(captured.length, 1)
+ assertMatch(captured[0].properties, {
+ resource: 'core',
+ used,
+ remaining: 15000 - used,
+ limit: 15000,
+ reset_in_seconds: 600,
+ source: 'depot_ambient',
+ installation_repositories: spent ? null : 'PostHog/posthog,PostHog/posthog-js',
+ })
+ })
+ }
+
it('counts capture failures without aborting later emissions', async () => {
process.env.POSTHOG_DEVEX_PROJECT_API_TOKEN = 'devex-key'
let calls = 0