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.

feat(replay): add metrics to the ml block-metadata parquet sink (#71119)

Robbieac1b42b07e21
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-batcher.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-batcher.ts
index f00b8433af2d..6e2e64a2f754 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-batcher.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-batcher.ts
@@ -6,6 +6,7 @@ import { findOffsetsToCommit } from '~/common/kafka/consumer/consumer-v1'
import { parseBlockMetadataMessages } from './block-metadata-message'
import { BlockMetadataParquetStore } from './block-metadata-parquet-store'
import { MlBlockMetadataRow } from './block-metadata-row'
+import { MlParquetSinkMetrics } from './metrics'
/** The subset of the Kafka consumer the batcher needs: storing offsets it has durably written. */
export interface OffsetStore {
@@ -61,7 +62,8 @@ export class BlockMetadataBatcher {
*/
public async flush(nowMs: number): Promise<void> {
this.lastFlushMs = nowMs
- if (this.buffer.length > 0) {
+ const wroteObject = this.buffer.length > 0
+ if (wroteObject) {
await this.store.write(this.buffer)
this.buffer = []
}
@@ -69,6 +71,9 @@ export class BlockMetadataBatcher {
// Commit after the write lands so a failed write replays; skipped-only batches still advance here.
this.offsetStore.offsetsStore([...this.pendingOffsets.values()])
this.pendingOffsets.clear()
+ // 'empty' means we advanced past messages that produced no rows (all skipped/malformed): healthy
+ // offset progress with zero Parquet output, the one state Kafka lag can't distinguish.
+ MlParquetSinkMetrics.incFlush(wroteObject ? 'written' : 'empty')
}
}
}
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-message.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-message.ts
index 575ae641d3c7..9c9da1ea6fc6 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-message.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-message.ts
@@ -4,6 +4,7 @@ import { logger } from '~/common/utils/logger'
import { isWellFormedRow } from './block-metadata-columns'
import { MlBlockMetadataRow } from './block-metadata-row'
+import { MlParquetSinkMetrics } from './metrics'
export function parseBlockMetadataMessages(messages: readonly { value: Buffer | null }[]): MlBlockMetadataRow[] {
const rows: MlBlockMetadataRow[] = []
@@ -17,13 +18,16 @@ export function parseBlockMetadataMessages(messages: readonly { value: Buffer |
} catch (error) {
// Skip malformed rows rather than wedge the partition; they're rare and non-fatal for training data.
logger.warn('🪶', 'ml_parquet_metadata_parse_failed', { error: String(error) })
+ MlParquetSinkMetrics.incRowsRejected('parse_failed')
continue
}
if (!isWellFormedRow(row)) {
logger.warn('🪶', 'ml_parquet_metadata_row_invalid')
+ MlParquetSinkMetrics.incRowsRejected('invalid')
continue
}
rows.push(row)
}
+ MlParquetSinkMetrics.incRowsParsed(rows.length)
return rows
}
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-parquet-store.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-parquet-store.ts
index 8a8f16f25fbc..c85ec21efe18 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-parquet-store.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/block-metadata-parquet-store.ts
@@ -5,6 +5,7 @@ import { randomUUID } from 'crypto'
import { logger } from '~/common/utils/logger'
import { MlBlockMetadataRow } from './block-metadata-row'
+import { MlParquetSinkMetrics } from './metrics'
import { rowsToParquetBuffer } from './parquet-writer'
const cmp = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0)
@@ -30,16 +31,26 @@ export class BlockMetadataParquetStore {
}
// Sorting clusters a recording's blocks together for better compression and reads.
rows.sort((a, b) => cmp(a.team_id, b.team_id) || cmp(a.session_id, b.session_id))
- const body = await rowsToParquetBuffer(rows)
- const key = this.objectKey(minEventDate(rows))
- await this.s3Client.send(
- new PutObjectCommand({
- Bucket: this.bucket,
- Key: key,
- Body: body,
- ContentType: 'application/vnd.apache.parquet',
- })
- )
+ let body: Buffer
+ let key: string
+ try {
+ // Encoding, key derivation, and upload all count as write failures: each leaves the batch to
+ // replay from Kafka, so the counter must see them, not just the S3 send.
+ body = await rowsToParquetBuffer(rows)
+ key = this.objectKey(minEventDate(rows))
+ await this.s3Client.send(
+ new PutObjectCommand({
+ Bucket: this.bucket,
+ Key: key,
+ Body: body,
+ ContentType: 'application/vnd.apache.parquet',
+ })
+ )
+ } catch (error) {
+ MlParquetSinkMetrics.incWriteError()
+ throw error
+ }
+ MlParquetSinkMetrics.observeWrite(rows.length, body.length)
logger.info('🪶', 'ml_parquet_metadata_written', { rows: rows.length, bytes: body.length, key })
}
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts
new file mode 100644
index 000000000000..91eb451e641a
--- /dev/null
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts
@@ -0,0 +1,57 @@
+import { Counter } from 'prom-client'
+
+/**
+ * Metrics for the ML block-metadata Parquet sink (drains the mirror's block-metadata topic to the ML
+ * bucket). The sink can consume and commit offsets while writing nothing: if every row is rejected the
+ * buffer stays empty, so flush advances offsets without a write. Kafka lag alone can't see that state.
+ */
+export class MlParquetSinkMetrics {
+ private static readonly rowsParsed = new Counter({
+ name: 'ml_mirror_parquet_sink_rows_parsed_total',
+ help: 'Block-metadata rows parsed from Kafka and accepted into the Parquet buffer',
+ })
+ private static readonly rowsRejected = new Counter({
+ name: 'ml_mirror_parquet_sink_rows_rejected_total',
+ help: 'Block-metadata Kafka messages skipped before buffering, by reason',
+ labelNames: ['reason'],
+ })
+ private static readonly objectsWritten = new Counter({
+ name: 'ml_mirror_parquet_sink_objects_written_total',
+ help: 'Parquet objects written to the ML bucket',
+ })
+ private static readonly rowsWritten = new Counter({
+ name: 'ml_mirror_parquet_sink_rows_written_total',
+ help: 'Block-metadata rows written to the ML bucket as Parquet',
+ })
+ private static readonly bytesWritten = new Counter({
+ name: 'ml_mirror_parquet_sink_bytes_written_total',
+ help: 'Parquet bytes written to the ML bucket',
+ })
+ private static readonly writeErrors = new Counter({
+ name: 'ml_mirror_parquet_sink_write_errors_total',
+ help: 'Parquet object writes that threw (the batch replays from Kafka)',
+ })
+ private static readonly flushes = new Counter({
+ name: 'ml_mirror_parquet_sink_flushes_total',
+ help: 'Batcher flushes by outcome: wrote a Parquet object, or committed offsets with an empty buffer',
+ labelNames: ['outcome'],
+ })
+
+ public static incRowsParsed(count: number): void {
+ this.rowsParsed.inc(count)
+ }
+ public static incRowsRejected(reason: 'parse_failed' | 'invalid'): void {
+ this.rowsRejected.labels(reason).inc()
+ }
+ public static observeWrite(rows: number, bytes: number): void {
+ this.objectsWritten.inc()
+ this.rowsWritten.inc(rows)
+ this.bytesWritten.inc(bytes)
+ }
+ public static incWriteError(): void {
+ this.writeErrors.inc()
+ }
+ public static incFlush(outcome: 'written' | 'empty'): void {
+ this.flushes.labels(outcome).inc()
+ }
+}