🪢 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
fix: allow userId filter on /api/public/observations again (#4644)
Steffen Schmitzd7832336dd9c
diff --git a/web/src/__tests__/async/observation-api.servertest.ts b/web/src/__tests__/async/observation-api.servertest.tsindex 4706297fbd..558bb22592 100644--- a/web/src/__tests__/async/observation-api.servertest.ts+++ b/web/src/__tests__/async/observation-api.servertest.ts@@ -1,18 +1,22 @@-import { createObservation as createObservationObject } from "@langfuse/shared/src/server";+import {+ createObservation as createObservationObject,+ createTrace,+ createTracesCh,+} from "@langfuse/shared/src/server";import { createObservationsCh as createObservationsInClickhouse } from "@langfuse/shared/src/server";import { makeZodVerifiedAPICall } from "@/src/__tests__/test-utils";import { GetObservationV1Response } from "@/src/features/public-api/types/observations";-import { v4 } from "uuid";import { v4 as uuidv4 } from "uuid";import { GetObservationsV1Response } from "@/src/features/public-api/types/observations";+import { snakeCase } from "lodash";const projectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";describe("/api/public/observations API Endpoint", () => {describe("GET /api/public/observations/:id", () => {it("should GET an observation", async () => {- const observationId = v4();- const traceId = v4();+ const observationId = uuidv4();+ const traceId = uuidv4();const observation = createObservationObject({id: observationId,@@ -137,8 +141,6 @@ describe("/api/public/observations API Endpoint", () => {undefined,);- console.log(fetchedObservations.body);-expect(fetchedObservations.status).toBe(200);expect(fetchedObservations.body.data.length).toBe(1);@@ -148,8 +150,55 @@ describe("/api/public/observations API Endpoint", () => {expect(fetchedObservations.body.data[0]?.type).toEqual("GENERATION");});+ it.each([+ ["userId", uuidv4()],+ ["traceId", uuidv4()],+ ["name", uuidv4()],+ ["version", uuidv4()],+ ])(+ "should fetch all observations filtered by a value (%s, %s)",+ async (prop: string, value: string) => {+ const traceId = uuidv4();++ if (prop === "userId") {+ const createdTrace = createTrace({+ id: traceId,+ [snakeCase(prop)]: value,+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",+ });++ await createTracesCh([createdTrace]);+ }++ const observation = createObservationObject({+ id: uuidv4(),+ trace_id: traceId,+ start_time: new Date("2021-01-01T00:00:00.000Z").getTime(),+ end_time: new Date("2021-01-01T00:00:00.000Z").getTime(),+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",+ type: "GENERATION",+ [snakeCase(prop)]: value,+ });++ await createObservationsInClickhouse([observation]);++ const observations = await makeZodVerifiedAPICall(+ GetObservationsV1Response,+ "GET",+ `/api/public/observations?${prop}=${value}`,+ );++ expect(observations.body.meta.totalItems).toBe(1);+ expect(observations.body.data.length).toBe(1);+ const obsResult = observations.body.data[0];+ expect(obsResult.projectId).toBe("7a88fb47-b4e2-43b8-a06c-a5ce950dc53a");+ if (prop === "userId") return;+ expect((obsResult as any)[prop]).toBe(value);+ },+ );+it("GET /observations with timestamp filters and pagination", async () => {- const traceId = v4();+ const traceId = uuidv4();const obs1 = createObservationObject({id: "observation-2021-01-01",trace_id: traceId,diff --git a/web/src/features/public-api/server/observations.ts b/web/src/features/public-api/server/observations.tsindex 20c1d68e9b..a223403241 100644--- a/web/src/features/public-api/server/observations.ts+++ b/web/src/features/public-api/server/observations.ts@@ -21,8 +21,9 @@ type QueryType = {};export const generateObservationsForPublicApi = async (props: QueryType) => {- const filter = generateFilter(props);- const appliedFilter = filter.apply();+ const chFilter = generateFilter(props);+ const appliedFilter = chFilter.apply();+ const traceFilter = chFilter.find((f) => f.clickhouseTable === "traces");const query = `SELECT@@ -56,7 +57,9 @@ export const generateObservationsForPublicApi = async (props: QueryType) => {updated_at,event_tsFROM observations o- WHERE project_id = {projectId: String}+ ${traceFilter ? `LEFT JOIN traces t ON o.trace_id = t.id AND t.project_id = o.project_id` : ""}+ WHERE o.project_id = {projectId: String}+ ${traceFilter ? `AND t.project_id = {projectId: String}` : ""}AND ${appliedFilter.query}ORDER BY start_time descLIMIT 1 by id, project_id@@ -78,15 +81,18 @@ export const generateObservationsForPublicApi = async (props: QueryType) => {};export const getObservationsCountForPublicApi = async (props: QueryType) => {- const filter = generateFilter(props).apply();+ const chFilter = generateFilter(props);+ const filter = chFilter.apply();+ const traceFilter = chFilter.find((f) => f.clickhouseTable === "traces");const query = `- SELECT- count() as count- FROM observations o- WHERE project_id = {projectId: String}- AND ${filter.query}- `;+ SELECT count() as count+ FROM observations o+ ${traceFilter ? `LEFT JOIN traces t ON o.trace_id = t.id AND t.project_id = o.project_id` : ""}+ WHERE o.project_id = {projectId: String}+ ${traceFilter ? `AND t.project_id = {projectId: String}` : ""}+ AND ${filter.query}+ `;const records = await queryClickhouse<{ count: string }>({query,@@ -100,8 +106,8 @@ const filterParams = [id: "userId",clickhouseSelect: "user_id",filterType: "StringFilter",- clickhouseTable: "observations",- clickhousePrefix: "o",+ clickhouseTable: "traces",+ clickhousePrefix: "t",},{id: "traceId",