Skip to content

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

feat(api): add sessionId filter for traces endpoint (#2227)

Richard Krümmele3b4dc192818
diff --git a/fern/apis/server/definition/trace.yml b/fern/apis/server/definition/trace.yml
index e1f3c79017..1c16c27d9b 100644
--- a/fern/apis/server/definition/trace.yml
+++ b/fern/apis/server/definition/trace.yml
@@ -30,6 +30,7 @@ service:
docs: Limit of items per page. If you encounter api issues due to too large page sizes, try to reduce the limit.
userId: optional<string>
name: optional<string>
+ sessionId: optional<string>
fromTimestamp:
type: optional<datetime>
docs: Retrieve only traces newer than this datetime (ISO 8601).
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index 99e45a43aa..9081dd59e8 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -1262,6 +1262,12 @@ paths:
schema:
type: string
nullable: true
+ - name: sessionId
+ in: query
+ required: false
+ schema:
+ type: string
+ nullable: true
- name: fromTimestamp
in: query
description: Retrieve only traces newer than this datetime (ISO 8601).
diff --git a/web/public/generated/postman/collection.json b/web/public/generated/postman/collection.json
index 65757b2af2..b5500dc24f 100644
--- a/web/public/generated/postman/collection.json
+++ b/web/public/generated/postman/collection.json
@@ -963,7 +963,7 @@
"request": {
"description": "Get list of traces.",
"url": {
- "raw": "{{baseUrl}}/api/public/traces?page=&limit=&userId=&name=&fromTimestamp=&orderBy=&tags=",
+ "raw": "{{baseUrl}}/api/public/traces?page=&limit=&userId=&name=&sessionId=&fromTimestamp=&orderBy=&tags=",
"host": [
"{{baseUrl}}"
],
@@ -993,6 +993,11 @@
"value": "",
"description": null
},
+ {
+ "key": "sessionId",
+ "value": "",
+ "description": null
+ },
{
"key": "fromTimestamp",
"value": "",
diff --git a/web/src/__tests__/traces.servertest.ts b/web/src/__tests__/traces.servertest.ts
index ae0bff4720..738f58637a 100644
--- a/web/src/__tests__/traces.servertest.ts
+++ b/web/src/__tests__/traces.servertest.ts
@@ -238,4 +238,62 @@ describe("/api/public/traces API Endpoint", () => {
expect(trace.body.htmlPath).toContain(`/traces/${traceId}`);
expect(trace.body.htmlPath).toContain(`/project/`); // do not know the projectId
});
+
+ it("should filter traces by session ID", async () => {
+ const sessionId = "test-session-id";
+ const anotherSessionId = "another-session-id";
+
+ // Create traces with different session IDs
+ await makeAPICall("POST", "/api/public/traces", {
+ id: "trace-1",
+ name: "test-trace-1",
+ sessionId,
+ userId: "user-1",
+ projectId: "project-1",
+ metadata: { key: "value" },
+ release: "1.0.0",
+ version: "1.0.0",
+ });
+
+ await makeAPICall("POST", "/api/public/traces", {
+ id: "trace-2",
+ name: "test-trace-2",
+ sessionId: anotherSessionId,
+ userId: "user-2",
+ projectId: "project-1",
+ metadata: { key: "value" },
+ release: "1.0.0",
+ version: "1.0.0",
+ });
+
+ // Filter by session ID
+ const tracesBySessionId = await makeAPICall<GetTracesAPIResponse>(
+ "GET",
+ `/api/public/traces?sessionId=${sessionId}`,
+ );
+
+ expect(tracesBySessionId.status).toBe(200);
+ expect(tracesBySessionId.body.data).toHaveLength(1);
+ expect(tracesBySessionId.body.data[0].id).toBe("trace-1");
+
+ // Filter by another session ID
+ const tracesByAnotherSessionId = await makeAPICall<GetTracesAPIResponse>(
+ "GET",
+ `/api/public/traces?sessionId=${anotherSessionId}`,
+ );
+
+ expect(tracesByAnotherSessionId.status).toBe(200);
+ expect(tracesByAnotherSessionId.body.data).toHaveLength(1);
+ expect(tracesByAnotherSessionId.body.data[0].id).toBe("trace-2");
+
+ // Filter by non-existent session ID
+ const tracesByNonExistentSessionId =
+ await makeAPICall<GetTracesAPIResponse>(
+ "GET",
+ `/api/public/traces?sessionId=non-existent-session-id`,
+ );
+
+ expect(tracesByNonExistentSessionId.status).toBe(200);
+ expect(tracesByNonExistentSessionId.body.data).toHaveLength(0);
+ });
});
diff --git a/web/src/pages/api/public/traces.ts b/web/src/pages/api/public/traces.ts
index b09d636a2e..1947e1ce31 100644
--- a/web/src/pages/api/public/traces.ts
+++ b/web/src/pages/api/public/traces.ts
@@ -25,6 +25,7 @@ const GetTracesSchema = z.object({
userId: z.string().nullish(),
name: z.string().nullish(),
tags: z.union([z.array(z.string()), z.string()]).nullish(),
+ sessionId: z.string().nullish(),
fromTimestamp: stringDate,
orderBy: z
.string() // orderBy=timestamp.asc
@@ -105,6 +106,9 @@ export default async function handler(
", ",
)}] <@ t."tags"`
: Prisma.empty;
+ const sessionCondition = obj.sessionId
+ ? Prisma.sql`AND t."session_id" = ${obj.sessionId}`
+ : Prisma.empty;
const fromTimestampCondition = obj.fromTimestamp
? Prisma.sql`AND t."timestamp" >= ${obj.fromTimestamp}::timestamp with time zone at time zone 'UTC'`
: Prisma.empty;
@@ -145,6 +149,7 @@ export default async function handler(
${userCondition}
${nameCondition}
${tagsCondition}
+ ${sessionCondition}
${orderByCondition}
LIMIT ${obj.limit} OFFSET ${skipValue}
) AS t
@@ -168,6 +173,7 @@ export default async function handler(
projectId: authCheck.scope.projectId,
name: obj.name ? obj.name : undefined,
userId: obj.userId ? obj.userId : undefined,
+ sessionId: obj.sessionId ? obj.sessionId : undefined,
timestamp: obj.fromTimestamp
? { gte: new Date(obj.fromTimestamp) }
: undefined,