🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(worker): stabilize data retention lag metrics (#15267)
Valery Meleshkina86a64e0a8c4
diff --git a/worker/src/__tests__/batchDataRetentionCleaner.test.ts b/worker/src/__tests__/batchDataRetentionCleaner.test.tsindex 35e377de7d..4a712a41c1 100644--- a/worker/src/__tests__/batchDataRetentionCleaner.test.ts+++ b/worker/src/__tests__/batchDataRetentionCleaner.test.ts@@ -22,9 +22,15 @@ import { env } from "../env";const integrationHooks = vi.hoisted(() => ({failExactEnrichmentForProjectId: null as string | null,+ omitExactEnrichmentForProjectId: null as string | null,failCandidateStreamAfterFirstRow: false,activeCandidateStreams: 0,candidateHttpTimeouts: [] as Array<{ send: number; receive: number }>,+ exactEnrichmentRequests: [] as Array<{+ projectIds: string[];+ preferredClickhouseService: string | undefined;+ requestTimeout: number | undefined;+ }>,gaugeCalls: [] as Array<[stat: string, value: number | undefined]>,incrementCalls: [] as Array<[stat: string, value: number | undefined]>,}));@@ -39,8 +45,18 @@ vi.mock("@langfuse/shared/src/server", async (importOriginal) => {opts: Parameters<typeof actual.queryClickhouse>[0],) => {const candidateProjectIds = opts.params?.candidateProjectIds;+ if (!opts.query.includes("AS oldest_timestamp")) {+ return actual.queryClickhouse<T>(opts);+ }++ integrationHooks.exactEnrichmentRequests.push({+ projectIds: Array.isArray(candidateProjectIds)+ ? candidateProjectIds.map(String)+ : [],+ preferredClickhouseService: opts.preferredClickhouseService,+ requestTimeout: opts.clickhouseConfigs?.request_timeout,+ });if (- opts.query.includes("AS oldest_timestamp") &&integrationHooks.failExactEnrichmentForProjectId !== null &&Array.isArray(candidateProjectIds) &&candidateProjectIds.includes(@@ -49,7 +65,14 @@ vi.mock("@langfuse/shared/src/server", async (importOriginal) => {) {throw new Error("forced exact enrichment failure");}- return actual.queryClickhouse<T>(opts);+ const result = await actual.queryClickhouse<T>(opts);+ return integrationHooks.omitExactEnrichmentForProjectId === null+ ? result+ : result.filter(+ (row) =>+ (row as { project_id?: string }).project_id !==+ integrationHooks.omitExactEnrichmentForProjectId,+ );},queryClickhouseStream: async function* <T>(opts: Parameters<typeof actual.queryClickhouseStream>[0],@@ -461,7 +484,7 @@ describe("BatchDataRetentionCleaner", () => {});});- describe("candidate discovery and fallback queries", () => {+ describe("candidate discovery and enrichment", () => {const timestampColumns = TIMESTAMP_COLUMN_MAP as Record<string, string>;let tableName: string;let table: BatchDataRetentionTable;@@ -476,9 +499,11 @@ describe("BatchDataRetentionCleaner", () => {env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_PROJECT_LIMIT;timestampColumns[tableName] = "start_time";integrationHooks.failExactEnrichmentForProjectId = null;+ integrationHooks.omitExactEnrichmentForProjectId = null;integrationHooks.failCandidateStreamAfterFirstRow = false;integrationHooks.activeCandidateStreams = 0;integrationHooks.candidateHttpTimeouts = [];+ integrationHooks.exactEnrichmentRequests = [];integrationHooks.gaugeCalls = [];integrationHooks.incrementCalls = [];await createRetentionTestTable(tableName);@@ -604,42 +629,94 @@ describe("BatchDataRetentionCleaner", () => {"langfuse.batch_data_retention_cleaner.candidate_query_failures",1,]);+ expect(+ getLastGaugeValue(+ "langfuse.batch_data_retention_cleaner.seconds_past_cutoff",+ ),+ ).toBe(0);});- it("uses partition lag when exact enrichment fails", async () => {- const [fallbackProjectId] = await createProjectsWithRetention(1);- await insertRetentionTestRows(tableName, [- {- projectId: fallbackProjectId!,- startTime: new Date("2020-03-15T00:00:00.000Z").getTime(),- },- {- projectId: fallbackProjectId!,+ it("continues other chunks after one enrichment exhausts its retry", async () => {+ const [failedProjectId, successfulProjectId] =+ await createProjectsWithRetention(2);+ await insertRetentionTestRows(+ tableName,+ [failedProjectId!, successfulProjectId!].map((projectId) => ({+ projectId,startTime: new Date("2020-01-15T00:00:00.000Z").getTime(),- },- ]);- integrationHooks.failExactEnrichmentForProjectId = fallbackProjectId!;- const beforeRun = Date.now();+ })),+ );+ integrationHooks.failExactEnrichmentForProjectId = failedProjectId!;+ env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_CHUNK_SIZE = 1;await new BatchDataRetentionCleaner(table).processBatch();- const afterRun = Date.now();+ expect(await getRetentionTestRowCount(tableName)).toBe(0);+ const failedProjectRequests =+ integrationHooks.exactEnrichmentRequests.filter((request) =>+ request.projectIds.includes(failedProjectId!),+ );+ expect(+ failedProjectRequests.map(+ (request) => request.preferredClickhouseService,+ ),+ ).toEqual([undefined, "ReadOnly"]);+ expect(+ integrationHooks.exactEnrichmentRequests.filter((request) =>+ request.projectIds.includes(successfulProjectId!),+ ),+ ).toHaveLength(1);+ expect(+ integrationHooks.exactEnrichmentRequests.every(+ (request) =>+ request.requestTimeout ===+ env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_CANDIDATE_QUERY_TIMEOUT_MS,+ ),+ ).toBe(true);+ expect(+ integrationHooks.incrementCalls.filter(+ ([name]) =>+ name ===+ "langfuse.batch_data_retention_cleaner.enrichment_query_failures",+ ),+ ).toHaveLength(1);expect(- await getRetentionTestProjectCount(tableName, fallbackProjectId!),+ getLastGaugeValue(+ "langfuse.batch_data_retention_cleaner.seconds_past_cutoff",+ ),).toBe(0);+ });- const lagValue = getLastGaugeValue(- "langfuse.batch_data_retention_cleaner.seconds_past_cutoff",+ it("keeps sibling enrichment when a candidate disappears", async () => {+ const [staleProjectId, siblingProjectId] =+ await createProjectsWithRetention(2);+ const dayMs = 24 * 60 * 60 * 1000;+ await insertRetentionTestRows(+ tableName,+ [staleProjectId!, siblingProjectId!].map((projectId) => ({+ projectId,+ startTime: Date.now() - 10 * dayMs,+ })),);- const partitionStart = new Date("2020-01-01T00:00:00.000Z").getTime();- const retentionMs = 7 * 24 * 60 * 60 * 1000;+ integrationHooks.omitExactEnrichmentForProjectId = staleProjectId!;- expect(lagValue).toBeGreaterThanOrEqual(- (beforeRun - retentionMs - partitionStart) / 1000,- );- expect(lagValue).toBeLessThanOrEqual(- (afterRun - retentionMs - partitionStart) / 1000,- );+ await new BatchDataRetentionCleaner(table).processBatch();++ expect(+ await getRetentionTestProjectCount(tableName, siblingProjectId!),+ ).toBe(0);+ expect(+ integrationHooks.incrementCalls.some(+ ([name]) =>+ name ===+ "langfuse.batch_data_retention_cleaner.enrichment_query_failures",+ ),+ ).toBe(false);+ expect(+ getLastGaugeValue(+ "langfuse.batch_data_retention_cleaner.seconds_past_cutoff",+ ),+ ).toBeGreaterThan(2 * (dayMs / 1000));});});});diff --git a/worker/src/env.ts b/worker/src/env.tsindex bd94145dad..08c4301665 100644--- a/worker/src/env.ts+++ b/worker/src/env.ts@@ -430,11 +430,16 @@ const EnvSchema = z.object({.number().positive().default(100), // Chunk size for counting projects in ClickHouse+ LANGFUSE_BATCH_DATA_RETENTION_CLEANER_QUERY_CONCURRENCY: z.coerce+ .number()+ .int()+ .positive()+ .default(2), // Max concurrent query pipelines per table cleanerLANGFUSE_BATCH_DATA_RETENTION_CLEANER_CANDIDATE_QUERY_TIMEOUT_MS: z.coerce.number().int().positive()- .default(180_000), // 3 minutes for candidate discovery queries+ .default(180_000), // 3 minutes for candidate and enrichment queriesLANGFUSE_BATCH_DATA_RETENTION_CLEANER_DELETE_TIMEOUT_MS: z.coerce.number().positive()diff --git a/worker/src/features/batch-data-retention-cleaner/index.ts b/worker/src/features/batch-data-retention-cleaner/index.tsindex 7b4dcfeca0..c9767d2cc3 100644--- a/worker/src/features/batch-data-retention-cleaner/index.ts+++ b/worker/src/features/batch-data-retention-cleaner/index.ts@@ -1,4 +1,5 @@import { createHash } from "crypto";+import pLimit from "p-limit";import { prisma } from "@langfuse/shared/src/db";import {@@ -61,52 +62,17 @@ interface ProjectWorkload extends ProjectRetention {interface ProjectWorkloadSelection {observedWorkloads: ProjectWorkload[];selectedWorkloads: ProjectWorkload[];+ lagMeasurementComplete: boolean;}-function parseMonthlyPartitionStart(partitionId: string): Date | null {- if (!/^\d{6}$/.test(partitionId)) {- return null;- }-- const year = Number(partitionId.slice(0, 4));- const month = Number(partitionId.slice(4, 6));- if (year < 100 || month < 1 || month > 12) {- return null;- }-- return new Date(Date.UTC(year, month - 1, 1));+interface CandidateDiscoveryResult {+ candidates: ProjectRetention[];+ complete: boolean;}-async function getOldestProjectPartitionStarts(- tableName: BatchDataRetentionTable,- projectIds: string[],-): Promise<Map<string, Date | null>> {- if (projectIds.length === 0) {- return new Map();- }-- const result = await queryClickhouse<{- project_id: string;- oldest_expired_partition: string;- }>({- query: `- SELECT- project_id,- min(_partition_id) AS oldest_expired_partition- FROM ${tableName}- PREWHERE project_id IN ({candidateProjectIds: Array(String)})- GROUP BY project_id- `,- params: { candidateProjectIds: projectIds },- useMultipartParamsAuto: true,- });-- return new Map(- result.map((row) => [- row.project_id,- parseMonthlyPartitionStart(row.oldest_expired_partition),- ]),- );+interface EnrichmentResult {+ workloads: ProjectWorkload[];+ complete: boolean;}/**@@ -217,6 +183,8 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {logger.info(`Starting ${this.instanceName}`, {intervalMs: env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_INTERVAL_MS,projectLimit: env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_PROJECT_LIMIT,+ queryConcurrency:+ env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_QUERY_CONCURRENCY,candidateQueryTimeoutMs:env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_CANDIDATE_QUERY_TIMEOUT_MS,deleteTimeoutMs:@@ -281,7 +249,7 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {this.lastLockExtensionAt = 0;// Step 1: Get project workloads (streamed CH candidates + PG config)- const { observedWorkloads, selectedWorkloads } =+ const { observedWorkloads, selectedWorkloads, lagMeasurementComplete } =await this.getProjectWorkloads();recordGauge(@@ -295,7 +263,7 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {// Retention cutoffs come from Postgres and are frozen once per run.// Fold the per-project results here to preserve one maximum across all// bounded ClickHouse queries.- const maxSecondsPastCutoff = observedWorkloads.reduce(+ const observedMaxSecondsPastCutoff = observedWorkloads.reduce((maximum, workload) =>workload.secondsPastCutoff === null? maximum@@ -303,13 +271,15 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {0,);- recordGauge(- `${METRIC_PREFIX}.seconds_past_cutoff`,- maxSecondsPastCutoff,- {- table: this.tableName,- },- );+ if (lagMeasurementComplete) {+ recordGauge(+ `${METRIC_PREFIX}.seconds_past_cutoff`,+ observedMaxSecondsPastCutoff,+ {+ table: this.tableName,+ },+ );+ }// Step 2: Execute DELETEif (selectedWorkloads.length > 0) {@@ -320,7 +290,8 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {(workload) => workload.projectId,),pendingProjectsSeen: observedWorkloads.length,- maxSecondsPastCutoff,+ observedMaxSecondsPastCutoff,+ lagMeasurementComplete,},);@@ -377,7 +348,11 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {});if (projectsWithRetention.length === 0) {- return { observedWorkloads: [], selectedWorkloads: [] };+ return {+ observedWorkloads: [],+ selectedWorkloads: [],+ lagMeasurementComplete: true,+ };}// Step 2: Calculate cutoffs for all projects upfront@@ -409,16 +384,58 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {// count breaks ties, but deletion must not depend on this grouped query// succeeding. Keep enrichment per input chunk so its parameters remain// bounded too.- const chunkWorkloads = await Promise.all(- projectChunks.map(async (projectChunk) => {- const candidates = await this.findExpiredProjectCandidates(- timestampColumn,- projectChunk,- );- return this.enrichProjectCandidates(timestampColumn, candidates);- }),+ const limitRetentionQuery = pLimit(+ env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_QUERY_CONCURRENCY,);- const workloads = chunkWorkloads.flat();+ let leaseLost = false;+ // Wait for every limited pipeline so no queued query outlives the lock.+ const settledChunkResults = await Promise.allSettled(+ projectChunks.map((projectChunk) =>+ limitRetentionQuery(async () => {+ if (leaseLost) {+ return null;+ }++ try {+ const discovery = await this.findExpiredProjectCandidates(+ timestampColumn,+ projectChunk,+ );+ if (leaseLost) {+ return null;+ }++ const enrichment = await this.enrichProjectCandidates(+ timestampColumn,+ discovery.candidates,+ );+ return {+ workloads: enrichment.workloads,+ complete: discovery.complete && enrichment.complete,+ };+ } catch (error) {+ if (error instanceof BatchDataRetentionCleanerLeaseLostError) {+ leaseLost = true;+ }+ throw error;+ }+ }),+ ),+ );++ const failedChunk = settledChunkResults.find(+ (result): result is PromiseRejectedResult => result.status === "rejected",+ );+ if (failedChunk) {+ throw failedChunk.reason;+ }++ const chunkResults = settledChunkResults.flatMap((result) =>+ result.status === "fulfilled" && result.value !== null+ ? [result.value]+ : [],+ );+ const workloads = chunkResults.flatMap((result) => result.workloads);// Prioritize the projects furthest behind their cutoff. A failed lag or// count estimate can indicate a particularly large workload, so keep@@ -452,6 +469,7 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {0,env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_PROJECT_LIMIT,),+ lagMeasurementComplete: chunkResults.every((result) => result.complete),};}@@ -462,9 +480,9 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {private async findExpiredProjectCandidates(timestampColumn: string,projects: ProjectRetention[],- ): Promise<ProjectRetention[]> {+ ): Promise<CandidateDiscoveryResult> {if (projects.length === 0) {- return [];+ return { candidates: [], complete: true };}const { conditions, params } = buildRetentionConditions(@@ -480,6 +498,7 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {`;const candidatesById = new Map<string, ProjectRetention>();+ let complete = true;const projectsById = new Map(projects.map((project) => [project.projectId, project]),);@@ -515,6 +534,7 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {if (error instanceof BatchDataRetentionCleanerLeaseLostError) {throw error;}+ complete = false;recordIncrement(`${METRIC_PREFIX}.candidate_query_failures`, 1, {table: this.tableName,});@@ -527,22 +547,26 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {// A completed query is progress even when it did not yield a candidate.await this.extendLockOnProgress();- return Array.from(candidatesById.values());+ return {+ candidates: Array.from(candidatesById.values()),+ complete,+ };}/*** Best-effort total count and oldest retention timestamp for discovered* candidates. The query deliberately filters only on project_id. Retention* cutoffs come from Postgres and are frozen once per run, so they are applied- * after the result is returned. If this query fails, estimate the oldest- * timestamp from the monthly partition instead.+ * after the result is returned. A failed primary query is retried once on a+ * read-only pool. If both attempts fail, candidates remain eligible for+ * deletion without publishing a partial lag maximum.*/private async enrichProjectCandidates(timestampColumn: string,candidates: ProjectRetention[],- ): Promise<ProjectWorkload[]> {+ ): Promise<EnrichmentResult> {if (candidates.length === 0) {- return [];+ return { workloads: [], complete: true };}const query = `@@ -555,7 +579,9 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {GROUP BY project_id`;- try {+ const queryExactWorkloads = async (+ preferredClickhouseService?: "ReadOnly" | "EventsReadOnly",+ ): Promise<ProjectWorkload[]> => {const result = await queryClickhouse<{project_id: string;row_count: number;@@ -568,6 +594,15 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {),},useMultipartParamsAuto: true,+ preferredClickhouseService,+ clickhouseConfigs: {+ request_timeout:+ env.LANGFUSE_BATCH_DATA_RETENTION_CLEANER_CANDIDATE_QUERY_TIMEOUT_MS,+ clickhouse_settings: {+ http_send_timeout: this.candidateQueryHttpTimeoutSeconds,+ http_receive_timeout: this.candidateQueryHttpTimeoutSeconds,+ },+ },});const resultByProjectId = new Map(@@ -591,8 +626,6 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {}),);- await this.extendLockOnProgress();-return candidates.flatMap((candidate) => {const resultForProject = resultByProjectId.get(candidate.projectId);if (@@ -614,74 +647,60 @@ export class BatchDataRetentionCleaner extends PeriodicExclusiveRunner {},];});+ };++ try {+ const workloads = await queryExactWorkloads();+ await this.extendLockOnProgress();+ return { workloads, complete: true };} catch (error) {if (error instanceof BatchDataRetentionCleanerLeaseLostError) {throw error;}- recordIncrement(`${METRIC_PREFIX}.enrichment_query_failures`, 1, {- table: this.tableName,- });- logger.warn(`${this.instanceName}: Candidate enrichment failed`, {- error,- candidatesFound: candidates.length,- });+ logger.warn(+ `${this.instanceName}: Candidate enrichment failed; retrying on read-only`,+ {+ error,+ candidatesFound: candidates.length,+ },+ );- await this.extendLockOnProgress();- return this.enrichCandidatesFromOldestPartition(candidates);- }- }+ // Reset the lease before another potentially long exact query.+ await this.extendLockOnProgress(true);- /**- * Estimate each candidate's lag from its oldest monthly partition. The- * partition start is an upper bound on how far the oldest row is past the- * fixed project cutoff. If this query also fails, retain the candidates- * without metrics.- */- private async enrichCandidatesFromOldestPartition(- candidates: ProjectRetention[],- ): Promise<ProjectWorkload[]> {- try {- const partitionStartByProjectId = await getOldestProjectPartitionStarts(- this.tableName,- candidates.map((candidate) => candidate.projectId),- );+ const preferredClickhouseService =+ this.tableName === "events_full" || this.tableName === "events_core"+ ? "EventsReadOnly"+ : "ReadOnly";- await this.extendLockOnProgress();+ try {+ const workloads = await queryExactWorkloads(preferredClickhouseService);+ await this.extendLockOnProgress();+ return { workloads, complete: true };+ } catch (retryError) {+ if (retryError instanceof BatchDataRetentionCleanerLeaseLostError) {+ throw retryError;+ }- return candidates.map((candidate) => {- const partitionStart = partitionStartByProjectId.get(- candidate.projectId,- );+ recordIncrement(`${METRIC_PREFIX}.enrichment_query_failures`, 1, {+ table: this.tableName,+ });+ logger.warn(`${this.instanceName}: Candidate enrichment failed`, {+ error: retryError,+ primaryError: error,+ candidatesFound: candidates.length,+ });++ await this.extendLockOnProgress();return {- ...candidate,- rowCount: null,- secondsPastCutoff: partitionStart- ? Math.max(- (candidate.cutoffDate.getTime() - partitionStart.getTime()) /- 1000,- 0,- )- : null,+ workloads: candidates.map((candidate) => ({+ ...candidate,+ rowCount: null,+ secondsPastCutoff: null,+ })),+ complete: false,};- });- } catch (error) {- if (error instanceof BatchDataRetentionCleanerLeaseLostError) {- throw error;}- recordIncrement(`${METRIC_PREFIX}.partition_fallback_query_failures`, 1, {- table: this.tableName,- });- logger.warn(`${this.instanceName}: Partition fallback query failed`, {- error,- candidatesFound: candidates.length,- });-- await this.extendLockOnProgress();- return candidates.map((candidate) => ({- ...candidate,- rowCount: null,- secondsPastCutoff: null,- }));}}