Skip to content

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

fix: update ordering for get traces calls to avoid stale results (#5090)

Steffen Schmitza12f3f675eaf
diff --git a/packages/shared/src/server/services/traces-ui-table-service.ts b/packages/shared/src/server/services/traces-ui-table-service.ts
index b200f812ed..dece3f0c4c 100644
--- a/packages/shared/src/server/services/traces-ui-table-service.ts
+++ b/packages/shared/src/server/services/traces-ui-table-service.ts
@@ -322,19 +322,28 @@ const getTracesTableGeneric = async <T>(props: FetchTracesTableProps) => {
clickhouseSelect: "toDate(t.timestamp)",
uiTableName: "timestamp_to_date",
uiTableId: "timestamp_to_date",
- clickhouseTableName: "observations",
+ clickhouseTableName: "traces",
+ },
+ {
+ clickhouseSelect: "t.event_ts",
+ uiTableName: "event_ts",
+ uiTableId: "event_ts",
+ clickhouseTableName: "traces",
},
];
const chOrderBy = orderByToClickhouseSql(
[
defaultOrder
- ? {
- column: "timestamp_to_date",
- order: orderBy.order,
- }
+ ? [
+ {
+ column: "timestamp_to_date",
+ order: orderBy.order,
+ },
+ { column: "event_ts", order: "DESC" as "DESC" },
+ ]
: null,
orderBy ?? null,
- ],
+ ].flat(),
orderByCols,
);
@@ -342,7 +351,7 @@ const getTracesTableGeneric = async <T>(props: FetchTracesTableProps) => {
// - we only join scores and observations if we really need them to speed up default views
// - we use FINAL on traces only in case we not need to order by something different than time. Otherwise we cannot guarantee correct reads.
// - we filter the observations and scores as much as possible before joining them to traces.
- // - we order by todate(timestamp), timestamp per default and do not use FINAL.
+ // - we order by todate(timestamp), event_ts desc per default and do not use FINAL.
// In this case, CH is able to read the data only from the latest date from disk and filtering them in memory. No need to read all data e.g. for 1 month from disk.
const query = `
diff --git a/web/src/__tests__/async/traces-api.servertest.ts b/web/src/__tests__/async/traces-api.servertest.ts
index ec53a4e093..4ede056779 100644
--- a/web/src/__tests__/async/traces-api.servertest.ts
+++ b/web/src/__tests__/async/traces-api.servertest.ts
@@ -261,6 +261,7 @@ describe("/api/public/traces API Endpoint", () => {
const trace2 = traces.body.data[1];
expect(trace2.name).toBe("trace-name1");
});
+
it("should return 400 error when page=0", async () => {
const response = await makeZodVerifiedAPICallSilent(
GetTracesV1Response,
diff --git a/web/src/features/public-api/server/traces.ts b/web/src/features/public-api/server/traces.ts
index ec60aeb24b..a70f97c50a 100644
--- a/web/src/features/public-api/server/traces.ts
+++ b/web/src/features/public-api/server/traces.ts
@@ -48,9 +48,13 @@ export const generateTracesForPublicApi = async (
(f.operator === ">=" || f.operator === ">"),
) as DateTimeFilter | undefined;
- const chOrderBy = orderBy
- ? orderByToClickhouseSql(orderBy, orderByColumns)
- : "ORDER BY t.timestamp desc";
+ // If user provides an order we prefer it or fallback to timestamp as the default.
+ // In both cases we append a t.event_ts desc order to pick the latest event in case of duplicates.
+ // This may still return stale information if the orderBy key was updated between traces or if a filter
+ // applies only to a stale value.
+ const chOrderBy =
+ (orderByToClickhouseSql(orderBy || [], orderByColumns) ||
+ "ORDER BY t.timestamp desc") + ", t.event_ts desc";
const query = `
WITH observation_stats AS (
@@ -68,8 +72,8 @@ export const generateTracesForPublicApi = async (
SELECT
trace_id,
project_id,
- groupArray(id) as score_ids
- FROM scores FINAL
+ groupUniqArray(id) as score_ids
+ FROM scores
WHERE project_id = {projectId: String}
${timeFilter ? `AND timestamp >= {cteTimeFilter: DateTime64(3)}` : ""}
GROUP BY project_id, trace_id
@@ -99,8 +103,8 @@ export const generateTracesForPublicApi = async (
COALESCE(o.latency_milliseconds / 1000, 0) as latency,
COALESCE(o.total_cost, 0) as totalCost
FROM traces t
- LEFT JOIN score_stats s ON t.id = s.trace_id AND t.project_id = s.project_id
LEFT JOIN observation_stats o ON t.id = o.trace_id AND t.project_id = o.project_id
+ LEFT JOIN score_stats s ON t.id = s.trace_id AND t.project_id = s.project_id
WHERE t.project_id = {projectId: String}
${filter.length() > 0 ? `AND ${appliedFilter.query}` : ""}
${chOrderBy}