Skip to content

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

fix: deduplicate events on worker ingestion (#1908)

Max Deichmannc338db5a4da6
diff --git a/worker/src/__tests__/evnets-api.test.ts b/worker/src/__tests__/evnets-api.test.ts
new file mode 100644
index 0000000000..d9bebed0b7
--- /dev/null
+++ b/worker/src/__tests__/evnets-api.test.ts
@@ -0,0 +1,31 @@
+import { expect, test, describe } from "vitest";
+import { createRedisEvents } from "../api";
+
+describe.sequential("create redis events", () => {
+ test("deduplicate events from the same project id", async () => {
+ const events = [
+ { traceId: "trace1", projectId: "project1" },
+ { traceId: "trace2", projectId: "project1" },
+ { traceId: "trace2", projectId: "project2" },
+ { traceId: "trace3", projectId: "project2" },
+ { traceId: "trace3", projectId: "project2" },
+ ];
+
+ const jobs = createRedisEvents(events);
+
+ expect(jobs).toBeDefined();
+ expect(jobs.length).toBe(4);
+
+ const traceIdProjectIds = jobs.map((event) => ({
+ projectId: event.data.payload.projectId,
+ traceId: event.data.payload.traceId,
+ }));
+
+ expect(traceIdProjectIds).toEqual([
+ { traceId: "trace1", projectId: "project1" },
+ { traceId: "trace2", projectId: "project1" },
+ { traceId: "trace2", projectId: "project2" },
+ { traceId: "trace3", projectId: "project2" },
+ ]);
+ });
+});
diff --git a/worker/src/api/index.ts b/worker/src/api/index.ts
index bc84352add..d2d077e0d8 100644
--- a/worker/src/api/index.ts
+++ b/worker/src/api/index.ts
@@ -71,27 +71,9 @@ router
const events = eventBody.parse(body);
- const jobs = events.map((event) => ({
- name: QueueJobs.TraceUpsert,
- data: {
- payload: {
- projectId: event.projectId,
- traceId: event.traceId,
- },
- id: randomUUID(),
- timestamp: new Date(),
- name: QueueJobs.TraceUpsert as const,
- },
- opts: {
- removeOnFail: 10_000,
- removeOnComplete: true,
- attempts: 5,
- backoff: {
- type: "exponential",
- delay: 1000,
- },
- },
- }));
+ // Find set of traces per project. There might be two events for the same trace in one API call.
+ // If we don't deduplicate, we will end up processing the same trace twice on two different workers in parallel.
+ const jobs = createRedisEvents(events);
await evalQueue?.addBulk(jobs); // add all jobs as bulk
@@ -103,3 +85,42 @@ router
router.use("/emojis", emojis);
export default router;
+
+export function createRedisEvents(events: z.infer<typeof eventBody>) {
+ const uniqueTracesPerProject = events.reduce((acc, event) => {
+ if (!acc.get(event.projectId)) {
+ acc.set(event.projectId, new Set());
+ }
+ acc.get(event.projectId)?.add(event.traceId);
+ return acc;
+ }, new Map<string, Set<string>>());
+
+ const jobs = [...uniqueTracesPerProject.entries()]
+ .map((tracesPerProject) => {
+ const [projectId, traceIds] = tracesPerProject;
+
+ return [...traceIds].map((traceId) => ({
+ name: QueueJobs.TraceUpsert,
+ data: {
+ payload: {
+ projectId,
+ traceId,
+ },
+ id: randomUUID(),
+ timestamp: new Date(),
+ name: QueueJobs.TraceUpsert as const,
+ },
+ opts: {
+ removeOnFail: 10000,
+ removeOnComplete: true,
+ attempts: 5,
+ backoff: {
+ type: "exponential",
+ delay: 1000,
+ },
+ },
+ }));
+ })
+ .flat();
+ return jobs;
+}