Skip to content

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

feat(tracing): filter for ingested api key (#15515)

Nimar4deb6bc20aed
diff --git a/packages/shared/src/eventsTable.ts b/packages/shared/src/eventsTable.ts
index b324ac5201..3a435cea59 100644
--- a/packages/shared/src/eventsTable.ts
+++ b/packages/shared/src/eventsTable.ts
@@ -66,6 +66,13 @@ const eventsTableColsDefinition = [
options: [], // to be added at runtime
nullable: true,
},
+ {
+ name: "API Key",
+ id: "ingestionApiKey",
+ type: "stringOptions",
+ internal: "e.ingestion_api_key",
+ options: [], // to be added at runtime
+ },
{
name: "Version",
id: "version",
diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts b/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts
index 999a930a65..f32d3193ff 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/event-filter-options.ts
@@ -97,6 +97,12 @@ const EVENTS_FILTER_OPTION_DEFINITIONS = {
includeWhen: "e.environment IS NOT NULL AND length(e.environment) > 0",
sort: "countDesc",
},
+ ingestionApiKey: {
+ kind: "scalar",
+ expression: "e.ingestion_api_key",
+ includeWhen: "length(e.ingestion_api_key) > 0",
+ sort: "countDesc",
+ },
promptName: {
kind: "scalar",
expression: "e.prompt_name",
diff --git a/packages/shared/src/server/tableMappings/mapEventsTable.ts b/packages/shared/src/server/tableMappings/mapEventsTable.ts
index 13698b90a0..54c9c3228f 100644
--- a/packages/shared/src/server/tableMappings/mapEventsTable.ts
+++ b/packages/shared/src/server/tableMappings/mapEventsTable.ts
@@ -16,6 +16,12 @@ export const eventsTableNativeUiColumnDefinitions: UiColumnMappings = [
clickhouseTableName: "events_proto",
clickhouseSelect: 'e."environment"',
},
+ {
+ uiTableName: "API Key",
+ uiTableId: "ingestionApiKey",
+ clickhouseTableName: "events_proto",
+ clickhouseSelect: 'e."ingestion_api_key"',
+ },
{
uiTableName: "Type",
uiTableId: "type",
diff --git a/web/src/__tests__/server/event-query-builder.servertest.ts b/web/src/__tests__/server/event-query-builder.servertest.ts
index 5c47953115..6bdd807bbb 100644
--- a/web/src/__tests__/server/event-query-builder.servertest.ts
+++ b/web/src/__tests__/server/event-query-builder.servertest.ts
@@ -2,10 +2,13 @@ import {
buildEventsFilterOptionColumnQuery,
buildEventsFilterOptionsForColumnsQuery,
CTEQueryBuilder,
+ createFilterFromFilterState,
EventsAggregationQueryBuilder,
EventsQueryBuilder,
+ eventsTableUiColumnDefinitions,
ExperimentsAggregationQueryBuilder,
} from "@langfuse/shared/src/server";
+import { eventsTableCols } from "@langfuse/shared";
describe("buildEventsFilterOptionsForColumnsQuery", () => {
it("builds one events_core scan for multiple filter option columns", () => {
@@ -188,6 +191,44 @@ describe("buildEventsFilterOptionsForColumnsQuery", () => {
});
});
+ it("builds API key filter options from ingestion attribution", () => {
+ const built = buildEventsFilterOptionColumnQuery({
+ projectId: "test-project",
+ filter: [],
+ column: "ingestionApiKey",
+ limit: 10,
+ });
+
+ expect(built).not.toBeNull();
+ if (!built) throw new Error("expected query");
+
+ expect(built.query).toContain("'ingestionApiKey' AS column");
+ expect(built.query).toContain("toString(e.ingestion_api_key) AS value");
+ expect(built.query).toContain("length(e.ingestion_api_key) > 0");
+ expect(built.query).not.toContain("FINAL");
+ });
+
+ it("maps API key filters to the ingestion attribution column", () => {
+ const [filter] = createFilterFromFilterState(
+ [
+ {
+ column: "ingestionApiKey",
+ type: "stringOptions",
+ operator: "any of",
+ value: ["pk-lf-test"],
+ },
+ ],
+ eventsTableUiColumnDefinitions,
+ eventsTableCols,
+ );
+
+ expect(filter).toBeDefined();
+ if (!filter) throw new Error("expected filter");
+ const applied = filter.apply();
+ expect(applied.query).toContain('e."ingestion_api_key" IN');
+ expect(Object.values(applied.params)).toContainEqual(["pk-lf-test"]);
+ });
+
it("builds a direct grouped query for one boolean filter option column", () => {
const built = buildEventsFilterOptionColumnQuery({
projectId: "test-project",
diff --git a/web/src/features/events/config/filter-config.ts b/web/src/features/events/config/filter-config.ts
index d20a1b56be..476c143f02 100644
--- a/web/src/features/events/config/filter-config.ts
+++ b/web/src/features/events/config/filter-config.ts
@@ -96,6 +96,11 @@ export const observationEventsFilterConfig: FilterConfig = {
column: "environment",
label: getEventsColumnName("environment"),
},
+ {
+ type: "categorical" as const,
+ column: "ingestionApiKey",
+ label: getEventsColumnName("ingestionApiKey"),
+ },
{
type: "categorical" as const,
column: "type",
diff --git a/web/src/features/events/hooks/useEventsFilterOptions.ts b/web/src/features/events/hooks/useEventsFilterOptions.ts
index 0c0e376007..ce75a90bbf 100644
--- a/web/src/features/events/hooks/useEventsFilterOptions.ts
+++ b/web/src/features/events/hooks/useEventsFilterOptions.ts
@@ -37,6 +37,7 @@ const ALL_EVENT_FILTER_OPTION_COLUMNS = [
"sessionId",
"level",
"environment",
+ "ingestionApiKey",
"experimentDatasetId",
"experimentId",
"experimentName",
@@ -299,6 +300,7 @@ export function useEventsFilterOptions({
return {
environment: rawData.environment ?? undefined,
+ ingestionApiKey: rawData.ingestionApiKey ?? undefined,
name: rawData.name ?? undefined,
type: rawData.type ?? undefined,
level: rawData.level ?? undefined,
diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.ts
index 1cf9065617..6a91438b32 100644
--- a/web/src/features/events/server/eventsService.ts
+++ b/web/src/features/events/server/eventsService.ts
@@ -127,6 +127,7 @@ const EVENT_FILTER_OPTION_COLUMNS = [
"sessionId",
"level",
"environment",
+ "ingestionApiKey",
"experimentDatasetId",
"experimentId",
"experimentName",
diff --git a/web/src/features/search-bar/lib/fields.ts b/web/src/features/search-bar/lib/fields.ts
index e7ce8044df..5da007a223 100644
--- a/web/src/features/search-bar/lib/fields.ts
+++ b/web/src/features/search-bar/lib/fields.ts
@@ -51,6 +51,7 @@ export const FIELDS: FieldDef[] = [
{ id: "traceName", aliases: ["tracename", "trace_name"], kind: "text", syncMode: "exactOption", description: "Trace name", nullable: true },
{ id: "type", aliases: [], kind: "text", syncMode: "exactOption", description: "Observation type" },
{ id: "environment", aliases: ["env"], kind: "text", syncMode: "exactOption", description: "Environment", nullable: true },
+ { id: "ingestionApiKey", aliases: ["ingestionapikey", "ingestion_api_key", "apikey", "api_key", "publickey", "public_key"], kind: "text", syncMode: "exactOption", description: "Public ingestion API key" },
{ id: "userId", aliases: ["userid", "user_id", "user"], kind: "text", syncMode: "exactOption", description: "Trace user id", nullable: true },
{ id: "sessionId", aliases: ["sessionid", "session_id", "session"], kind: "text", syncMode: "exactOption", description: "Trace session id", nullable: true },
{ id: "level", aliases: [], kind: "text", syncMode: "exactOption", description: "Observation level" },
diff --git a/web/src/features/search-bar/lib/langQ.clienttest.ts b/web/src/features/search-bar/lib/langQ.clienttest.ts
index 9eae313057..9dfa5e3918 100644
--- a/web/src/features/search-bar/lib/langQ.clienttest.ts
+++ b/web/src/features/search-bar/lib/langQ.clienttest.ts
@@ -30,13 +30,24 @@ function strip(node: ASTNode | null): unknown {
describe("langQ parser", () => {
it("resolves aliases to canonical field ids", () => {
- const r = parse("env:prod");
+ const r = parse("env:prod apiKey:pk-lf-test");
expect(r.valid).toBe(true);
expect(strip(r.ast)).toEqual({
- kind: "filter",
- key: "environment",
- op: "=",
- values: ["prod"],
+ kind: "and",
+ children: [
+ {
+ kind: "filter",
+ key: "environment",
+ op: "=",
+ values: ["prod"],
+ },
+ {
+ kind: "filter",
+ key: "ingestionApiKey",
+ op: "=",
+ values: ["pk-lf-test"],
+ },
+ ],
});
});