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(session-replay): wait for image scrubber sidecar (#89008)

Robbie427b152791b8
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.test.ts
index f645539b9229..7d1dc6756035 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.test.ts
@@ -97,6 +97,33 @@ describe('ScrubClient', () => {
expect(requests).toBe(1)
})
+ it('retries until the sidecar listener accepts connections', async () => {
+ const address = server.address() as AddressInfo
+ const baseUrl = `http://127.0.0.1:${address.port}`
+ await new Promise<void>((resolve, reject) =>
+ server.close((error) => {
+ if (error) {
+ reject(error)
+ } else {
+ resolve()
+ }
+ })
+ )
+ let retries = 0
+ const failedProbesBeforeListen = 3
+ const scrubClient = new ScrubClient(baseUrl, 1000)
+
+ await scrubClient.waitUntilReachable(async () => {
+ retries += 1
+ if (retries === failedProbesBeforeListen) {
+ await new Promise<void>((resolve) => server.listen(address.port, '127.0.0.1', resolve))
+ }
+ })
+
+ expect(retries).toBe(failedProbesBeforeListen)
+ expect(requests).toBe(1)
+ })
+
it('never dead-letters on saturation alone, however long the sidecar sheds', async () => {
// The safety property of the whole dead-letter path. Under a backlog every image waits a
// long time, so anything keyed on waiting or failure count alone would park the entire
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.ts
index 6159f9241c44..63a2baf27ffb 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/scrub-client.ts
@@ -6,6 +6,7 @@ import { ImageScrubConsumerMetrics } from './metrics'
/** The timeout's own message, matched on rather than duplicated, so the reason label cannot drift. */
const REQUEST_TIMED_OUT = 'scrub request timed out'
+const REACHABILITY_RETRY_MS = 1_000
/**
* Why a single attempt did not come back with bytes.
@@ -182,6 +183,19 @@ export class ScrubClient {
this.url = new URL('/scrub', baseUrl)
}
+ public async waitUntilReachable(
+ sleepBeforeRetry: (ms: number) => Promise<void> = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
+ ): Promise<void> {
+ for (;;) {
+ try {
+ await this.probe()
+ return
+ } catch {
+ await sleepBeforeRetry(REACHABILITY_RETRY_MS)
+ }
+ }
+ }
+
/**
* Scrubbed bytes, or null when the sidecar permanently rejected the content (422/413).
*
@@ -345,4 +359,16 @@ export class ScrubClient {
req.end(bytes)
})
}
+
+ private probe(): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const req = request(this.url, { method: 'GET' }, (res) => {
+ res.resume()
+ resolve()
+ })
+ req.setTimeout(this.timeoutMs, () => req.destroy(new Error(REQUEST_TIMED_OUT)))
+ req.on('error', reject)
+ req.end()
+ })
+ }
}
diff --git a/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts b/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts
index 3239dfef2db8..fbdc43f4e9b3 100644
--- a/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts
+++ b/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts
@@ -122,6 +122,7 @@ export class IngestionSessionReplayMlImageScrubServer implements NodeServer {
Date.now(),
deadLetters
)
+ await scrubClient.waitUntilReachable()
await consumer.connect((messages) => {
const heartbeat = setInterval(() => consumer.heartbeat(), BATCH_HEARTBEAT_INTERVAL_MS)
return batcher.handleBatch(messages, Date.now()).finally(() => clearInterval(heartbeat))