🪢 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
perf: skip processing of already seen events in worker (#5437)
Steffen Schmitz0dfefd279357
diff --git a/packages/shared/src/server/ingestion/processEventBatch.ts b/packages/shared/src/server/ingestion/processEventBatch.tsindex 51d3167d2b..cbbfcbb237 100644--- a/packages/shared/src/server/ingestion/processEventBatch.ts+++ b/packages/shared/src/server/ingestion/processEventBatch.ts@@ -47,7 +47,10 @@ const getDelay = (delay: number | null) => {return env.LANGFUSE_INGESTION_QUEUE_DELAY_MS;}- return 0;+ // Use 5s here to avoid duplicate processing on the worker. If the ingestion delay is set to a lower value,+ // we use this instead.+ // Values should be revisited based on a cost/performance trade-off.+ return Math.min(5000, env.LANGFUSE_INGESTION_QUEUE_DELAY_MS);};/**@@ -225,6 +228,7 @@ export const processEventBatch = async (data: {type: sortedBatchByEventBodyId[id].type,eventBodyId: sortedBatchByEventBodyId[id].eventBodyId,+ fileKey: sortedBatchByEventBodyId[id].key,},authCheck,},diff --git a/packages/shared/src/server/queues.ts b/packages/shared/src/server/queues.tsindex 3c2c752c52..7dbbe5e18e 100644--- a/packages/shared/src/server/queues.ts+++ b/packages/shared/src/server/queues.ts@@ -10,6 +10,7 @@ export const IngestionEvent = z.object({data: z.object({type: z.nativeEnum(eventTypes),eventBodyId: z.string(),+ fileKey: z.string().optional(),}),authCheck: z.object({validKey: z.literal(true),diff --git a/worker/src/env.ts b/worker/src/env.tsindex 431946f78f..9fdc399d53 100644--- a/worker/src/env.ts+++ b/worker/src/env.ts@@ -126,6 +126,10 @@ const EnvSchema = z.object({.enum(["true", "false"]).default("true"),+ LANGFUSE_ENABLE_REDIS_SEEN_EVENT_CACHE: z+ .enum(["true", "false"])+ .default("false"),+// Flags to toggle queue consumers on or off.QUEUE_CONSUMER_CLOUD_USAGE_METERING_QUEUE_IS_ENABLED: z.enum(["true", "false"])diff --git a/worker/src/queues/ingestionQueue.ts b/worker/src/queues/ingestionQueue.tsindex 06adf51b57..f0ccbd3b5c 100644--- a/worker/src/queues/ingestionQueue.ts+++ b/worker/src/queues/ingestionQueue.ts@@ -13,6 +13,7 @@ import {getCurrentSpan,getQueue,recordDistribution,+ recordIncrement,} from "@langfuse/shared/src/server";import { prisma } from "@langfuse/shared/src/db";@@ -61,6 +62,35 @@ export const ingestionQueueProcessorBuilder = ("messaging.bullmq.job.input.type",job.data.payload.data.type,);+ span.setAttribute(+ "messaging.bullmq.job.input.fileKey",+ job.data.payload.data.fileKey ?? "",+ );+ }++ // If fileKey was processed within the last minutes, i.e. has a match in redis, we skip processing.+ if (+ env.LANGFUSE_ENABLE_REDIS_SEEN_EVENT_CACHE === "true" &&+ redis &&+ job.data.payload.data.fileKey+ ) {+ const key = `langfuse:ingestion:recently-processed:${job.data.payload.authCheck.scope.projectId}:${job.data.payload.data.type}:${job.data.payload.data.eventBodyId}:${job.data.payload.data.fileKey}`;+ const exists = await redis.exists(key);+ if (exists) {+ recordIncrement("langfuse.ingestion.recently_processed_cache", 1, {+ type: job.data.payload.data.type,+ skipped: "true",+ });+ logger.debug(+ `Skipping ingestion event ${job.data.payload.data.fileKey} for project ${job.data.payload.authCheck.scope.projectId}`,+ );+ return;+ } else {+ recordIncrement("langfuse.ingestion.recently_processed_cache", 1, {+ type: job.data.payload.data.type,+ skipped: "false",+ });+ }}if (@@ -177,6 +207,21 @@ export const ingestionQueueProcessorBuilder = (return;}+ // Set "seen" keys in Redis to avoid reprocessing for fast updates.+ if (env.LANGFUSE_ENABLE_REDIS_SEEN_EVENT_CACHE === "true" && redis) {+ const pipeline = redis.pipeline();+ for (const event of eventFiles) {+ const key = event.file.split("/").pop() ?? "";+ pipeline.set(+ `langfuse:ingestion:recently-processed:${job.data.payload.authCheck.scope.projectId}:${job.data.payload.data.type}:${job.data.payload.data.eventBodyId}:${key?.replace(".json", "")}`,+ "1",+ "EX",+ 60 * 5, // 5 minutes+ );+ }+ await pipeline.exec();+ }+// Perform merge of those eventsif (!redis) throw new Error("Redis not available");if (!prisma) throw new Error("Prisma not available");