🪢 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
chore: allow skipping ingestion CH reads for projects created after cutoff date (#5116)
Steffen Schmitz026f7f1b4b73
diff --git a/worker/src/env.ts b/worker/src/env.tsindex 9fa948ef67..ccc315625f 100644--- a/worker/src/env.ts+++ b/worker/src/env.ts@@ -107,6 +107,12 @@ const EnvSchema = z.object({// project ids. Applicable for projects that were created after the S3 write// was activated and which don't rely on historic updates.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_PROJECT_IDS: z.string().default(""),+ // Set a date after which S3 was active. Projects created after this date do+ // perform a ClickHouse read as part of the ingestion pipeline.+ LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_MIN_PROJECT_CREATE_DATE: z+ .string()+ .date()+ .optional(),// OtelOTEL_EXPORTER_OTLP_ENDPOINT: z.string().default("http://localhost:4318"),diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.tsindex 6ffbcfe8c9..a82faf9bf7 100644--- a/worker/src/services/IngestionService/index.ts+++ b/worker/src/services/IngestionService/index.ts@@ -2,7 +2,13 @@ import { Redis } from "ioredis";import { v4 } from "uuid";import { Prisma } from "@prisma/client";-import { Model, Price, PrismaClient, Prompt } from "@langfuse/shared";+import {+ LangfuseNotFoundError,+ Model,+ Price,+ PrismaClient,+ Prompt,+} from "@langfuse/shared";import {ClickhouseClientType,IngestionEntityTypes,@@ -722,6 +728,57 @@ export class IngestionService {};}+ private skipClickhouseReadProjectsCache = new Map<string, boolean>();++ private async shouldSkipClickHouseRead(+ projectId: string,+ minProjectCreateDate: string | undefined = undefined,+ ): Promise<boolean> {+ if (+ env.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_PROJECT_IDS &&+ env.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_PROJECT_IDS.split(+ ",",+ ).includes(projectId)+ ) {+ return true;+ }++ if (+ !env.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_MIN_PROJECT_CREATE_DATE &&+ !minProjectCreateDate+ ) {+ return false;+ }++ if (this.skipClickhouseReadProjectsCache.has(projectId)) {+ return this.skipClickhouseReadProjectsCache.get(projectId) ?? false;+ }++ const project = await this.prisma.project.findFirst({+ where: {+ id: projectId,+ deletedAt: null,+ },+ select: {+ id: true,+ createdAt: true,+ },+ });++ if (!project) {+ throw new LangfuseNotFoundError(`Project ${projectId} not found`);+ }++ const cutoffDate = new Date(+ env.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_MIN_PROJECT_CREATE_DATE ??+ minProjectCreateDate ??+ new Date(), // Fallback to today. Should never apply.+ );+ const result = project.createdAt >= cutoffDate;+ this.skipClickhouseReadProjectsCache.set(projectId, result);+ return result;+ }+private async getClickhouseRecord(params: {projectId: string;entityId: string;@@ -758,12 +815,7 @@ export class IngestionService {params: Record<string, unknown>;};}) {- if (- env.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_PROJECT_IDS &&- env.LANGFUSE_SKIP_INGESTION_CLICKHOUSE_READ_PROJECT_IDS.split(- ",",- ).includes(params.projectId)- ) {+ if (await this.shouldSkipClickHouseRead(params.projectId)) {return null;}diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.tsindex 3c6e8ac2ea..1511b84a46 100644--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts@@ -1925,6 +1925,22 @@ describe("Ingestion end-to-end tests", () => {expect(trace.user_id).toBe("user-1");});+ it("should skip clickhouse read for recently created projects", async () => {+ const projectId = randomUUID();+ await prisma.project.create({+ data: {+ id: projectId,+ name: randomUUID(),+ orgId: "seed-org-id",+ },+ });+ const shouldSkip = await ingestionService.shouldSkipClickHouseRead(+ projectId,+ "2024-01-01", // Use some date in the past+ );+ expect(shouldSkip).toBe(true);+ });+[{inputs: [{ a: "a" }, { b: "b" }],