Skip to content

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

feat: add export functionality to audit log table (#7202)

Thorsten Spiekerc7aa992586af
diff --git a/packages/shared/src/interfaces/tableNames.ts b/packages/shared/src/interfaces/tableNames.ts
index 62076fc1e8..f2814f124d 100644
--- a/packages/shared/src/interfaces/tableNames.ts
+++ b/packages/shared/src/interfaces/tableNames.ts
@@ -9,4 +9,5 @@ export enum BatchTableNames {
Traces = "traces",
Observations = "observations",
DatasetRunItems = "dataset_run_items",
+ AuditLogs = "audit_logs",
}
diff --git a/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx b/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx
index c85a7e6af6..39132950a6 100644
--- a/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx
+++ b/web/src/ee/features/audit-log-viewer/AuditLogsTable.tsx
@@ -13,6 +13,8 @@ import { useRowHeightLocalStorage } from "@/src/components/table/data-table-row-
import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
import { type RouterOutputs } from "@/src/utils/api";
import { SettingsTableCard } from "@/src/components/layouts/settings-table-card";
+import { BatchExportTableButton } from "@/src/components/BatchExportTableButton";
+import { BatchExportTableName } from "@langfuse/shared";
type AuditLogRow = RouterOutputs["auditLogs"]["all"]["data"][number];
@@ -123,6 +125,15 @@ 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" }}
+ />,
+ ]}
className="px-0"
/>
<SettingsTableCard>
diff --git a/worker/src/__tests__/batchExport.test.ts b/worker/src/__tests__/batchExport.test.ts
index 2f3ac91118..41404a9814 100644
--- a/worker/src/__tests__/batchExport.test.ts
+++ b/worker/src/__tests__/batchExport.test.ts
@@ -911,4 +911,109 @@ describe("batch export test suite", () => {
expect(rows[0].value).toBe(0.9);
expect(rows[0].traceId).toBe(traces[1].id);
});
+
+ it("should export audit logs", async () => {
+ const { projectId, orgId } = await createOrgProjectAndApiKey();
+
+ // Create some audit log entries directly in the database
+ const auditLogEntries = [
+ {
+ id: randomUUID(),
+ projectId: projectId,
+ orgId: orgId,
+ type: "USER" as const,
+ userId: randomUUID(),
+ userOrgRole: "OWNER",
+ userProjectRole: "ADMIN",
+ resourceType: "trace",
+ resourceId: randomUUID(),
+ action: "CREATE",
+ before: null,
+ after: JSON.stringify({ name: "test-trace", version: 1 }),
+ createdAt: new Date("2024-01-01T10:00:00Z"),
+ updatedAt: new Date("2024-01-01T10:00:00Z"),
+ },
+ {
+ id: randomUUID(),
+ projectId: projectId,
+ orgId: orgId,
+ type: "API_KEY" as const,
+ apiKeyId: randomUUID(),
+ resourceType: "score",
+ resourceId: randomUUID(),
+ action: "UPDATE",
+ before: JSON.stringify({ value: 0.5 }),
+ after: JSON.stringify({ value: 0.8 }),
+ createdAt: new Date("2024-01-02T10:00:00Z"),
+ updatedAt: new Date("2024-01-02T10:00:00Z"),
+ },
+ {
+ id: randomUUID(),
+ projectId: projectId,
+ orgId: orgId,
+ type: "USER" as const,
+ userId: randomUUID(),
+ userOrgRole: "MEMBER",
+ userProjectRole: "VIEWER",
+ resourceType: "prompt",
+ resourceId: randomUUID(),
+ action: "DELETE",
+ before: JSON.stringify({ name: "old-prompt", content: "Hello World" }),
+ after: null,
+ createdAt: new Date("2024-01-03T10:00:00Z"),
+ updatedAt: new Date("2024-01-03T10:00:00Z"),
+ },
+ ];
+
+ // Insert audit log entries
+ await prisma.auditLog.createMany({
+ data: auditLogEntries,
+ });
+
+ // Export audit logs
+ const stream = await getDatabaseReadStream({
+ projectId: projectId,
+ tableName: BatchExportTableName.AuditLogs,
+ cutoffCreatedAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
+ filter: [],
+ orderBy: { column: "createdAt", order: "DESC" },
+ });
+
+ const rows: any[] = [];
+
+ for await (const chunk of stream) {
+ rows.push(chunk);
+ }
+
+ expect(rows).toHaveLength(3);
+
+ // Verify the audit logs are sorted by createdAt DESC
+ expect(rows[0].action).toBe("DELETE");
+ expect(rows[0].resourceType).toBe("prompt");
+ expect(rows[0].type).toBe("USER");
+ expect(rows[0].before).toBe(
+ JSON.stringify({ name: "old-prompt", content: "Hello World" }),
+ );
+ expect(rows[0].after).toBe(null);
+
+ expect(rows[1].action).toBe("UPDATE");
+ expect(rows[1].resourceType).toBe("score");
+ expect(rows[1].type).toBe("API_KEY");
+ expect(rows[1].before).toBe(JSON.stringify({ value: 0.5 }));
+ expect(rows[1].after).toBe(JSON.stringify({ value: 0.8 }));
+
+ expect(rows[2].action).toBe("CREATE");
+ expect(rows[2].resourceType).toBe("trace");
+ expect(rows[2].type).toBe("USER");
+ expect(rows[2].before).toBe(null);
+ expect(rows[2].after).toBe(
+ JSON.stringify({ name: "test-trace", version: 1 }),
+ );
+
+ // Verify all rows have the correct project ID
+ rows.forEach((row) => {
+ expect(row.projectId).toBe(projectId);
+ expect(row.orgId).toBe(orgId);
+ });
+ });
});
diff --git a/worker/src/features/database-read-stream/getDatabaseReadStream.ts b/worker/src/features/database-read-stream/getDatabaseReadStream.ts
index 01351ddd81..1da951e8c3 100644
--- a/worker/src/features/database-read-stream/getDatabaseReadStream.ts
+++ b/worker/src/features/database-read-stream/getDatabaseReadStream.ts
@@ -35,6 +35,7 @@ const tableNameToTimeFilterColumn: Record<BatchTableNames, string> = {
traces: "timestamp",
observations: "startTime",
dataset_run_items: "createdAt",
+ audit_logs: "createdAt",
};
const tableNameToTimeFilterColumnCh: Record<BatchTableNames, string> = {
scores: "timestamp",
@@ -42,6 +43,7 @@ const tableNameToTimeFilterColumnCh: Record<BatchTableNames, string> = {
traces: "timestamp",
observations: "startTime",
dataset_run_items: "createdAt",
+ audit_logs: "createdAt",
};
const isGenerationTimestampFilter = (
filter: FilterCondition,
@@ -431,6 +433,46 @@ export const getDatabaseReadStream = async ({
rowLimit,
);
}
+
+ case "audit_logs": {
+ return new DatabaseReadStream<unknown>(
+ async (pageSize: number, offset: number) => {
+ const auditLogs = await prisma.auditLog.findMany({
+ where: {
+ projectId: projectId,
+ createdAt: {
+ lt: cutoffCreatedAt,
+ },
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ skip: offset,
+ take: pageSize,
+ });
+
+ return auditLogs.map((log) => ({
+ id: log.id,
+ createdAt: log.createdAt,
+ updatedAt: log.updatedAt,
+ type: log.type,
+ apiKeyId: log.apiKeyId,
+ userId: log.userId,
+ orgId: log.orgId,
+ userOrgRole: log.userOrgRole,
+ projectId: log.projectId,
+ userProjectRole: log.userProjectRole,
+ resourceType: log.resourceType,
+ resourceId: log.resourceId,
+ action: log.action,
+ before: log.before,
+ after: log.after,
+ }));
+ },
+ env.BATCH_EXPORT_PAGE_SIZE,
+ rowLimit,
+ );
+ }
default:
throw new Error(`Unhandled table case: ${tableName}`);
}