Skip to content

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

feat: add org audit log viewer (#11529)

Jannik Maierhöfer5ac1a184304e
diff --git a/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx b/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx
index 0f54b32706..23b286f1de 100644
--- a/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx
+++ b/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx
@@ -24,7 +24,7 @@ export function AuditLogsSettingsPage(props: { projectId: string }) {
</AlertDescription>
</Alert>
) : (
- <AuditLogsTable projectId={props.projectId} />
+ <AuditLogsTable scope="project" projectId={props.projectId} />
);
return (
diff --git a/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx b/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx
index 7e8c48df97..c305d3f052 100644
--- a/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx
+++ b/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx
@@ -17,21 +17,42 @@ import { SettingsTableCard } from "@/src/components/layouts/settings-table-card"
import { BatchExportTableButton } from "@/src/components/BatchExportTableButton";
import { BatchExportTableName } from "@langfuse/shared";
+// Both endpoints return the same shape
type AuditLogRow = RouterOutputs["auditLogs"]["all"]["data"][number];
-export function AuditLogsTable(props: { projectId: string }) {
+type AuditLogsTableProps =
+ | { scope: "project"; projectId: string }
+ | { scope: "organization"; orgId: string };
+
+export function AuditLogsTable(props: AuditLogsTableProps) {
const [paginationState, setPaginationState] = useQueryParams({
pageIndex: withDefault(NumberParam, 0),
pageSize: withDefault(NumberParam, 50),
});
- const auditLogs = api.auditLogs.all.useQuery({
- projectId: props.projectId,
- page: paginationState.pageIndex,
- limit: paginationState.pageSize,
- });
+ // Use the appropriate query based on scope
+ const projectAuditLogs = api.auditLogs.all.useQuery(
+ {
+ projectId: props.scope === "project" ? props.projectId : "",
+ page: paginationState.pageIndex,
+ limit: paginationState.pageSize,
+ },
+ { enabled: props.scope === "project" },
+ );
- const [rowHeight, setRowHeight] = useRowHeightLocalStorage("auditLogs", "s");
+ const orgAuditLogs = api.auditLogs.allByOrg.useQuery(
+ {
+ orgId: props.scope === "organization" ? props.orgId : "",
+ page: paginationState.pageIndex,
+ limit: paginationState.pageSize,
+ },
+ { enabled: props.scope === "organization" },
+ );
+
+ const auditLogs = props.scope === "project" ? projectAuditLogs : orgAuditLogs;
+
+ const tableId = props.scope === "project" ? "auditLogs" : "orgAuditLogs";
+ const [rowHeight, setRowHeight] = useRowHeightLocalStorage(tableId, "s");
const columns: LangfuseColumnDef<AuditLogRow>[] = [
{
@@ -50,7 +71,7 @@ export function AuditLogsTable(props: { projectId: string }) {
},
cell: (row) => {
const actor = row.getValue() as AuditLogRow["actor"];
- if (actor.type === "USER") {
+ if (actor?.type === "USER") {
const user = actor.body;
return (
<div className="flex items-center gap-2">
@@ -74,7 +95,7 @@ export function AuditLogsTable(props: { projectId: string }) {
);
}
- if (actor.type === "API_KEY") {
+ if (actor?.type === "API_KEY") {
const apiKey = actor.body;
return (
<div className="flex items-center gap-2">
@@ -126,20 +147,24 @@ export function AuditLogsTable(props: { projectId: string }) {
columns={columns}
rowHeight={rowHeight}
setRowHeight={setRowHeight}
- actionButtons={[
- <BatchExportTableButton
- key="audit-logs-export"
- projectId={props.projectId}
- tableName={BatchExportTableName.AuditLogs}
- filterState={[]}
- orderByState={{ column: "createdAt", order: "DESC" }}
- />,
- ]}
+ actionButtons={
+ props.scope === "project"
+ ? [
+ <BatchExportTableButton
+ key="audit-logs-export"
+ projectId={props.projectId}
+ tableName={BatchExportTableName.AuditLogs}
+ filterState={[]}
+ orderByState={{ column: "createdAt", order: "DESC" }}
+ />,
+ ]
+ : []
+ }
className="px-0"
/>
<SettingsTableCard>
<DataTable
- tableName={"auditLogs"}
+ tableName={tableId}
columns={columns}
data={
auditLogs.isPending
diff --git a/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx b/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx
new file mode 100644
index 0000000000..2ebe2846a5
--- /dev/null
+++ b/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx
@@ -0,0 +1,41 @@
+import Header from "@/src/components/layouts/header";
+import { Alert, AlertDescription, AlertTitle } from "@/src/components/ui/alert";
+import { AuditLogsTable } from "@/src/ee/features/audit-log-viewer/AuditLogsTable";
+import { useHasEntitlement } from "@/src/features/entitlements/hooks";
+import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
+
+export function OrgAuditLogsSettingsPage(props: { orgId: string }) {
+ const hasAccess = useHasOrganizationAccess({
+ organizationId: props.orgId,
+ scope: "auditLogs:read",
+ });
+ const hasEntitlement = useHasEntitlement("audit-logs");
+
+ const body = !hasEntitlement ? (
+ <p className="text-sm text-muted-foreground">
+ Audit logs are an Enterprise feature. Upgrade your plan to track all
+ changes made to your organization.
+ </p>
+ ) : !hasAccess ? (
+ <Alert>
+ <AlertTitle>Access Denied</AlertTitle>
+ <AlertDescription>
+ Contact your organization administrator to request access.
+ </AlertDescription>
+ </Alert>
+ ) : (
+ <AuditLogsTable scope="organization" orgId={props.orgId} />
+ );
+
+ return (
+ <>
+ <Header title="Organization Audit Logs" />
+ <p className="mb-2 text-sm text-muted-foreground">
+ Track who changed what in your organization and when. Monitor
+ organization settings, project creation/deletion, and membership changes
+ over time.
+ </p>
+ {body}
+ </>
+ );
+}
diff --git a/web/src/features/rbac/constants/organizationAccessRights.ts b/web/src/features/rbac/constants/organizationAccessRights.ts
index e6c95a8373..8dff526d4f 100644
--- a/web/src/features/rbac/constants/organizationAccessRights.ts
+++ b/web/src/features/rbac/constants/organizationAccessRights.ts
@@ -11,6 +11,7 @@ export const organizationScopes = [
"organizationMembers:read",
"organizationMembers:CUD",
"langfuseCloudBilling:CRUD",
+ "auditLogs:read",
] as const;
// type string of all Resource:Action, e.g. "organizationMembers:read"
@@ -26,6 +27,7 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
"organizationMembers:CUD",
"organizationMembers:read",
"langfuseCloudBilling:CRUD",
+ "auditLogs:read",
],
ADMIN: [
"projects:create",
@@ -34,6 +36,7 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
"organization:update",
"organizationMembers:CUD",
"organizationMembers:read",
+ "auditLogs:read",
],
MEMBER: ["organizationMembers:read"],
VIEWER: [],
diff --git a/web/src/pages/organization/[organizationId]/settings/index.tsx b/web/src/pages/organization/[organizationId]/settings/index.tsx
index 3b0fb2edfe..e32865cafc 100644
--- a/web/src/pages/organization/[organizationId]/settings/index.tsx
+++ b/web/src/pages/organization/[organizationId]/settings/index.tsx
@@ -18,6 +18,7 @@ import { ApiKeyList } from "@/src/features/public-api/components/ApiKeyList";
import AIFeatureSwitch from "@/src/features/organizations/components/AIFeatureSwitch";
import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
import { env } from "@/src/env.mjs";
+import { OrgAuditLogsSettingsPage } from "@/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage";
type OrganizationSettingsPage = {
title: string;
@@ -30,6 +31,7 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
const { organization } = useQueryProjectOrOrganization();
const showBillingSettings = useHasEntitlement("cloud-billing");
const showOrgApiKeySettings = useHasEntitlement("admin-api");
+ const showAuditLogs = useHasEntitlement("audit-logs");
const plan = usePlan();
const isLangfuseCloud = isCloudPlan(plan) ?? false;
const isCloudBillingAvailable = useIsCloudBillingAvailable();
@@ -40,6 +42,7 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
organization,
showBillingSettings: showBillingSettings && isCloudBillingAvailable,
showOrgApiKeySettings,
+ showAuditLogs,
isLangfuseCloud,
});
}
@@ -48,11 +51,13 @@ export const getOrganizationSettingsPages = ({
organization,
showBillingSettings,
showOrgApiKeySettings,
+ showAuditLogs,
isLangfuseCloud,
}: {
organization: { id: string; name: string; metadata: Record<string, unknown> };
showBillingSettings: boolean;
showOrgApiKeySettings: boolean;
+ showAuditLogs: boolean;
isLangfuseCloud: boolean;
}): OrganizationSettingsPage[] => [
{
@@ -116,6 +121,13 @@ export const getOrganizationSettingsPages = ({
</div>
),
},
+ {
+ title: "Audit Logs",
+ slug: "audit-logs",
+ cmdKKeywords: ["audit", "logs", "history", "changes"],
+ content: <OrgAuditLogsSettingsPage orgId={organization.id} />,
+ show: showAuditLogs,
+ },
{
title: "Billing",
slug: "billing",
diff --git a/web/src/server/api/routers/auditLogs.ts b/web/src/server/api/routers/auditLogs.ts
index a252b645b7..8918374024 100644
--- a/web/src/server/api/routers/auditLogs.ts
+++ b/web/src/server/api/routers/auditLogs.ts
@@ -1,9 +1,79 @@
import { z } from "zod/v4";
-import { createTRPCRouter, protectedProjectProcedure } from "../trpc";
+import {
+ createTRPCRouter,
+ protectedProjectProcedure,
+ protectedOrganizationProcedure,
+} from "../trpc";
import { throwIfNoProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { throwIfNoOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { throwIfNoEntitlement } from "@/src/features/entitlements/server/hasEntitlement";
import { paginationZod } from "@langfuse/shared";
-import { AuditLogRecordType } from "@langfuse/shared/src/db";
+import { AuditLogRecordType, type AuditLog } from "@langfuse/shared/src/db";
+
+type AuditLogActor =
+ | {
+ type: "API_KEY";
+ body: { id: string | null; publicKey: string | null };
+ }
+ | {
+ type: "USER";
+ body: {
+ id: string | null;
+ name: string | null;
+ email: string | null;
+ image: string | null;
+ };
+ }
+ | null;
+
+function mapAuditLogsWithActors(
+ auditLogs: AuditLog[],
+ userMap: Map<
+ string,
+ {
+ id: string;
+ name: string | null;
+ email: string | null;
+ image: string | null;
+ }
+ >,
+ apiKeyMap: Map<string, { id: string; publicKey: string }>,
+) {
+ return auditLogs.map((log) => {
+ let actor: AuditLogActor = null;
+ switch (log.type) {
+ case AuditLogRecordType.USER:
+ actor = {
+ type: log.type,
+ body: userMap.get(log.userId ?? "") ?? {
+ id: log.userId,
+ name: null,
+ email: null,
+ image: null,
+ },
+ };
+ break;
+ case AuditLogRecordType.API_KEY:
+ actor = {
+ type: log.type,
+ body: apiKeyMap.get(log.apiKeyId ?? "") ?? {
+ id: log.apiKeyId,
+ publicKey: null,
+ },
+ };
+ break;
+ default:
+ /* eslint-disable-next-line @typescript-eslint/no-unused-vars */
+ const exhaustiveCheckDefault: never = log.type;
+ throw new Error(`Type ${log.type} not found`);
+ }
+
+ return {
+ ...log,
+ actor,
+ };
+ });
+}
export const auditLogsRouter = createTRPCRouter({
all: protectedProjectProcedure
@@ -101,54 +171,106 @@ export const auditLogsRouter = createTRPCRouter({
const apiKeyMap = new Map(apiKeys.map((apiKey) => [apiKey.id, apiKey]));
return {
- data: auditLogs.map((log) => {
- let actor:
- | {
- type: "API_KEY";
- body: { id: string | null; publicKey: string | null };
- }
- | {
- type: "USER";
- body: {
- id: string | null;
- name: string | null;
- email: string | null;
- image: string | null;
- };
- }
- | null = null;
- switch (log.type) {
- case AuditLogRecordType.USER:
- actor = {
- type: log.type,
- body: userMap.get(log.userId ?? "") ?? {
- id: log.userId,
- name: null,
- email: null,
- image: null,
- },
- };
- break;
- case AuditLogRecordType.API_KEY:
- actor = {
- type: log.type,
- body: apiKeyMap.get(log.apiKeyId ?? "") ?? {
- id: log.apiKeyId,
- publicKey: null,
- },
- };
- break;
- default:
- /* eslint-disable-next-line @typescript-eslint/no-unused-vars */
- const exhaustiveCheckDefault: never = log.type;
- throw new Error(`Type ${log.type} not found`);
- }
-
- return {
- ...log,
- actor,
- };
+ data: mapAuditLogsWithActors(auditLogs, userMap, apiKeyMap),
+ totalCount,
+ };
+ }),
+
+ allByOrg: protectedOrganizationProcedure
+ .input(
+ z.object({
+ orgId: z.string(),
+ ...paginationZod,
+ }),
+ )
+ .query(async ({ ctx, input }) => {
+ // Check if user has access to audit logs feature at org level
+ throwIfNoEntitlement({
+ entitlement: "audit-logs",
+ sessionUser: ctx.session.user,
+ orgId: input.orgId,
+ });
+
+ // Check if user has access to organization audit logs
+ throwIfNoOrganizationAccess({
+ session: ctx.session,
+ organizationId: input.orgId,
+ scope: "auditLogs:read",
+ });
+
+ // Fetch organization-level audit logs (where projectId is null)
+ // This includes: organization CRUD, project CRUD, org membership changes
+ const [auditLogs, totalCount] = await Promise.all([
+ ctx.prisma.auditLog.findMany({
+ where: {
+ orgId: input.orgId,
+ projectId: null,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ skip: input.page * input.limit,
+ take: input.limit,
+ }),
+ ctx.prisma.auditLog.count({
+ where: {
+ orgId: input.orgId,
+ projectId: null,
+ },
}),
+ ]);
+
+ // Fetch user information for each audit log
+ const userIds = [
+ ...new Set(
+ auditLogs.flatMap((log) => (log?.userId ? [log.userId] : [])),
+ ),
+ ];
+ const apiKeyIds = [
+ ...new Set(
+ auditLogs.flatMap((log) => (log?.apiKeyId ? [log.apiKeyId] : [])),
+ ),
+ ];
+
+ const [users, apiKeys] = await Promise.all([
+ ctx.prisma.user.findMany({
+ where: {
+ id: {
+ in: userIds,
+ },
+ organizationMemberships: {
+ some: {
+ orgId: input.orgId,
+ },
+ },
+ },
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ image: true,
+ },
+ }),
+ ctx.prisma.apiKey.findMany({
+ where: {
+ id: {
+ in: apiKeyIds,
+ },
+ orgId: input.orgId,
+ scope: "ORGANIZATION",
+ },
+ select: {
+ id: true,
+ publicKey: true,
+ },
+ }),
+ ]);
+
+ const userMap = new Map(users.map((user) => [user.id, user]));
+ const apiKeyMap = new Map(apiKeys.map((apiKey) => [apiKey.id, apiKey]));
+
+ return {
+ data: mapAuditLogsWithActors(auditLogs, userMap, apiKeyMap),
totalCount,
};
}),