Skip to content

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

fix(evals): add missing observation columns to checkTraceExistsAndGetTimestamp CTE (#12269)

Valery Meleshkinba94dd7235d4
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index ee286b279f..7e00bf5fd9 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -107,6 +107,9 @@ export const checkTraceExistsAndGetTimestamp = async ({
countIf(level = 'WARNING') as warning_count,
countIf(level = 'DEFAULT') as default_count,
countIf(level = 'DEBUG') as debug_count,
+ date_diff('millisecond', least(min(start_time), min(end_time)), greatest(max(start_time), max(end_time))) as latency_milliseconds,
+ sumMap(usage_details) as usage_details,
+ sumMap(cost_details) as cost_details,
trace_id,
project_id
FROM observations o FINAL
diff --git a/web/src/__tests__/server/repositories/trace-repository.servertest.ts b/web/src/__tests__/server/repositories/trace-repository.servertest.ts
index 47fbda58c7..5879225414 100644
--- a/web/src/__tests__/server/repositories/trace-repository.servertest.ts
+++ b/web/src/__tests__/server/repositories/trace-repository.servertest.ts
@@ -398,6 +398,75 @@ describe("Clickhouse Traces Repository Test", () => {
expect(exists).toBe(true);
});
+ it.each([
+ {
+ name: "latency",
+ column: "latency",
+ observationOverrides: {
+ start_time: Date.now() - 15_000,
+ end_time: Date.now(),
+ },
+ matchingValue: 10,
+ nonMatchingValue: 20,
+ },
+ {
+ name: "totalCost",
+ column: "totalCost",
+ observationOverrides: {
+ cost_details: { input: 5, output: 10, total: 15 },
+ },
+ matchingValue: 10,
+ nonMatchingValue: 20,
+ },
+ ])(
+ "should check if trace exists with $name filter",
+ async ({
+ column,
+ observationOverrides,
+ matchingValue,
+ nonMatchingValue,
+ }) => {
+ const traceId = v4();
+ const trace = createTrace({ id: traceId, project_id: projectId });
+
+ await createTracesCh([trace]);
+ await createObservationsCh([
+ createObservation({
+ trace_id: traceId,
+ project_id: projectId,
+ ...observationOverrides,
+ }),
+ ]);
+
+ const { exists: shouldExist } = await checkTraceExistsAndGetTimestamp({
+ projectId,
+ traceId,
+ timestamp: new Date(),
+ filter: [
+ { type: "number", column, operator: ">", value: matchingValue },
+ ],
+ maxTimeStamp: undefined,
+ });
+ expect(shouldExist).toBe(true);
+
+ const { exists: shouldNotExist } = await checkTraceExistsAndGetTimestamp({
+ projectId,
+ traceId,
+ timestamp: new Date(),
+ filter: [
+ {
+ type: "number",
+ column,
+ operator: ">",
+ value: nonMatchingValue,
+ },
+ ],
+ maxTimeStamp: undefined,
+ });
+ expect(shouldNotExist).toBe(false);
+ },
+ );
+
it("should handle timestamp filter in checkTraceExistsAndGetTimestamp", async () => {
const traceId = v4();
const trace = createTrace({