Skip to content

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

chore: decrease ingestion queue delay outside of date boundaries (#5364)

Steffen Schmitz1baa0aaaed94
diff --git a/packages/shared/src/server/ingestion/processEventBatch.ts b/packages/shared/src/server/ingestion/processEventBatch.ts
index 0799109007..388b9eb859 100644
--- a/packages/shared/src/server/ingestion/processEventBatch.ts
+++ b/packages/shared/src/server/ingestion/processEventBatch.ts
@@ -48,9 +48,37 @@ const getS3StorageServiceClient = (bucketName: string): StorageService => {
return s3StorageServiceClient;
};
+/**
+ * Get the delay for the event based on the event type. Uses delay if set, 0 if current UTC timestamp is not between
+ * 23:45 and 00:15, and env.LANGFUSE_INGESTION_QUEUE_DELAY_MS otherwise.
+ * We need the delay around date boundaries to avoid duplicates for out-of-order processing of events.
+ * @param delay - Delay overwrite. Used if non-null.
+ */
+const getDelay = (delay: number | null) => {
+ if (delay !== null) {
+ return delay;
+ }
+ const now = new Date();
+ const hours = now.getUTCHours();
+ const minutes = now.getUTCMinutes();
+
+ if ((hours === 23 && minutes >= 45) || (hours === 0 && minutes <= 15)) {
+ return env.LANGFUSE_INGESTION_QUEUE_DELAY_MS;
+ }
+
+ return 0;
+};
+
+/**
+ * Processes a batch of events.
+ * @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.
+ */
export const processEventBatch = async (
input: unknown[],
authCheck: AuthHeaderValidVerificationResult,
+ delay: number | null = null,
): Promise<{
successes: { id: string; status: number }[];
errors: {
@@ -210,9 +238,7 @@ export const processEventBatch = async (
authCheck,
},
},
- {
- delay: env.LANGFUSE_INGESTION_QUEUE_DELAY_MS,
- },
+ { delay: getDelay(delay) },
)
: Promise.reject("Failed to instantiate queue"),
),
diff --git a/web/src/pages/api/public/otel/v1/traces/index.ts b/web/src/pages/api/public/otel/v1/traces/index.ts
index 92b6197523..73b7591835 100644
--- a/web/src/pages/api/public/otel/v1/traces/index.ts
+++ b/web/src/pages/api/public/otel/v1/traces/index.ts
@@ -54,7 +54,8 @@ export default withMiddlewares({
const events: IngestionEventType[] = resourceSpans.flatMap(
convertOtelSpanToIngestionEvent,
);
- return processEventBatch(events, auth);
+ // We set a delay of 0 for OTel, as we never expect updates.
+ return processEventBatch(events, auth, 0);
},
}),
});