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.

chore(ci): add trigger context to rate-limit monitor (#61007)

Raúl Negrón-Oterod9109d3733a0
diff --git a/.github/scripts/monitor-github-rate-limit.js b/.github/scripts/monitor-github-rate-limit.js
index 006edf35a5ed..59f2460914ba 100644
--- a/.github/scripts/monitor-github-rate-limit.js
+++ b/.github/scripts/monitor-github-rate-limit.js
@@ -29,7 +29,30 @@ async function captureEvent({ fetchImpl, posthogToken, event, distinctId, proper
}
}
-function buildProperties({ resource, snapshot, observedAt, observedAtSeconds, repo, runId }) {
+// The workflow runs on every PR open/synchronize and master push, so each sample
+// can be attributed to the event that triggered it. Capturing that context turns
+// the aggregate /rate_limit series into a per-trigger breakdown downstream in
+// PostHog — which event types and PR sizes precede the steepest core burn.
+function buildTrigger(context) {
+ const payload = context.payload || {}
+ const pr = payload.pull_request || null
+ const num = (value) => (typeof value === 'number' ? value : null)
+ // PR head refs are short (`feature/foo`); push refs are qualified
+ // (`refs/heads/master`). Normalize to the short form so the column is uniform.
+ const ref = pr?.head?.ref ?? payload.ref ?? null
+ return {
+ trigger_event: context.eventName || null,
+ trigger_action: payload.action || null,
+ head_ref: ref ? ref.replace(/^refs\/heads\//, '') : null,
+ pr_number: num(pr?.number),
+ pr_author: pr?.user?.login ?? null,
+ pr_changed_files: num(pr?.changed_files),
+ pr_additions: num(pr?.additions),
+ pr_deletions: num(pr?.deletions),
+ }
+}
+
+function buildProperties({ resource, snapshot, observedAt, observedAtSeconds, repo, runId, trigger }) {
const used = typeof snapshot.used === 'number' ? snapshot.used : snapshot.limit - snapshot.remaining
const utilization = snapshot.limit > 0 ? used / snapshot.limit : 0
return {
@@ -44,6 +67,7 @@ function buildProperties({ resource, snapshot, observedAt, observedAtSeconds, re
source: 'github_token',
observed_at: observedAt,
workflow_run_id: runId || null,
+ ...trigger,
}
}
@@ -54,6 +78,7 @@ module.exports = async ({ github, context, core }, { now: _now, fetch: _fetch }
const observedAtSeconds = Math.floor(observedAtDate.getTime() / 1000)
const repo = `${context.repo.owner}/${context.repo.repo}`
const runId = process.env.GITHUB_RUN_ID || null
+ const trigger = buildTrigger(context)
const posthogToken = process.env.POSTHOG_DEVEX_PROJECT_API_TOKEN
if (!posthogToken) {
@@ -70,7 +95,7 @@ module.exports = async ({ github, context, core }, { now: _now, fetch: _fetch }
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 })
+ const properties = buildProperties({ resource, snapshot, observedAt, observedAtSeconds, repo, runId, trigger })
core.info(`${resource}: ${properties.remaining}/${properties.limit} remaining (resets ${properties.reset_at})`)
try {
await captureEvent({
@@ -94,4 +119,5 @@ module.exports = async ({ github, context, core }, { now: _now, fetch: _fetch }
}
module.exports.buildProperties = buildProperties
+module.exports.buildTrigger = buildTrigger
module.exports.captureEvent = captureEvent
diff --git a/.github/scripts/monitor-github-rate-limit.test.js b/.github/scripts/monitor-github-rate-limit.test.js
index 06b2191f42c2..32b88a4e7c8b 100644
--- a/.github/scripts/monitor-github-rate-limit.test.js
+++ b/.github/scripts/monitor-github-rate-limit.test.js
@@ -86,6 +86,62 @@ describe('monitor-github-rate-limit', () => {
expect(core.warning).toHaveBeenCalledWith(expect.stringContaining('capture 500'))
})
+ test('captures the triggering event context on each sample', async () => {
+ process.env.POSTHOG_DEVEX_PROJECT_API_TOKEN = 'devex-key'
+ const captured = []
+ const fetchMock = jest.fn((_url, opts) => {
+ captured.push(JSON.parse(opts.body))
+ return fetchOk()
+ })
+ const github = createGithubMock({ core: snapshot({ remaining: 3000, limit: 15000 }) })
+ const core = createCore()
+ const prContext = {
+ repo: { owner: 'PostHog', repo: 'posthog' },
+ eventName: 'pull_request',
+ payload: {
+ action: 'synchronize',
+ pull_request: {
+ number: 12345,
+ head: { ref: 'feature/foo' },
+ user: { login: 'octocat' },
+ changed_files: 7,
+ additions: 120,
+ deletions: 4,
+ },
+ },
+ }
+
+ await monitor({ github, context: prContext, core }, { now: () => T_BASE, fetch: fetchMock })
+
+ expect(captured[0].properties).toMatchObject({
+ trigger_event: 'pull_request',
+ trigger_action: 'synchronize',
+ head_ref: 'feature/foo',
+ pr_number: 12345,
+ pr_author: 'octocat',
+ pr_changed_files: 7,
+ pr_additions: 120,
+ pr_deletions: 4,
+ })
+ })
+
+ test.each([
+ ['push', { ref: 'refs/heads/master' }, 'master'],
+ ['schedule', {}, null],
+ ['workflow_dispatch', {}, null],
+ ])('buildTrigger nulls PR fields and resolves head_ref for non-PR event %s', (eventName, payload, expectedRef) => {
+ expect(monitor.buildTrigger({ eventName, payload })).toMatchObject({
+ trigger_event: eventName,
+ trigger_action: null,
+ head_ref: expectedRef,
+ pr_number: null,
+ pr_author: null,
+ pr_changed_files: null,
+ pr_additions: null,
+ pr_deletions: null,
+ })
+ })
+
test('skips emission when devex token is not configured', async () => {
const fetchMock = jest.fn(fetchOk)
const github = createGithubMock({ core: snapshot({ remaining: 1, limit: 15000 }) })