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(cdp): report zero depth for a drained cyclotron queue (#92411)

Daniel Marchuke3cff050ac4e
diff --git a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts
index 4b14cdb61023..f3f3601c48d1 100644
--- a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts
+++ b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts
@@ -1,4 +1,5 @@
import { Pool } from 'pg'
+import { register } from 'prom-client'
import { v7 as uuidv7 } from 'uuid'
import { parseJSON } from '~/common/utils/json-parse'
@@ -158,6 +159,12 @@ async function dequeueOneBatch(worker: CyclotronV2Worker, timeoutMs = 2000): Pro
// ── Tests ────────────────────────────────────────────────────────────
+async function gaugeValueForQueue(queue: string): Promise<number | null> {
+ const metric = await register.getSingleMetricAsString('cdp_cyclotron_v2_queue_depth')
+ const line = metric.split('\n').find((l) => l.includes(`queue="${queue}"`))
+ return line ? Number(line.trim().split(' ').pop()) : null
+}
+
describe('Cyclotron V2', () => {
jest.setTimeout(3000)
// Declared here, assigned in beforeAll
@@ -2489,6 +2496,24 @@ describe('Cyclotron V2', () => {
}
)
+ it('measureQueueDepths reports 0 for a queue that drains', async () => {
+ // GROUP BY returns no row for an empty queue. Without the zero write the gauge
+ // keeps the last depth, and a depth alert then fires on an idle queue.
+ const jobId = uuidv7()
+ await insertRawJob({ id: jobId, queue_name: 'queue-drains', status: 'available' })
+
+ const janitor = createJanitor({ stallTimeoutMs: 60_000 })
+ const before = await janitor.runOnce()
+ expect(before.depths.get('queue-drains')).toBe(1)
+ expect(await gaugeValueForQueue('queue-drains')).toBe(1)
+
+ await assertPool.query('DELETE FROM cyclotron_jobs WHERE id = $1', [jobId])
+ await janitor.runOnce()
+ await janitor.stop()
+
+ expect(await gaugeValueForQueue('queue-drains')).toBe(0)
+ })
+
it('measureQueueDepths returns correct counts per queue', async () => {
await insertRawJob({ id: uuidv7(), queue_name: 'queue-a', status: 'available' })
await insertRawJob({ id: uuidv7(), queue_name: 'queue-a', status: 'available' })
diff --git a/nodejs/src/cdp/services/cyclotron-v2/janitor.ts b/nodejs/src/cdp/services/cyclotron-v2/janitor.ts
index 297726a80e08..685ec53deffc 100644
--- a/nodejs/src/cdp/services/cyclotron-v2/janitor.ts
+++ b/nodejs/src/cdp/services/cyclotron-v2/janitor.ts
@@ -91,6 +91,8 @@ interface PoisonRow {
export class CyclotronV2Janitor {
private pool: Pool
private intervalHandle: ReturnType<typeof setInterval> | null = null
+ // Queue names this process has seen hold work, so a drained queue can report 0.
+ private readonly seenQueues = new Set<string>()
private readonly cleanupBatchSize: number
private readonly cleanupIntervalMs: number
@@ -481,9 +483,21 @@ export class CyclotronV2Janitor {
for (const row of result.rows) {
const count = parseInt(row.count, 10)
depths.set(row.queue_name, count)
+ this.seenQueues.add(row.queue_name)
queueDepthGauge.labels({ queue: row.queue_name }).set(count)
}
+ // GROUP BY returns no row for a queue that is empty. Without the write below,
+ // that queue keeps the last depth this process set, and an alert on the value
+ // fires while the queue is idle. Write 0 for every queue this process saw
+ // before. The set holds only observed queue names, so it needs no maintenance
+ // and it adds no series for a queue that does not use this table.
+ for (const queue of this.seenQueues) {
+ if (!depths.has(queue)) {
+ queueDepthGauge.labels({ queue }).set(0)
+ }
+ }
+
return depths
}