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): report image-scrub stage timings and source format mix (#73627)

Robbie090e70dd7482
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/metrics.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/metrics.ts
index a21ef338bed2..51ee0a3401c6 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/metrics.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/metrics.ts
@@ -46,6 +46,31 @@ const outputBytes = new Histogram({
buckets: [64, 256, 1024, 4096, 16384, 65536],
registers: [register],
})
+// The total duration above says the scrub is slow; these say which stage to attack. Every one of
+// these numbers was already being measured per image and thrown away.
+const stageDuration = new Histogram({
+ name: 'ml_mirror_image_scrub_stage_duration_seconds',
+ help: 'Scrub wall time by stage. Sums to roughly the total, so the stage with the largest rate() share is where the CPU goes',
+ labelNames: ['stage'],
+ buckets: [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2],
+ registers: [register],
+})
+// Which decoder we actually pay for. Only formats with a multi-resolution decode (JPEG, WebP) can
+// be shrunk on load; PNG has to be fully inflated whatever the target size, so the value of any
+// decode-side optimization is bounded by this distribution.
+const sourceFormat = new Counter({
+ name: 'ml_mirror_image_scrub_source_format_total',
+ help: 'Decoded images by source container format',
+ labelNames: ['format'],
+ registers: [register],
+})
+const sourceMegapixels = new Histogram({
+ name: 'ml_mirror_image_scrub_source_megapixels',
+ help: 'Source megapixels before the SCRUB_MAX_PIXELS downscale, by format. Mass above the 2.56 budget bucket is the only traffic a shrink-on-load path could speed up',
+ labelNames: ['format'],
+ buckets: [0.1, 0.25, 0.5, 1, 2, 2.56, 4, 8, 16, 50],
+ registers: [register],
+})
// The scrub is a privacy control, so its OUTCOME signals matter as much as its error signals: a
// runaway NSFW gate irreversibly blanking everything, or a detector flatlining at zero (persisting
// un-redacted screenshots), must be distinguishable from healthy operation.
@@ -86,5 +111,20 @@ export const ScrubMetrics = {
facesRedacted.inc(t.faces)
textBoxesRedacted.inc(t.textBoxes)
codesRedacted.inc(t.codes)
+
+ sourceFormat.labels(t.format).inc()
+ sourceMegapixels.labels(t.format).observe(t.inputPixels / 1e6)
+ stageDuration.labels('decode').observe(t.decodeMs / 1000)
+ stageDuration.labels('nsfw').observe(t.nsfwMs / 1000)
+ // A blanked image returns before detection and compose, so recording their zeros would
+ // drag those stages' quantiles toward zero rather than describing the work they do.
+ if (t.blanked) {
+ return
+ }
+ stageDuration.labels('face').observe(t.faceMs / 1000)
+ stageDuration.labels('text').observe(t.textMs / 1000)
+ stageDuration.labels('codes').observe(t.codesMs / 1000)
+ stageDuration.labels('compose').observe(t.composeMs / 1000)
+ stageDuration.labels('encode').observe(t.encodeMs / 1000)
},
}
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/qr.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/qr.test.ts
index 4befc5794a83..802f7c35dded 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/qr.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/qr.test.ts
@@ -35,7 +35,13 @@ describe('detectCodes', () => {
.raw()
.toBuffer({ resolveWithObject: true })
- const boxes = await detectCodes({ data, W: info.width, H: info.height })
+ const boxes = await detectCodes({
+ data,
+ W: info.width,
+ H: info.height,
+ format: 'raw',
+ inputPixels: info.width * info.height,
+ })
expect(boxes).toHaveLength(1)
const b = boxes[0]
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts
index a29dc6d44eea..a194463520a4 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts
@@ -65,6 +65,10 @@ export interface StageTimings {
faces: number
textBoxes: number
codes: number
+ /** Source format and its pre-downscale size: which stage to attack depends on the corpus mix,
+ * and only formats with a multi-resolution decode can be shrunk on load. */
+ format: string
+ inputPixels: number
}
const NSFW_THRESHOLD = numFromEnv('NSFW_THRESHOLD', 0.6, 0.05, 0.95) // NSFL+NSFW combined; deliberately loose, this is a safety net
@@ -124,6 +128,8 @@ export async function advancedScrub(
faces: 0,
textBoxes: 0,
codes: 0,
+ format: 'unknown',
+ inputPixels: 0,
}
const t0 = performance.now()
const tDec = performance.now()
@@ -137,6 +143,8 @@ export async function advancedScrub(
}
const { W, H } = src
timings.decodeMs = performance.now() - tDec
+ timings.format = src.format
+ timings.inputPixels = src.inputPixels
// 1. NSFW / gore gate FIRST: if it trips we skip all detection. Running it first (rather than
// overlapping detection) keeps each worker ~1 core, which packs better under multi-process
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/src-image.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/src-image.ts
index 4c09da17ab33..54205a54895a 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/src-image.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/src-image.ts
@@ -18,6 +18,9 @@ export interface Src {
data: Buffer
W: number
H: number
+ /** From the header sharp already read, so reporting the corpus mix costs nothing. */
+ format: string
+ inputPixels: number
}
export async function decodeSrc(input: Buffer): Promise<Src> {
@@ -36,7 +39,13 @@ export async function decodeSrc(input: Buffer): Promise<Src> {
.flatten({ background: '#fff' })
.raw()
.toBuffer({ resolveWithObject: true })
- return { data, W: info.width, H: info.height }
+ return {
+ data,
+ W: info.width,
+ H: info.height,
+ format: meta.format ?? 'unknown',
+ inputPixels: meta.width * meta.height,
+ }
}
/** A fresh sharp pipeline over the already-decoded raw pixels (no PNG decode). */