🪢 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 S3 list calls for OTEL observations (#6995)
Steffen Schmitzc359900b5921
diff --git a/packages/shared/src/server/ingestion/processEventBatch.ts b/packages/shared/src/server/ingestion/processEventBatch.tsindex e24f02df89..41c613fdd1 100644--- a/packages/shared/src/server/ingestion/processEventBatch.ts+++ b/packages/shared/src/server/ingestion/processEventBatch.ts@@ -79,11 +79,13 @@ const getDelay = (delay: number | null) => {* @param input - Batch of IngestionEventType. Will validate the types first thing and return errors if they are invalid.* @param authCheck - AuthHeaderValidVerificationResult* @param delay - (Optional) Delay in ms to wait before processing events in the batch.+ * @param source - (Optional) Source of the events for metrics tracking (e.g., "otel", "api").*/export const processEventBatch = async (input: unknown[],authCheck: AuthHeaderValidVerificationResult,delay: number | null = null,+ source: "api" | "otel" = "api",): Promise<{successes: { id: string; status: number }[];errors: {@@ -95,8 +97,10 @@ export const processEventBatch = async (}> => {// add context of api call to the spanconst currentSpan = getCurrentSpan();- recordIncrement("langfuse.ingestion.event", input.length);- recordDistribution("langfuse.ingestion.event_distribution", input.length);+ recordIncrement("langfuse.ingestion.event", input.length, { source });+ recordDistribution("langfuse.ingestion.event_distribution", input.length, {+ source,+ });currentSpan?.setAttribute("langfuse.ingestion.batch_size", input.length);currentSpan?.setAttribute(@@ -240,6 +244,11 @@ export const processEventBatch = async (type: sortedBatchByEventBodyId[id].type,eventBodyId: sortedBatchByEventBodyId[id].eventBodyId,fileKey: sortedBatchByEventBodyId[id].key,+ skipS3List:+ source === "otel" &&+ getClickhouseEntityType(+ sortedBatchByEventBodyId[id].type,+ ) === "observation",},authCheck: authCheck as {validKey: true;diff --git a/packages/shared/src/server/queues.ts b/packages/shared/src/server/queues.tsindex d2b1506640..a42f5b2286 100644--- a/packages/shared/src/server/queues.ts+++ b/packages/shared/src/server/queues.ts@@ -12,6 +12,7 @@ export const IngestionEvent = z.object({type: z.nativeEnum(eventTypes),eventBodyId: z.string(),fileKey: z.string().optional(),+ skipS3List: z.boolean().optional(),}),authCheck: z.object({validKey: z.literal(true),diff --git a/web/src/pages/api/public/otel/v1/traces/index.ts b/web/src/pages/api/public/otel/v1/traces/index.tsindex b2a3ab19c7..2eb4000be6 100644--- a/web/src/pages/api/public/otel/v1/traces/index.ts+++ b/web/src/pages/api/public/otel/v1/traces/index.ts@@ -94,7 +94,8 @@ export default withMiddlewares({convertOtelSpanToIngestionEvent(span, auth.scope.publicKey),);// We set a delay of 0 for OTel, as we never expect updates.- return processEventBatch(events, auth, 0);+ // We also set the source to "otel" which helps us with metric tracking and skipping list calls for S3.+ return processEventBatch(events, auth, 0, "otel");},}),});diff --git a/worker/src/queues/ingestionQueue.ts b/worker/src/queues/ingestionQueue.tsindex 9c0e4b8305..836ab77905 100644--- a/worker/src/queues/ingestionQueue.ts+++ b/worker/src/queues/ingestionQueue.ts@@ -132,9 +132,45 @@ export const ingestionQueueProcessorBuilder = (const clickhouseEntityType = getClickhouseEntityType(job.data.payload.data.type,);- const eventFiles = await s3Client.listFiles(- `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${clickhouseEntityType}/${job.data.payload.data.eventBodyId}/`,- );++ let eventFiles: { file: string; createdAt: Date }[] = [];+ const events: IngestionEventType[] = [];++ // Check if we should skip S3 list operation+ // The producer sets skipS3List to true if it's an OTel observation+ const shouldSkipS3List =+ job.data.payload.data.skipS3List && job.data.payload.data.fileKey;++ const s3Prefix = `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${clickhouseEntityType}/${job.data.payload.data.eventBodyId}/`;++ if (shouldSkipS3List) {+ // Direct file download - skip S3 list operation+ const filePath = `${s3Prefix}${job.data.payload.data.fileKey}.json`;+ eventFiles = [{ file: filePath, createdAt: new Date() }];++ const file = await s3Client.download(filePath);+ const parsedFile = JSON.parse(file);+ events.push(...(Array.isArray(parsedFile) ? parsedFile : [parsedFile]));+ } else {+ eventFiles = await s3Client.listFiles(s3Prefix);++ // Process files in batches+ // If a user has 5k events, this will likely take 100 seconds.+ const downloadAndParseFile = async (fileRef: { file: string }) => {+ const file = await s3Client.download(fileRef.file);+ const parsedFile = JSON.parse(file);+ return Array.isArray(parsedFile) ? parsedFile : [parsedFile];+ };++ const S3_CONCURRENT_READS = env.LANGFUSE_S3_CONCURRENT_READS;+ const batches = chunk(eventFiles, S3_CONCURRENT_READS);+ for (const batch of batches) {+ const batchEvents = await Promise.all(+ batch.map(downloadAndParseFile),+ );+ events.push(...batchEvents.flat());+ }+ }recordDistribution("langfuse.ingestion.count_files_distribution",@@ -155,23 +191,6 @@ export const ingestionQueueProcessorBuilder = (.sort().shift() ?? new Date();- const S3_CONCURRENT_READS = env.LANGFUSE_S3_CONCURRENT_READS;- const events: IngestionEventType[] = [];-- // Process files in batches- // If a user has 5k events, this will likely take 100 seconds.- const downloadAndParseFile = async (fileRef: { file: string }) => {- const file = await s3Client.download(fileRef.file);- const parsedFile = JSON.parse(file);- return Array.isArray(parsedFile) ? parsedFile : [parsedFile];- };-- const batches = chunk(eventFiles, S3_CONCURRENT_READS);- for (const batch of batches) {- const batchEvents = await Promise.all(batch.map(downloadAndParseFile));- events.push(...batchEvents.flat());- }-if (events.length === 0) {logger.warn(`No events found for project ${job.data.payload.authCheck.scope.projectId} and event ${job.data.payload.data.eventBodyId}`,