Skip to content

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

fix: handle errors in worker API (#2412)

Max Deichmann2ea0b9bac43f
diff --git a/worker/src/api/index.ts b/worker/src/api/index.ts
index 3fba43c5aa..4761b71b12 100644
--- a/worker/src/api/index.ts
+++ b/worker/src/api/index.ts
@@ -28,7 +28,7 @@ export const evalQueue = redis
: null;
type EventsResponse = {
- status: "success";
+ status: "success" | "error";
};
router.get<{}, { status: string }>("/health", async (_req, res) => {
@@ -68,36 +68,50 @@ router
})
)
.post<{}, EventsResponse>("/events", async (req, res) => {
- const { body } = req;
- logger.info(`Received events, ${JSON.stringify(body)}`);
-
- const event = EventBodySchema.parse(body);
-
- if (event.name === EventName.TraceUpsert) {
- // 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(event.payload);
- await evalQueue?.addBulk(jobs); // add all jobs as bulk
-
- return res.json({
- status: "success",
- });
- }
-
- if (event.name === EventName.BatchExport) {
- await batchExportQueue?.add(event.name, {
- id: event.payload.batchExportId, // Use the batchExportId to deduplicate when the same job is sent multiple times
- name: QueueJobs.BatchExportJob,
- timestamp: new Date(),
- payload: event.payload,
- });
-
- return res.json({
- status: "success",
+ try {
+ const { body } = req;
+ logger.info(`Received events, ${JSON.stringify(body)}`);
+
+ const event = EventBodySchema.safeParse(body);
+
+ if (!event.success) {
+ logger.error("Invalid event body", event.error);
+ return res.status(400).json({
+ status: "error",
+ });
+ }
+
+ if (event.data.name === EventName.TraceUpsert) {
+ // 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(event.data.payload);
+ await evalQueue?.addBulk(jobs); // add all jobs as bulk
+
+ return res.json({
+ status: "success",
+ });
+ }
+
+ if (event.data.name === EventName.BatchExport) {
+ await batchExportQueue?.add(event.data.name, {
+ id: event.data.payload.batchExportId, // Use the batchExportId to deduplicate when the same job is sent multiple times
+ name: QueueJobs.BatchExportJob,
+ timestamp: new Date(),
+ payload: event.data.payload,
+ });
+
+ return res.json({
+ status: "success",
+ });
+ }
+
+ return res.status(400);
+ } catch (e) {
+ logger.error(e, "Error processing events");
+ return res.status(500).json({
+ status: "error",
});
}
-
- return res.status(400);
});
router.use("/emojis", emojis);