Skip to content

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

chore: allow trpc events-filterOptions to specify columns list (#14557)

Valery Meleshkin2a372e7fd796
diff --git a/web/src/__tests__/server/repositories/event-repository.servertest.ts b/web/src/__tests__/server/repositories/event-repository.servertest.ts
index 730405a51b..e3b135a298 100644
--- a/web/src/__tests__/server/repositories/event-repository.servertest.ts
+++ b/web/src/__tests__/server/repositories/event-repository.servertest.ts
@@ -14,6 +14,8 @@ import {
getTracesIdentifierForSessionFromEvents,
getEventsFilterOptionsForColumns,
getEventsFilterOptionValuesPage,
+ createScoresCh,
+ createTraceScore,
type EventFilterOptionColumn,
} from "@langfuse/shared/src/server";
import {
@@ -614,6 +616,65 @@ describe("Clickhouse Events Repository Test", () => {
});
});
+ it("only loads requested filter option columns", async () => {
+ const uniqueProjectId = randomUUID();
+ const traceId = randomUUID();
+ const now = Date.now();
+
+ await createEventsCh([
+ createEvent({
+ id: randomUUID(),
+ span_id: randomUUID(),
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ type: "SPAN",
+ name: "unrequested-filter-option-name",
+ level: "WARNING",
+ tags: ["unrequested-filter-option-tag"],
+ start_time: now * 1000,
+ }),
+ ]);
+ await createScoresCh([
+ createTraceScore({
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ name: "unrequested-filter-option-score",
+ data_type: "NUMERIC",
+ timestamp: now,
+ event_ts: now,
+ created_at: now,
+ updated_at: now,
+ }),
+ createTraceScore({
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ name: "unrequested-filter-option-category",
+ data_type: "CATEGORICAL",
+ string_value: "unrequested-category-value",
+ value: 1,
+ timestamp: now,
+ event_ts: now,
+ created_at: now,
+ updated_at: now,
+ }),
+ ]);
+
+ await waitForExpect(async () => {
+ const options = await getEventFilterOptions({
+ projectId: uniqueProjectId,
+ columns: ["level"],
+ });
+
+ expect(options.level.map((level) => level.value)).toContain("WARNING");
+ expect(options.name).toEqual([]);
+ expect(options.traceTags).toEqual([]);
+ expect(options.scores_avg).toEqual([]);
+ expect(options.score_categories).toEqual([]);
+ expect(options.trace_scores_avg).toEqual([]);
+ expect(options.trace_score_categories).toEqual([]);
+ });
+ });
+
it("applies the default startTime bound to paged and numeric filter values", async () => {
const uniqueProjectId = randomUUID();
const traceId = randomUUID();
diff --git a/web/src/features/events/hooks/useEventsFilterOptions.ts b/web/src/features/events/hooks/useEventsFilterOptions.ts
index 1c095d815a..a888ca0525 100644
--- a/web/src/features/events/hooks/useEventsFilterOptions.ts
+++ b/web/src/features/events/hooks/useEventsFilterOptions.ts
@@ -1,17 +1,22 @@
-import { api } from "@/src/utils/api";
+import { api, type RouterInputs } from "@/src/utils/api";
import { useMemo } from "react";
import { type FilterState, type TimeFilter } from "@langfuse/shared";
+type EventFilterOptionColumnsInput =
+ RouterInputs["events"]["filterOptions"]["columns"];
+
type UseEventsFilterOptionsParams = {
projectId: string;
oldFilterState: FilterState;
isRootObservation?: boolean;
+ columns?: EventFilterOptionColumnsInput;
};
export function useEventsFilterOptions({
projectId,
oldFilterState,
isRootObservation,
+ columns,
}: UseEventsFilterOptionsParams) {
// Extract start time filters for filter options query
const startTimeFilters = useMemo(() => {
@@ -29,6 +34,7 @@ export function useEventsFilterOptions({
startTimeFilter:
startTimeFilters.length > 0 ? startTimeFilters : undefined,
isRootObservation,
+ columns,
},
{
trpc: {
diff --git a/web/src/features/events/server/eventsRouter.ts b/web/src/features/events/server/eventsRouter.ts
index e6d86874ce..e1f6135df6 100644
--- a/web/src/features/events/server/eventsRouter.ts
+++ b/web/src/features/events/server/eventsRouter.ts
@@ -22,6 +22,7 @@ import {
getEventCount,
getEventFilterOptions,
getEventBatchIO,
+ EVENT_FILTER_OPTIONS_COLUMNS,
} from "./eventsService";
import {
instrumentAsync,
@@ -56,6 +57,12 @@ export type GetAllEventsInput = z.infer<typeof GetAllEventsInput>;
const GetEventFilterOptionsInput = zodSchema.object({
projectId: zodSchema.string(),
startTimeFilter: zodSchema.array(timeFilter).optional(),
+ monitorWindow: MonitorWindowSchema.optional(),
+ isRootObservation: zodSchema.boolean().optional(),
+ hasParentObservation: zodSchema.boolean().optional(),
+ columns: zodSchema
+ .array(zodSchema.enum(EVENT_FILTER_OPTIONS_COLUMNS))
+ .optional(),
});
export type GetEventFilterOptionsInput = z.infer<
@@ -150,15 +157,7 @@ export const eventsRouter = createTRPCRouter({
);
}),
filterOptions: protectedProjectProcedure
- .input(
- zodSchema.object({
- projectId: zodSchema.string(),
- startTimeFilter: zodSchema.array(timeFilter).optional(),
- monitorWindow: MonitorWindowSchema.optional(),
- isRootObservation: zodSchema.boolean().optional(),
- hasParentObservation: zodSchema.boolean().optional(),
- }),
- )
+ .input(GetEventFilterOptionsInput)
.query(async ({ input }) => {
return instrumentAsync(
{
@@ -176,6 +175,7 @@ export const eventsRouter = createTRPCRouter({
(input.hasParentObservation !== undefined
? !input.hasParentObservation
: undefined), // backward compat for legacy hasParentObservation filterOption
+ columns: input.columns,
});
},
);
diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.ts
index 3acfa670a3..e2dfdf2e24 100644
--- a/web/src/features/events/server/eventsService.ts
+++ b/web/src/features/events/server/eventsService.ts
@@ -71,6 +71,7 @@ interface GetObservationsFilterOptionsParams {
isRootObservation?: boolean;
hasParentObservation?: boolean;
observationType?: string;
+ columns?: readonly EventFilterOptionsColumn[];
}
type EventFilterValueOption = {
@@ -100,6 +101,20 @@ const EVENT_FILTER_OPTION_COLUMNS = [
"calledToolNames",
] as const satisfies readonly EventFilterOptionColumn[];
+const EVENT_SCORE_FILTER_OPTION_COLUMNS = [
+ "scores_avg",
+ "score_categories",
+ "trace_scores_avg",
+ "trace_score_categories",
+] as const;
+
+export const EVENT_FILTER_OPTIONS_COLUMNS = [
+ ...EVENT_FILTER_OPTION_COLUMNS,
+ ...EVENT_SCORE_FILTER_OPTION_COLUMNS,
+] as const;
+
+type EventFilterOptionsColumn = (typeof EVENT_FILTER_OPTIONS_COLUMNS)[number];
+
const OBSERVATIONS_TO_TRACE_INTERVAL_MS = 2 * 24 * 60 * 60 * 1000;
const SCORE_TO_TRACE_OBSERVATIONS_INTERVAL_MS = 60 * 60 * 1000;
const EVENT_FILTER_OPTIONS_SCORE_LOOKBACK_BUFFER_MS =
@@ -527,9 +542,20 @@ export async function getEventFilterOptions(
params: GetObservationsFilterOptionsParams,
) {
const scopedParams = ensureStartTimeFilterForEventFilterOptions(params);
- const { projectId } = scopedParams;
+ const { projectId, columns = EVENT_FILTER_OPTIONS_COLUMNS } = scopedParams;
const { eventsFilter, traceTimestampFilters, traceScoreTimestampFilters } =
getEventFilterOptionsScope(scopedParams);
+ const requestedColumns = new Set<EventFilterOptionsColumn>(columns);
+ const eventColumns = EVENT_FILTER_OPTION_COLUMNS.filter((column) =>
+ requestedColumns.has(column),
+ );
+ const shouldLoadScoresAvg = requestedColumns.has("scores_avg");
+ const shouldLoadScoreCategories =
+ requestedColumns.has("score_categories") ||
+ requestedColumns.has("trace_score_categories");
+ const shouldLoadTraceScores =
+ requestedColumns.has("trace_scores_avg") ||
+ requestedColumns.has("trace_score_categories");
const [
numericScoreNames,
@@ -537,17 +563,25 @@ export async function getEventFilterOptions(
traceScoreColumns,
eventFilterOptions,
] = await Promise.all([
- getNumericScoresGroupedByName(projectId, traceTimestampFilters),
- getCategoricalScoresGroupedByName(projectId, traceTimestampFilters),
- getScoresGroupedByNameSourceType({
- projectId,
- filter: [...TRACE_SCORE_SCOPE_FILTER, ...traceScoreTimestampFilters],
- }),
- getEventsFilterOptionsForColumns({
- projectId,
- filter: eventsFilter,
- columns: EVENT_FILTER_OPTION_COLUMNS,
- }),
+ shouldLoadScoresAvg
+ ? getNumericScoresGroupedByName(projectId, traceTimestampFilters)
+ : Promise.resolve([]),
+ shouldLoadScoreCategories
+ ? getCategoricalScoresGroupedByName(projectId, traceTimestampFilters)
+ : Promise.resolve([]),
+ shouldLoadTraceScores
+ ? getScoresGroupedByNameSourceType({
+ projectId,
+ filter: [...TRACE_SCORE_SCOPE_FILTER, ...traceScoreTimestampFilters],
+ })
+ : Promise.resolve([]),
+ eventColumns.length > 0
+ ? getEventsFilterOptionsForColumns({
+ projectId,
+ filter: eventsFilter,
+ columns: eventColumns,
+ })
+ : Promise.resolve([]),
]);
const traceNumericScoreNames = Array.from(
new Set(
@@ -569,12 +603,20 @@ export async function getEventFilterOptions(
return {
...eventFilterOptionsByColumn,
- scores_avg: numericScoreNames.map((score) => score.name),
- score_categories: categoricalScoreNames,
- trace_scores_avg: traceNumericScoreNames,
- trace_score_categories: categoricalScoreNames.filter((score) =>
- traceCategoricalScoreNames.has(score.label),
- ),
+ scores_avg: shouldLoadScoresAvg
+ ? numericScoreNames.map((score) => score.name)
+ : [],
+ score_categories: requestedColumns.has("score_categories")
+ ? categoricalScoreNames
+ : [],
+ trace_scores_avg: requestedColumns.has("trace_scores_avg")
+ ? traceNumericScoreNames
+ : [],
+ trace_score_categories: requestedColumns.has("trace_score_categories")
+ ? categoricalScoreNames.filter((score) =>
+ traceCategoricalScoreNames.has(score.label),
+ )
+ : [],
};
}