: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cdp): re-add cyclotron jobs-processed and batch-utilization metrics (#59605)
Daniel Marchuk51e6ba30465e
diff --git a/nodejs/src/cdp/cdp-e2e.test.ts b/nodejs/src/cdp/cdp-e2e.test.tsindex 8845c3fdc71d..e4cdb48d9c90 100644--- a/nodejs/src/cdp/cdp-e2e.test.ts+++ b/nodejs/src/cdp/cdp-e2e.test.ts@@ -111,7 +111,7 @@ describe('CDP Consumer loop', () => {...HOG_FILTERS_EXAMPLES.no_filters,})- const kafkaQueue = new CyclotronJobQueueKafka(hub.KAFKA_CLIENT_RACK, hub)+ const kafkaQueue = new CyclotronJobQueueKafka(hub.KAFKA_CLIENT_RACK, hub, hub.CONSUMER_BATCH_SIZE)const postgresV2Queue = new CyclotronJobQueuePostgresV2(hub.CONSUMER_BATCH_SIZE, hub)eventsConsumer = new CdpEventsConsumer(hub, createCdpConsumerDeps(hub, kafkaProducer), {diff --git a/nodejs/src/cdp/rerun/rerun-e2e.test.ts b/nodejs/src/cdp/rerun/rerun-e2e.test.tsindex 98c5758b53aa..20ed99c849fe 100644--- a/nodejs/src/cdp/rerun/rerun-e2e.test.ts+++ b/nodejs/src/cdp/rerun/rerun-e2e.test.ts@@ -190,7 +190,7 @@ describe('CDP hog invocation rerun e2e', () => {...HOG_FILTERS_EXAMPLES.no_filters,})- kafkaQueue = new CyclotronJobQueueKafka(hub.KAFKA_CLIENT_RACK, hub)+ kafkaQueue = new CyclotronJobQueueKafka(hub.KAFKA_CLIENT_RACK, hub, hub.CONSUMER_BATCH_SIZE)postgresV2Queue = new CyclotronJobQueuePostgresV2(hub.CONSUMER_BATCH_SIZE, hub)eventsConsumer = new CdpEventsConsumer(hub, createCdpConsumerDeps(hub, kafkaProducer), {diff --git a/nodejs/src/cdp/services/job-queue/job-queue-kafka.ts b/nodejs/src/cdp/services/job-queue/job-queue-kafka.tsindex 5257028e80fc..537ba975dc90 100644--- a/nodejs/src/cdp/services/job-queue/job-queue-kafka.ts+++ b/nodejs/src/cdp/services/job-queue/job-queue-kafka.ts@@ -14,7 +14,7 @@ import { logger } from '../../../utils/logger'import { CdpConfig } from '../../config'import { CyclotronJobInvocation, CyclotronJobInvocationResult, CyclotronJobQueueKind } from '../../types'import { JobQueue } from './job-queue.interface'-import { cdpJobSizeCompressedKb, cdpJobSizeKb, createInvocationSanitizer } from './shared'+import { cdpJobSizeCompressedKb, cdpJobSizeKb, createInvocationSanitizer, observeConsumedBatch } from './shared'export class CyclotronJobQueueKafka implements JobQueue {private kafkaConsumer?: KafkaConsumerInterface@@ -28,7 +28,8 @@ export class CyclotronJobQueueKafka implements JobQueue {private config: Pick<CdpConfig,'CDP_CYCLOTRON_COMPRESS_KAFKA_DATA' | 'CDP_CYCLOTRON_STRIP_PERSON_FROM_STATE_TEAMS'- >+ >,+ private consumerBatchSize: number) {this.sanitizer = createInvocationSanitizer(config)}@@ -163,6 +164,12 @@ export class CyclotronJobQueueKafka implements JobQueue {private async consumeKafkaBatch(messages: Message[]): Promise<{ backgroundTask: Promise<any> }> {if (messages.length === 0) {+ observeConsumedBatch({+ queue: this.queue!,+ source: 'kafka',+ batchSize: 0,+ maxBatchSize: this.consumerBatchSize,+ })return await this.consumeBatch!([])}@@ -184,6 +191,13 @@ export class CyclotronJobQueueKafka implements JobQueue {invocations.push(invocation)}+ observeConsumedBatch({+ queue: this.queue!,+ source: 'kafka',+ batchSize: invocations.length,+ maxBatchSize: this.consumerBatchSize,+ })+return await this.consumeBatch!(invocations)}}diff --git a/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts b/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.tsindex 640e94bea04f..6d0b99bc5111 100644--- a/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts+++ b/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts@@ -10,7 +10,7 @@ import { CdpConfig } from '../../config'import { CyclotronJobInvocation, CyclotronJobInvocationResult, CyclotronJobQueueKind } from '../../types'import { CyclotronV2DequeuedJob, CyclotronV2JobInit, CyclotronV2Manager, CyclotronV2Worker } from '../cyclotron-v2'import { JobQueue } from './job-queue.interface'-import { cdpJobSizeCompressedKb, cdpJobSizeKb, createInvocationSanitizer } from './shared'+import { cdpJobSizeCompressedKb, cdpJobSizeKb, createInvocationSanitizer, observeConsumedBatch } from './shared'const pendingJobsGauge = new Gauge({name: 'cdp_cyclotron_v2_pending_jobs',@@ -91,6 +91,13 @@ export class CyclotronJobQueuePostgresV2 implements JobQueue {pendingJobsGauge.set(this.pendingJobs.size)+ observeConsumedBatch({+ queue,+ source: 'postgres-v2',+ batchSize: invocations.length,+ maxBatchSize: this.consumerBatchSize,+ })+await consumeBatch(invocations)pendingJobsGauge.set(this.pendingJobs.size)diff --git a/nodejs/src/cdp/services/job-queue/job-queue-postgres.ts b/nodejs/src/cdp/services/job-queue/job-queue-postgres.tsindex 4cfd5679955a..8665f2cf3f6d 100644--- a/nodejs/src/cdp/services/job-queue/job-queue-postgres.ts+++ b/nodejs/src/cdp/services/job-queue/job-queue-postgres.ts@@ -23,7 +23,7 @@ import { captureException } from '../../../utils/posthog'import { CdpConfig } from '../../config'import { CyclotronJobInvocation, CyclotronJobInvocationResult, CyclotronJobQueueKind } from '../../types'import { JobQueue } from './job-queue.interface'-import { createInvocationSanitizer } from './shared'+import { createInvocationSanitizer, observeConsumedBatch } from './shared'/*** Legacy postgres v1 queue via @posthog/cyclotron.@@ -33,6 +33,7 @@ import { createInvocationSanitizer } from './shared'export class CyclotronJobQueuePostgres implements JobQueue {private cyclotronWorker?: CyclotronWorkerprivate cyclotronManager?: CyclotronManager+ private queue?: CyclotronJobQueueKindprivate consumeBatch?: (invocations: CyclotronJobInvocation[]) => Promise<{ backgroundTask: Promise<any> }>private sanitizer: ReturnType<typeof createInvocationSanitizer>@@ -82,6 +83,7 @@ export class CyclotronJobQueuePostgres implements JobQueue {queue: CyclotronJobQueueKind,consumeBatch: (invocations: CyclotronJobInvocation[]) => Promise<{ backgroundTask: Promise<any> }>) {+ this.queue = queuethis.consumeBatch = consumeBatchif (!this.config.CYCLOTRON_DATABASE_URL) {@@ -242,6 +244,13 @@ export class CyclotronJobQueuePostgres implements JobQueue {invocations.push(invocation)}+ observeConsumedBatch({+ queue: this.queue!,+ source: 'postgres',+ batchSize: invocations.length,+ maxBatchSize: this.consumerBatchSize,+ })+await this.consumeBatch!(invocations)// TODO: Ensure that all jobs eventually get acked!!!}diff --git a/nodejs/src/cdp/services/job-queue/shared.ts b/nodejs/src/cdp/services/job-queue/shared.tsindex 182bc4f19b17..ee5c1b6ef797 100644--- a/nodejs/src/cdp/services/job-queue/shared.ts+++ b/nodejs/src/cdp/services/job-queue/shared.ts@@ -1,9 +1,14 @@-import { Histogram } from 'prom-client'+import { Counter, Gauge, Histogram } from 'prom-client'import { buildIntegerMatcher } from '../../../config/config'import { ValueMatcher } from '../../../types'import { CdpConfig } from '../../config'-import { CyclotronJobInvocation, CyclotronJobInvocationResult } from '../../types'+import {+ CyclotronJobInvocation,+ CyclotronJobInvocationResult,+ CyclotronJobQueueKind,+ CyclotronJobQueueSource,+} from '../../types'export const cdpJobSizeKb = new Histogram({name: 'cdp_cyclotron_job_size_kb',@@ -19,6 +24,34 @@ export const cdpJobSizeCompressedKb = new Histogram({labelNames: ['queue_kind'],})+const cdpCyclotronBatchUtilization = new Gauge({+ name: 'cdp_cyclotron_batch_utilization',+ help: 'Indicates how big batches are we are processing compared to the max batch size. Useful as a scaling metric',+ labelNames: ['queue', 'source'],+})++const cdpCyclotronJobsProcessed = new Counter({+ name: 'cdp_cyclotron_jobs_processed',+ help: 'The number of jobs we are managing to process',+ labelNames: ['queue', 'source'],+})++/**+ * Records throughput and batch utilization for a consumed batch.+ * `cdp_cyclotron_batch_utilization` is consumed by KEDA to autoscale the cyclotron workers, so this+ * must be called on every batch — including empty ones — so idle workers report zero and can scale down.+ */+export function observeConsumedBatch(params: {+ queue: CyclotronJobQueueKind+ source: CyclotronJobQueueSource+ batchSize: number+ maxBatchSize: number+}): void {+ const { queue, source, batchSize, maxBatchSize } = params+ cdpCyclotronBatchUtilization.labels({ queue, source }).set(maxBatchSize > 0 ? batchSize / maxBatchSize : 0)+ cdpCyclotronJobsProcessed.inc({ queue, source }, batchSize)+}+/*** Strip transient data from invocation state before persisting.* Groups are always stripped. Person is stripped when stripPerson is true.diff --git a/nodejs/src/cdp/workflows-e2e.test.ts b/nodejs/src/cdp/workflows-e2e.test.tsindex 01ca32fb50a0..51585e9c2056 100644--- a/nodejs/src/cdp/workflows-e2e.test.ts+++ b/nodejs/src/cdp/workflows-e2e.test.ts@@ -110,7 +110,7 @@ describe.each(['postgres-v2' as const, 'postgres' as const])('Workflows E2E (%s)const deps = createCdpConsumerDeps(hub, kafkaProducer)- const kafkaQueue = new CyclotronJobQueueKafka(hub.KAFKA_CLIENT_RACK, hub)+ const kafkaQueue = new CyclotronJobQueueKafka(hub.KAFKA_CLIENT_RACK, hub, hub.CONSUMER_BATCH_SIZE)// Build the hogflow queue for the current modelet hogflowQueue: JobQueuediff --git a/nodejs/src/server.ts b/nodejs/src/server.tsindex 1ac62a40e1d8..f00bf795e980 100644--- a/nodejs/src/server.ts+++ b/nodejs/src/server.ts@@ -141,7 +141,11 @@ export class PluginServer implements NodeServer {}// Create shared job queue backends — each consumer gets the one(s) it needs- const kafkaQueue = new CyclotronJobQueueKafka(this.config.KAFKA_CLIENT_RACK, this.config)+ const kafkaQueue = new CyclotronJobQueueKafka(+ this.config.KAFKA_CLIENT_RACK,+ this.config,+ this.config.CONSUMER_BATCH_SIZE+ )const postgresV2Queue = new CyclotronJobQueuePostgresV2(this.config.CONSUMER_BATCH_SIZE, this.config)if (capabilities.cdpProcessedEvents) {