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(ingestion): retry transient person fetch and group type loads (#77156)

Paweł Ledwoń50b173e40c93
diff --git a/nodejs/src/common/groups/group-type-manager.ts b/nodejs/src/common/groups/group-type-manager.ts
index dacc0423bc13..551f47025718 100644
--- a/nodejs/src/common/groups/group-type-manager.ts
+++ b/nodejs/src/common/groups/group-type-manager.ts
@@ -2,7 +2,7 @@ import { DateTime } from 'luxon'
import { GroupRepository } from '~/common/groups/repositories/group-repository.interface'
import { timeoutGuard } from '~/common/utils/db/utils'
-import { LazyLoader } from '~/common/utils/lazy-loader'
+import { LazyLoader, LoaderRetryOptions } from '~/common/utils/lazy-loader'
import { captureTeamEvent } from '~/common/utils/posthog'
import { TeamManager } from '~/common/utils/team-manager'
import { GroupTypeIndex, GroupTypeToColumnIndex, GroupTypesByProjectId, ProjectId, Team, TeamId } from '~/types'
@@ -10,17 +10,24 @@ import { GroupTypeIndex, GroupTypeToColumnIndex, GroupTypesByProjectId, ProjectI
/** How many unique group types to allow per team */
export const MAX_GROUP_TYPES_PER_TEAM = 5
+export interface GroupTypeManagerOptions {
+ /** Retry transient group-type-load failures (e.g. a Postgres pooler blip) instead of letting them propagate. */
+ loaderRetry?: LoaderRetryOptions
+}
+
export class GroupTypeManager {
private loader: LazyLoader<GroupTypeToColumnIndex>
constructor(
private groupRepository: GroupRepository,
- private teamManager: TeamManager
+ private teamManager: TeamManager,
+ options?: GroupTypeManagerOptions
) {
this.loader = new LazyLoader({
name: 'GroupTypeManager',
refreshAgeMs: 30_000, // 30 seconds
refreshJitterMs: 0,
+ loaderRetry: options?.loaderRetry,
loader: async (projectIds: string[]) => {
const response: Record<string, GroupTypeToColumnIndex> = {}
const timeout = timeoutGuard(`Still running "fetchGroupTypes". Timeout warning after 30 sec!`)
diff --git a/nodejs/src/common/personhog/grpc-retry.test.ts b/nodejs/src/common/personhog/grpc-retry.test.ts
index a5b60a7ce033..6ee8acc3ceaf 100644
--- a/nodejs/src/common/personhog/grpc-retry.test.ts
+++ b/nodejs/src/common/personhog/grpc-retry.test.ts
@@ -64,35 +64,36 @@ describe('withRetry', () => {
['Canceled', Code.Canceled],
])('does not retry on %s', async (_name, code) => {
let callCount = 0
- await expect(
- withRetry(
- () => {
- callCount++
- throw new ConnectError('non-retryable', code)
- },
- 'test-client',
- 'test-method'
- )
- ).rejects.toThrow(ConnectError)
+ const error: unknown = await withRetry(
+ () => {
+ callCount++
+ throw new ConnectError('non-retryable', code)
+ },
+ 'test-client',
+ 'test-method'
+ ).catch((e: unknown) => e)
+ expect(error).toBeInstanceOf(ConnectError)
+ // Not tagged retriable: outer retry layers must not absorb these.
+ expect(error).not.toHaveProperty('isRetriable')
expect(callCount).toBe(1)
})
it('does not retry non-ConnectError errors', async () => {
let callCount = 0
- await expect(
- withRetry(
- () => {
- callCount++
- throw new Error('plain error')
- },
- 'test-client',
- 'test-method'
- )
- ).rejects.toThrow('plain error')
+ const error: unknown = await withRetry(
+ () => {
+ callCount++
+ throw new Error('plain error')
+ },
+ 'test-client',
+ 'test-method'
+ ).catch((e: unknown) => e)
+ expect(error).toBeInstanceOf(Error)
+ expect(error).not.toHaveProperty('isRetriable')
expect(callCount).toBe(1)
})
- it('throws after max retries exhausted', async () => {
+ it('throws after max retries exhausted, tagged retriable for outer retry layers', async () => {
let callCount = 0
await expect(
withRetry(
@@ -103,7 +104,7 @@ describe('withRetry', () => {
'test-client',
'test-method'
)
- ).rejects.toThrow(ConnectError)
+ ).rejects.toMatchObject({ code: Code.Unavailable, isRetriable: true })
// 1 initial + 2 retries = 3 total (default maxRetries=2)
expect(callCount).toBe(3)
})
diff --git a/nodejs/src/common/personhog/grpc-retry.ts b/nodejs/src/common/personhog/grpc-retry.ts
index 8de8bc4fe57d..16c43b588f9d 100644
--- a/nodejs/src/common/personhog/grpc-retry.ts
+++ b/nodejs/src/common/personhog/grpc-retry.ts
@@ -13,10 +13,20 @@ const RETRYABLE_CODES = new Set([
Code.Unknown,
])
-function isRetryable(error: unknown): boolean {
+function isRetryable(error: unknown): error is ConnectError {
return error instanceof ConnectError && RETRYABLE_CODES.has(error.code)
}
+/**
+ * Tag a terminal transient error as retriable so outer retry layers (pipeline
+ * chunk retry, LazyLoader loaderRetry) can keep retrying after the client-level
+ * budget here is exhausted. Non-transient errors are left untagged so
+ * unexpected failures surface loudly instead of being absorbed.
+ */
+function tagRetriable(error: ConnectError): ConnectError & { isRetriable: true } {
+ return Object.assign(error, { isRetriable: true as const })
+}
+
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
@@ -48,7 +58,7 @@ export async function withRetry<T>(
logger.error(`[${client}/${method}] gRPC call failed`, {
error: String(error),
})
- throw error
+ throw isRetryable(error) ? tagRetriable(error) : error
}
personhogRetriesTotal.inc({ method, client, error_type: grpcErrorType(error) })
logger.warn(`[${client}/${method}] Retryable gRPC error, retrying`, {
diff --git a/nodejs/src/ingestion/pipelines/ai/pipeline.ts b/nodejs/src/ingestion/pipelines/ai/pipeline.ts
index adf8ca5f6f21..464f82c497d0 100644
--- a/nodejs/src/ingestion/pipelines/ai/pipeline.ts
+++ b/nodejs/src/ingestion/pipelines/ai/pipeline.ts
@@ -176,8 +176,13 @@ export function createAiIngestionPipeline<
.pipeChunk(createApplyCookielessProcessingStep(cookielessManager))
.pipeChunk(createOnlyCookielessRateLimitToOverflowStep(preservePartitionLocality, overflowRedirectService))
.pipeChunk(createOverflowLaneTTLRefreshStep(overflowLaneTTLRefreshService))
- // Read-only batch person fetch (no person writes).
- .pipeChunk(createFetchPersonChunkStep(personRepository))
+ // Read-only batch person fetch (no person writes). The personhog
+ // client retries transient gRPC errors for ~150ms; this outer
+ // retry absorbs longer blips that would otherwise crash the
+ // worker via an unhandled rejection.
+ .pipeChunk(createFetchPersonChunkStep(personRepository), {
+ retry: { tries: 5, sleepMs: 100, name: 'fetch_person_chunk' },
+ })
// Prefetch hog functions for the batch's teams so the transformer
// honors Hog watcher's disabled-function state (mirrors analytics).
.pipeChunk(createPrefetchHogFunctionsStep(hogTransformer, cdpHogWatcherSampleRate))
diff --git a/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts b/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts
index c37611d21da6..0183330b9e37 100644
--- a/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts
+++ b/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts
@@ -188,8 +188,13 @@ export function createErrorTrackingPipeline(config: ErrorTrackingPipelineConfig)
return (
afterCymbal
- // Batch fetch person (read-only, no updates)
- .pipeChunk(createFetchPersonChunkStep(personRepository))
+ // Batch fetch person (read-only, no updates). The personhog client
+ // retries transient gRPC errors for ~150ms; this outer retry
+ // absorbs longer blips that would otherwise crash the worker via
+ // an unhandled rejection.
+ .pipeChunk(createFetchPersonChunkStep(personRepository), {
+ retry: { tries: 5, sleepMs: 100, name: 'fetch_person_chunk' },
+ })
// Run Hog transformations (including GeoIP if team has it enabled)
.pipe(createHogTransformEventStep(hogTransformer))
// Prepare event for emission
diff --git a/nodejs/src/servers/ingestion-general-server.ts b/nodejs/src/servers/ingestion-general-server.ts
index 138de179b20f..5c5cc165315f 100644
--- a/nodejs/src/servers/ingestion-general-server.ts
+++ b/nodejs/src/servers/ingestion-general-server.ts
@@ -237,7 +237,12 @@ export class IngestionGeneralServer implements NodeServer {
const integrationManager = new IntegrationManagerService(this.pubsub, this.postgres, encryptedFields)
// 3. Ingestion-specific services
- const groupTypeManager = new GroupTypeManager(groupRepository, teamManager)
+ // Same rationale as the team manager's loaderRetry above: group-type loads run
+ // detached in the LazyLoader buffer, so an un-retried transient failure can
+ // surface as an unhandled rejection and restart the worker.
+ const groupTypeManager = new GroupTypeManager(groupRepository, teamManager, {
+ loaderRetry: { retryIntervalMs: 250, retryJitterMs: 250, maxElapsedMs: 5000 },
+ })
const serviceLoaders: (() => Promise<PluginServerService>)[] = []