Skip to content

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

feat(ai-gateway): filter gateway analytics by user and API key (#19458)

Tobias Wochinger71ddcb9094f5
diff --git a/web/src/__tests__/server/queryBuilder.servertest.ts b/web/src/__tests__/server/queryBuilder.servertest.ts
index 755291139f..4468677788 100644
--- a/web/src/__tests__/server/queryBuilder.servertest.ts
+++ b/web/src/__tests__/server/queryBuilder.servertest.ts
@@ -4518,6 +4518,30 @@ describe("queryBuilder", () => {
]),
),
).toEqual({ "user-a": 2 });
+
+ const [keyFiltered] = await executeQuery(
+ projectId,
+ {
+ view: "observations",
+ dimensions: [],
+ metrics: [{ measure: "count", aggregation: "count" }],
+ filters: [
+ {
+ column: "aiGatewayKeyId",
+ operator: "any of",
+ value: ["key-a1", "service-key"],
+ type: "stringOptions",
+ },
+ ],
+ timeDimension: null,
+ fromTimestamp: new Date(Date.now() - 86400000).toISOString(),
+ toTimestamp: new Date(Date.now() + 86400000).toISOString(),
+ orderBy: null,
+ },
+ "v2",
+ true,
+ );
+ expect(Number(keyFiltered["count_count"])).toBe(2);
},
);
});
diff --git a/web/src/components/layouts/ContainerPage.tsx b/web/src/components/layouts/ContainerPage.tsx
index 9c5cd7b178..01f4131c0a 100644
--- a/web/src/components/layouts/ContainerPage.tsx
+++ b/web/src/components/layouts/ContainerPage.tsx
@@ -1,6 +1,7 @@
import PageHeader, {
type PageHeaderProps,
} from "@/src/components/layouts/page-header";
+import { PageHeaderControlsSlotProvider } from "@/src/components/layouts/page-header-controls-slot";
import { MobileTopBar } from "@/src/components/layouts/MobileTopBar";
import { MobilePageTitle } from "@/src/components/layouts/MobilePageTitle";
import { useIsMobile } from "@/src/hooks/use-mobile";
@@ -31,36 +32,38 @@ const ContainerPage = ({
const isMobile = useIsMobile();
return (
- <div
- className={cn(
- "relative flex flex-1 flex-col",
- fullHeight
- ? "h-screen-with-banner overflow-hidden"
- : "min-h-screen-with-banner",
- )}
- >
- <header className="sticky top-0 z-50 w-full">
- {isMobile ? (
- <MobileTopBar
- showSidebarTrigger={headerProps.showSidebarTrigger}
- leadingControl={headerProps.leadingControl}
- />
- ) : (
- <PageHeader {...headerProps} container />
- )}
- </header>
- {isMobile && <MobilePageTitle headerProps={headerProps} />}
- <main
+ <PageHeaderControlsSlotProvider>
+ <div
className={cn(
- "relative flex min-h-0 flex-1 flex-col p-3",
- extendRight
- ? rightExtendedContainerLayoutClassName
- : containerLayoutClassName,
+ "relative flex flex-1 flex-col",
+ fullHeight
+ ? "h-screen-with-banner overflow-hidden"
+ : "min-h-screen-with-banner",
)}
>
- {children}
- </main>
- </div>
+ <header className="sticky top-0 z-50 w-full">
+ {isMobile ? (
+ <MobileTopBar
+ showSidebarTrigger={headerProps.showSidebarTrigger}
+ leadingControl={headerProps.leadingControl}
+ />
+ ) : (
+ <PageHeader {...headerProps} container />
+ )}
+ </header>
+ {isMobile && <MobilePageTitle headerProps={headerProps} />}
+ <main
+ className={cn(
+ "relative flex min-h-0 flex-1 flex-col p-3",
+ extendRight
+ ? rightExtendedContainerLayoutClassName
+ : containerLayoutClassName,
+ )}
+ >
+ {children}
+ </main>
+ </div>
+ </PageHeaderControlsSlotProvider>
);
};
diff --git a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsDashboard/GatewayAnalyticsDashboard.tsx b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsDashboard/GatewayAnalyticsDashboard.tsx
index 9d272ffb58..20b3b9f89c 100644
--- a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsDashboard/GatewayAnalyticsDashboard.tsx
+++ b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsDashboard/GatewayAnalyticsDashboard.tsx
@@ -2,9 +2,12 @@ import { useMemo, useState } from "react";
import { TimeRangePicker } from "@/src/components/date-picker";
import Header from "@/src/components/layouts/header";
+import { PageHeaderControlsPortal } from "@/src/components/layouts/page-header-controls-slot";
import { GatewayAnalyticsWidgetCard } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsWidgetCard/GatewayAnalyticsWidgetCard";
import { GATEWAY_ANALYTICS_WIDGETS } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/constants/gatewayAnalyticsWidgets";
+import { useGatewayAnalyticsFilters } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/hooks/useGatewayAnalyticsFilters";
import { useGatewayAnalyticsLabels } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/hooks/useGatewayAnalyticsLabels";
+import { MultiSelect } from "@/src/features/filters";
import {
toAbsoluteTimeRange,
type TimeRange,
@@ -33,23 +36,44 @@ export function GatewayAnalyticsDashboard({
[timeRange],
);
const labels = useGatewayAnalyticsLabels(organizationId);
+ const { filters, user, apiKey } = useGatewayAnalyticsFilters({ labels });
return (
<div className="flex flex-col gap-4">
- <Header
- title="Gateway analytics"
- help={{
- description:
- "Cost and usage of requests made through the AI Gateway, read from its ingestion project.",
- }}
- actionButtons={
- <TimeRangePicker
- timeRange={timeRange}
- onTimeRangeChange={setTimeRange}
- timeRangePresets={TIME_RANGE_PRESETS}
+ <div className="flex flex-wrap items-center gap-3">
+ <Header
+ title="Gateway analytics"
+ className="mb-0!"
+ help={{
+ description:
+ "Cost and usage of requests made through the AI Gateway, read from its ingestion project.",
+ }}
+ />
+ {/* Option labels are member and API key names. */}
+ <div className="ph-no-capture flex flex-wrap items-center gap-2">
+ <MultiSelect
+ title="User"
+ label="User"
+ className="my-0 w-auto overflow-hidden"
+ {...user}
/>
- }
- />
+ <MultiSelect
+ title="API key"
+ label="API key"
+ className="my-0 w-auto overflow-hidden"
+ {...apiKey}
+ />
+ </div>
+ </div>
+ <PageHeaderControlsPortal>
+ <TimeRangePicker
+ timeRange={timeRange}
+ onTimeRangeChange={setTimeRange}
+ timeRangePresets={TIME_RANGE_PRESETS}
+ className="my-0 max-w-full overflow-x-auto"
+ triggerClassName="px-2"
+ />
+ </PageHeaderControlsPortal>
{absoluteTimeRange && (
<div className="grid grid-cols-1 gap-3 lg:grid-cols-6">
{GATEWAY_ANALYTICS_WIDGETS.map((widget) => (
@@ -58,6 +82,7 @@ export function GatewayAnalyticsDashboard({
projectId={projectId}
widget={widget}
timeRange={absoluteTimeRange}
+ filters={filters}
dimensionLabelMap={
widget.dimensionLabels && labels[widget.dimensionLabels]
}
diff --git a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsWidgetCard/GatewayAnalyticsWidgetCard.tsx b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsWidgetCard/GatewayAnalyticsWidgetCard.tsx
index 7e8b204aff..180ef3c17c 100644
--- a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsWidgetCard/GatewayAnalyticsWidgetCard.tsx
+++ b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/components/GatewayAnalyticsWidgetCard/GatewayAnalyticsWidgetCard.tsx
@@ -1,4 +1,5 @@
import { useMemo } from "react";
+import type { FilterState } from "@langfuse/shared";
import { buildGatewayAnalyticsQuery } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery";
import type { GatewayAnalyticsWidget } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/types/gatewayAnalyticsWidget";
@@ -27,16 +28,18 @@ export function GatewayAnalyticsWidgetCard({
projectId,
widget,
timeRange,
+ filters,
dimensionLabelMap,
}: {
projectId: string;
widget: GatewayAnalyticsWidget;
timeRange: { from: Date; to: Date };
+ filters: FilterState;
dimensionLabelMap?: ReadonlyMap<string, string>;
}) {
const query = useMemo(
- () => buildGatewayAnalyticsQuery(widget, timeRange),
- [widget, timeRange],
+ () => buildGatewayAnalyticsQuery(widget, timeRange, filters),
+ [widget, timeRange, filters],
);
return (
diff --git a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.clienttest.ts b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.clienttest.ts
index 12194f48a6..dc3d9683c9 100644
--- a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.clienttest.ts
+++ b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.clienttest.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
+import type { FilterState } from "@langfuse/shared";
import { query as querySchema, validateQuery } from "@langfuse/shared/query";
import { GATEWAY_ANALYTICS_WIDGETS } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/constants/gatewayAnalyticsWidgets";
@@ -9,6 +10,8 @@ const timeRange = {
to: new Date("2026-01-31T00:00:00.000Z"),
};
+const noFilters: FilterState = [];
+
const widget = (id: string) => {
const found = GATEWAY_ANALYTICS_WIDGETS.find((w) => w.id === id);
if (!found) throw new Error(`Unknown widget ${id}`);
@@ -19,7 +22,7 @@ describe("buildGatewayAnalyticsQuery", () => {
it.each(GATEWAY_ANALYTICS_WIDGETS.map((w) => [w.id, w] as const))(
"builds a valid v2 query scoped to gateway traffic for %s",
(_id, w) => {
- const query = buildGatewayAnalyticsQuery(w, timeRange);
+ const query = buildGatewayAnalyticsQuery(w, timeRange, noFilters);
expect(w.chartConfig.type).toBe(w.chartType);
expect(querySchema.safeParse(query).success).toBe(true);
@@ -37,6 +40,7 @@ describe("buildGatewayAnalyticsQuery", () => {
const query = buildGatewayAnalyticsQuery(
widget("cost-by-api-key"),
timeRange,
+ noFilters,
);
expect(query.dimensions).toEqual([{ field: "aiGatewayKeyId" }]);
@@ -51,9 +55,35 @@ describe("buildGatewayAnalyticsQuery", () => {
const query = buildGatewayAnalyticsQuery(
widget("cost-over-time"),
timeRange,
+ noFilters,
);
expect(query.timeDimension).toEqual({ granularity: "auto" });
expect(query.orderBy).toBeNull();
});
+
+ it("appends the header filters as valid v2 filters", () => {
+ const filters: FilterState = [
+ {
+ column: "aiGatewayKeyOwnerUserId",
+ operator: "any of",
+ value: ["user-a", "user-b"],
+ type: "stringOptions",
+ },
+ {
+ column: "aiGatewayKeyId",
+ operator: "any of",
+ value: ["key-1"],
+ type: "stringOptions",
+ },
+ ];
+ const query = buildGatewayAnalyticsQuery(
+ widget("cost-by-user"),
+ timeRange,
+ filters,
+ );
+
+ expect(validateQuery(query, "v2")).toEqual({ valid: true });
+ expect(query.filters).toEqual(expect.arrayContaining(filters));
+ });
});
diff --git a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.ts b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.ts
index 43e476df99..2dd9327a67 100644
--- a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.ts
+++ b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/fns/buildGatewayAnalyticsQuery.ts
@@ -1,4 +1,4 @@
-import type { FilterCondition } from "@langfuse/shared";
+import type { FilterCondition, FilterState } from "@langfuse/shared";
import {
buildWidgetOrderBy,
isV2BreakdownChart,
@@ -25,6 +25,7 @@ const DEFAULT_BREAKDOWN_ROW_LIMIT = 100;
export function buildGatewayAnalyticsQuery(
widget: GatewayAnalyticsWidget,
timeRange: { from: Date; to: Date },
+ filters: FilterState,
): QueryType {
const chartType = widget.chartType;
const isTimeSeries = isTimeSeriesChart(chartType);
@@ -43,7 +44,7 @@ export function buildGatewayAnalyticsQuery(
view: widget.view,
dimensions: widget.dimensions,
metrics,
- filters: [GATEWAY_TRAFFIC_FILTER, ...widget.filters],
+ filters: [GATEWAY_TRAFFIC_FILTER, ...widget.filters, ...filters],
timeDimension: isTimeSeries ? { granularity: "auto" } : null,
fromTimestamp: timeRange.from.toISOString(),
toTimestamp: timeRange.to.toISOString(),
diff --git a/web/src/features/ai-gateway/components/GatewayAnalyticsPage/hooks/useGatewayAnalyticsFilters.ts b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/hooks/useGatewayAnalyticsFilters.ts
new file mode 100644
index 0000000000..2e63a278f8
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayAnalyticsPage/hooks/useGatewayAnalyticsFilters.ts
@@ -0,0 +1,63 @@
+import { useMemo, useState } from "react";
+import type { FilterState } from "@langfuse/shared";
+
+import type { GatewayAnalyticsLabelSource } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/types/gatewayAnalyticsWidget";
+import { useDebounce } from "@/src/hooks/useDebounce";
+
+/**
+ * User and API key selections. Values are IDs; options show member and API
+ * key names. Deleted keys and former members have no option, so an ID can
+ * also be typed in.
+ */
+export function useGatewayAnalyticsFilters({
+ labels,
+}: {
+ labels: Record<GatewayAnalyticsLabelSource, ReadonlyMap<string, string>>;
+}) {
+ const [userIds, setUserIds] = useState<string[]>([]);
+ const [apiKeyIds, setApiKeyIds] = useState<string[]>([]);
+ const setUserIdsDebounced = useDebounce(setUserIds);
+ const setApiKeyIdsDebounced = useDebounce(setApiKeyIds);
+
+ const filters = useMemo(
+ () => [
+ ...anyOf("aiGatewayKeyOwnerUserId", userIds),
+ ...anyOf("aiGatewayKeyId", apiKeyIds),
+ ],
+ [userIds, apiKeyIds],
+ );
+
+ const userOptions = useMemo(() => toOptions(labels.users), [labels.users]);
+ const apiKeyOptions = useMemo(
+ () => toOptions(labels.apiKeys),
+ [labels.apiKeys],
+ );
+
+ return {
+ filters,
+ user: {
+ values: userIds,
+ options: userOptions,
+ onValueChange: setUserIdsDebounced,
+ isCustomSelectEnabled: true,
+ },
+ apiKey: {
+ values: apiKeyIds,
+ options: apiKeyOptions,
+ onValueChange: setApiKeyIdsDebounced,
+ isCustomSelectEnabled: true,
+ },
+ };
+}
+
+function toOptions(labelMap: ReadonlyMap<string, string>) {
+ return [...labelMap].map(([value, displayValue]) => ({
+ value,
+ displayValue,
+ }));
+}
+
+function anyOf(column: string, values: string[]): FilterState {
+ if (values.length === 0) return [];
+ return [{ column, operator: "any of", value: values, type: "stringOptions" }];
+}