🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(api): get traces by tag (#820)
Marc Klingen917bb8931e69
diff --git a/fern/apis/server/definition/trace.yml b/fern/apis/server/definition/trace.ymlindex 2e0f577862..a5a0fe063d 100644--- a/fern/apis/server/definition/trace.yml+++ b/fern/apis/server/definition/trace.yml@@ -26,6 +26,10 @@ service:limit: optional<integer>userId: optional<string>name: optional<string>+ tags:+ type: optional<string>+ allow-multiple: true+ docs: Only traces that include all of these tags will be returned.response: Tracestypes:diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.ymlindex 17a0286d0e..19986a057e 100644--- a/generated/openapi-server/openapi.yml+++ b/generated/openapi-server/openapi.yml@@ -883,6 +883,15 @@ paths:schema:type: stringnullable: true+ - name: tags+ in: query+ description: Only traces that include all of these tags will be returned.+ required: false+ schema:+ type: array+ items:+ type: string+ nullable: trueresponses:'200':description: ''diff --git a/generated/postman/collection.json b/generated/postman/collection.jsonindex bef9a32b16..5987e52efd 100644--- a/generated/postman/collection.json+++ b/generated/postman/collection.json@@ -708,7 +708,7 @@"request": {"description": "Get list of traces","url": {- "raw": "{{baseUrl}}/api/public/traces?page=&limit=&userId=&name=",+ "raw": "{{baseUrl}}/api/public/traces?page=&limit=&userId=&name=&tags=","host": ["{{baseUrl}}"],@@ -737,6 +737,11 @@"key": "name","value": "","description": null+ },+ {+ "key": "tags",+ "value": "",+ "description": "Only traces that include all of these tags will be returned."}],"variable": []diff --git a/src/__tests__/traces.servertest.ts b/src/__tests__/traces.servertest.tsindex ad5df34d09..a21b439bac 100644--- a/src/__tests__/traces.servertest.ts+++ b/src/__tests__/traces.servertest.ts@@ -40,7 +40,6 @@ describe("/api/public/traces API Endpoint", () => {id: "trace-id",name: "trace-name",userId: "user-1",- projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",metadata: { key: "value" },release: "1.0.0",version: "2.0.0",@@ -65,7 +64,6 @@ describe("/api/public/traces API Endpoint", () => {await makeAPICall("POST", "/api/public/traces", {id: "trace-id",- projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",metadata: { key: "value" },release: "1.0.0",version: "5.0.0",@@ -88,4 +86,54 @@ describe("/api/public/traces API Endpoint", () => {userId: "user-1",});});++ it("should use tags correctly on POST and GET", async () => {+ await pruneDatabase();++ await makeAPICall("POST", "/api/public/traces", {+ id: "trace-1",+ tags: ["tag-1", "tag-2", "tag-3"],+ });++ await makeAPICall("POST", "/api/public/traces", {+ id: "trace-2",+ tags: ["tag-1"],+ });++ await makeAPICall("POST", "/api/public/traces", {+ id: "trace-3",+ tags: ["tag-2", "tag-3"],+ });++ // multiple tags+ const traces = await makeAPICall(+ "GET",+ "/api/public/traces?tags=tag-2&tags=tag-3",+ );+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access+ const traceIds = traces.body.data.map((t: { id: string }) => t.id);+ // check for equality ok as ordered by timestamp+ expect(traceIds).toEqual(["trace-3", "trace-1"]);++ // single tag+ const traces2 = await makeAPICall("GET", "/api/public/traces?tags=tag-1");+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access+ const traceIds2 = traces2.body.data.map((t: { id: string }) => t.id);+ // check for equality ok as ordered by timestamp+ expect(traceIds2).toEqual(["trace-2", "trace-1"]);++ // wrong tag+ const traces3 = await makeAPICall("GET", "/api/public/traces?tags=tag-10");+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access+ const traceIds3 = traces3.body.data.map((t: { id: string }) => t.id);+ // check for equality ok as ordered by timestamp+ expect(traceIds3).toEqual([]);++ // no tag+ const traces4 = await makeAPICall("GET", "/api/public/traces?tags=");+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access+ const traceIds4 = traces4.body.data.map((t: { id: string }) => t.id);+ // check for equality ok as ordered by timestamp+ expect(traceIds4).toEqual(["trace-3", "trace-2", "trace-1"]);+ });});diff --git a/src/pages/api/public/traces.ts b/src/pages/api/public/traces.tsindex d49ef09087..f8542dfdf5 100644--- a/src/pages/api/public/traces.ts+++ b/src/pages/api/public/traces.ts@@ -20,6 +20,7 @@ const GetTracesSchema = z.object({...paginationZod,userId: z.string().nullish(),name: z.string().nullish(),+ tags: z.union([z.array(z.string()), z.string()]).nullish(),});export default async function handler(@@ -76,8 +77,20 @@ export default async function handler(const obj = GetTracesSchema.parse(req.query); // uses query and not bodyconst skipValue = (obj.page - 1) * obj.limit;- const userCondition = Prisma.sql`AND t."user_id" = ${obj.userId}`;- const nameCondition = Prisma.sql`AND t."name" = ${obj.name}`;+ const userCondition = obj.userId+ ? Prisma.sql`AND t."user_id" = ${obj.userId}`+ : Prisma.empty;+ const nameCondition = obj.name+ ? Prisma.sql`AND t."name" = ${obj.name}`+ : Prisma.empty;+ const tagsCondition = obj.tags+ ? Prisma.sql`AND ARRAY[${Prisma.join(+ (Array.isArray(obj.tags) ? obj.tags : [obj.tags]).map(+ (v) => Prisma.sql`${v}`,+ ),+ ", ",+ )}] <@ t."tags"`+ : Prisma.empty;const traces = await prisma.$queryRaw<Array<Trace & { observations: string[]; scores: string[] }>@@ -92,15 +105,16 @@ export default async function handler(t.user_id as "userId",t.release,t.version,+ t.tags,array_remove(array_agg(o.id), NULL) AS "observations",array_remove(array_agg(s.id), NULL) AS "scores"FROM "traces" AS t- LEFT JOIN "observations" AS o ON t.id = o.trace_id+ LEFT JOIN "observations" AS o ON t.id = o.trace_id AND o.project_id = ${authCheck.scope.projectId}LEFT JOIN "scores" AS s ON t.id = s.trace_idWHERE t.project_id = ${authCheck.scope.projectId}- AND o.project_id = ${authCheck.scope.projectId}- ${obj.userId ? userCondition : Prisma.empty}- ${obj.name ? nameCondition : Prisma.empty}+ ${userCondition}+ ${nameCondition}+ ${tagsCondition}GROUP BY t.idORDER BY t."timestamp" DESCLIMIT ${obj.limit} OFFSET ${skipValue}