Skip to content

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

fix(web): drop interactive transaction from API-key audit logs (#16748)

Niklas Semmler388f2aa0ef58
diff --git a/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts b/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts
index 04cc9a0963..8c57b8ed9e 100644
--- a/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts
+++ b/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts
@@ -1,10 +1,15 @@
import { randomUUID } from "crypto";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { auditLog } from "@/src/features/audit-logs/auditLog";
import { prisma, AuditLogRecordType } from "@langfuse/shared/src/db";
import { createAndAddApiKeysToDb } from "@langfuse/shared/src/server/auth/apiKeys";
import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
describe("in-app agent audit logging", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
it("stores the creator on in-app-agent MCP keys", async () => {
const { projectId } = await createOrgProjectAndApiKey();
const user = await prisma.user.create({
@@ -86,4 +91,47 @@ describe("in-app agent audit logging", () => {
userId: user.id,
});
});
+
+ it("writes API-key audit logs without an interactive Prisma transaction", async () => {
+ const { orgId, projectId, publicKey } = await createOrgProjectAndApiKey();
+ const apiKey = await prisma.apiKey.findUniqueOrThrow({
+ where: { publicKey },
+ select: { id: true },
+ });
+
+ const resourceId = randomUUID();
+ const transactionSpy = vi.spyOn(prisma, "$transaction");
+
+ await auditLog({
+ action: "create",
+ resourceType: "annotationQueueItem",
+ resourceId,
+ orgId,
+ projectId,
+ apiKeyId: apiKey.id,
+ });
+
+ expect(transactionSpy).not.toHaveBeenCalled();
+
+ const persistedAuditLog = await prisma.auditLog.findFirstOrThrow({
+ where: {
+ apiKeyId: apiKey.id,
+ resourceId,
+ },
+ select: {
+ type: true,
+ apiKeyId: true,
+ userId: true,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ expect(persistedAuditLog).toEqual({
+ type: AuditLogRecordType.API_KEY,
+ apiKeyId: apiKey.id,
+ userId: null,
+ });
+ });
});
diff --git a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
index 31c73c0685..43a3e49780 100644
--- a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
+++ b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
@@ -398,7 +398,7 @@ describe("MCP public API tools", () => {
).resolves.toBe(assignmentAuditLogCount + 1);
const auditLogCreateSpy = vi
- .spyOn(prisma, "$transaction")
+ .spyOn(prisma.auditLog, "create")
.mockRejectedValueOnce(new Error("audit failed"));
try {
diff --git a/web/src/features/audit-logs/auditLog.ts b/web/src/features/audit-logs/auditLog.ts
index 17654ab7a2..aec41a8a4b 100644
--- a/web/src/features/audit-logs/auditLog.ts
+++ b/web/src/features/audit-logs/auditLog.ts
@@ -94,28 +94,29 @@ export async function auditLog(log: AuditLog, prisma?: typeof _prisma) {
};
if ("apiKeyId" in log) {
- await db.$transaction(async (tx) => {
- const apiKey = await tx.apiKey.findUnique({
- where: { id: log.apiKeyId },
- select: {
- isInAppAgentKey: true,
- createdByUserId: true,
- },
- });
+ // Sequential find + create, not $transaction. Interactive transactions
+ // use a 5s timeout and hold a pooled connection across both awaits, so
+ // an event-loop stall can 500 public API writes that only need an audit log.
+ const apiKey = await db.apiKey.findUnique({
+ where: { id: log.apiKeyId },
+ select: {
+ isInAppAgentKey: true,
+ createdByUserId: true,
+ },
+ });
- await tx.auditLog.create({
- data: {
- apiKeyId: log.apiKeyId,
- userId:
- apiKey?.isInAppAgentKey === true
- ? (apiKey.createdByUserId ?? undefined)
- : undefined,
- orgId: log.orgId,
- projectId: log.projectId,
- type: AuditLogRecordType.API_KEY,
- ...shared,
- },
- });
+ await db.auditLog.create({
+ data: {
+ apiKeyId: log.apiKeyId,
+ userId:
+ apiKey?.isInAppAgentKey === true
+ ? (apiKey.createdByUserId ?? undefined)
+ : undefined,
+ orgId: log.orgId,
+ projectId: log.projectId,
+ type: AuditLogRecordType.API_KEY,
+ ...shared,
+ },
});
return;