Skip to content

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

fix: ordering of ingestion events by timestamp (#2417)

Max Deichmann983f34f49d7d
diff --git a/web/src/__tests__/ingestion.servertest.ts b/web/src/__tests__/ingestion.servertest.ts
index edc4da3e6a..8afd957d8e 100644
--- a/web/src/__tests__/ingestion.servertest.ts
+++ b/web/src/__tests__/ingestion.servertest.ts
@@ -769,6 +769,52 @@ describe("/api/public/ingestion API Endpoint", () => {
expect(dbTrace[0]?.tags.length).toBe(4);
});
+ it("should upsert traces in the right order", async () => {
+ const traceId = v4();
+
+ const latestEvent = new Date();
+ const oldEvent = new Date(latestEvent).setSeconds(
+ latestEvent.getSeconds() - 1,
+ );
+
+ const responseOne = await makeAPICall("POST", "/api/public/ingestion", {
+ batch: [
+ {
+ id: v4(),
+ type: "trace-create",
+ timestamp: latestEvent,
+ body: {
+ id: traceId,
+ name: "trace-name",
+ userId: "user-1",
+ },
+ },
+ {
+ id: v4(),
+ type: "trace-create",
+ timestamp: oldEvent,
+ body: {
+ id: traceId,
+ name: "trace-name",
+ userId: "user-2",
+ },
+ },
+ ],
+ });
+ expect(responseOne.status).toBe(207);
+
+ const dbTrace = await prisma.trace.findMany({
+ where: {
+ name: "trace-name",
+ },
+ });
+
+ expect(dbTrace.length).toBeGreaterThan(0);
+ expect(dbTrace[0]?.name).toBe("trace-name");
+ expect(dbTrace[0]?.userId).toBe("user-1");
+ expect(dbTrace[0]?.projectId).toBe("7a88fb47-b4e2-43b8-a06c-a5ce950dc53a");
+ });
+
it("should fail for wrong event formats", async () => {
const traceId = v4();
const scoreId = v4();
diff --git a/web/src/pages/api/public/ingestion.ts b/web/src/pages/api/public/ingestion.ts
index 8f6a567b8f..406e992be4 100644
--- a/web/src/pages/api/public/ingestion.ts
+++ b/web/src/pages/api/public/ingestion.ts
@@ -165,17 +165,26 @@ export default async function handler(
}
}
-const sortBatch = (batch: Array<z.infer<typeof ingestionEvent>>) => {
- // keep the order of events as they are. Order events in a way that types containing updates come last
- // Filter out OBSERVATION_UPDATE events
- const updates = batch.filter(
- (event) => event.type === eventTypes.OBSERVATION_UPDATE,
- );
+/**
+ * Sorts a batch of ingestion events. Orders by: updating events last, sorted by timestamp asc.
+ */
- // Keep all other events in their original order
- const others = batch.filter(
- (event) => event.type !== eventTypes.OBSERVATION_UPDATE,
- );
+const sortBatch = (batch: Array<z.infer<typeof ingestionEvent>>) => {
+ const updateEvents: (typeof eventTypes)[keyof typeof eventTypes][] = [
+ eventTypes.GENERATION_UPDATE,
+ eventTypes.SPAN_UPDATE,
+ eventTypes.OBSERVATION_UPDATE, // legacy event type
+ ];
+ const updates = batch
+ .filter((event) => updateEvents.includes(event.type))
+ .sort((a, b) => {
+ return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
+ });
+ const others = batch
+ .filter((event) => !updateEvents.includes(event.type))
+ .sort((a, b) => {
+ return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
+ });
// Return the array with non-update events first, followed by update events
return [...others, ...updates];