Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: report data retention behind cutoff as p90 (#11717)

Valery Meleshkinc3cc1d0a0d52
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index a4f2053e75..659d2f4100 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -17,6 +17,7 @@ export * from "./utils/typeChecks";
export * from "./utils/prompts";
export * from "./utils/jsonSchemaValidation";
export * from "./utils/chatml";
+export * from "./utils/math";
export * from "./features/entitlements/plans";
export * from "./interfaces/rate-limits";
export * from "./tableDefinitions/typeHelpers";
diff --git a/packages/shared/src/utils/math.ts b/packages/shared/src/utils/math.ts
new file mode 100644
index 0000000000..4fed54fe5d
--- /dev/null
+++ b/packages/shared/src/utils/math.ts
@@ -0,0 +1,10 @@
+/**
+ * Calculate the p-th percentile of a numeric array.
+ * Returns 0 for empty arrays.
+ */
+export function percentile(values: number[], p: number): number {
+ if (values.length === 0) return 0;
+ const sorted = values.toSorted((a, b) => a - b);
+ const index = Math.ceil(p * sorted.length) - 1;
+ return sorted[Math.max(0, index)];
+}
diff --git a/worker/src/features/batch-data-retention-cleaner/index.ts b/worker/src/features/batch-data-retention-cleaner/index.ts
index d7b635caea..8afbb70b87 100644
--- a/worker/src/features/batch-data-retention-cleaner/index.ts
+++ b/worker/src/features/batch-data-retention-cleaner/index.ts
@@ -1,5 +1,6 @@
import { createHash } from "crypto";
+import { percentile } from "@langfuse/shared";
import { prisma } from "@langfuse/shared/src/db";
import {
commandClickhouse,
@@ -121,16 +122,25 @@ export class BatchDataRetentionCleaner {
table: tableName,
});
- // Compute seconds past cutoff for each workload and find the max
+ // Compute seconds past cutoff for each workload
const SECONDS_PER_DAY = 86400;
- const maxSecondsPastCutoff = workloads
+ const secondsPastCutoffByProject = workloads
.filter((w) => w.oldestAgeSeconds !== null)
- .map((w) => w.oldestAgeSeconds! - w.retentionDays * SECONDS_PER_DAY)
- .reduce((max, val) => Math.max(max, val), -Infinity);
+ .map((w) => ({
+ projectId: w.projectId,
+ secondsPastCutoff:
+ w.oldestAgeSeconds! - w.retentionDays * SECONDS_PER_DAY,
+ }));
+
+ // Compute p90 for the gauge metric (0 when no pending work)
+ const p90SecondsPastCutoff = percentile(
+ secondsPastCutoffByProject.map((p) => p.secondsPastCutoff),
+ 0.9,
+ );
recordGauge(
`${METRIC_PREFIX}.seconds_past_cutoff`,
- Math.max(maxSecondsPastCutoff, 0),
+ Math.max(p90SecondsPastCutoff, 0),
{
table: tableName,
},
@@ -145,7 +155,7 @@ export class BatchDataRetentionCleaner {
logger.info(`${instanceName}: Processing ${workloads.length} projects`, {
projectIds: workloads.map((w) => w.projectId),
- secondsPastCutoff: maxSecondsPastCutoff,
+ secondsPastCutoffByProject,
});
// Step 2: Execute single batch DELETE for all selected projects