Skip to content

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

fix(tracing): preserve missing trace error on publish (#17668)

Max Deichmann2e07e447a094
diff --git a/web/src/__tests__/server/traces-trpc-events-only.servertest.ts b/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
index e76baa6b4a..510bffc9ed 100644
--- a/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
+++ b/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
@@ -322,9 +322,8 @@ maybe("traces trpc (events_only write mode)", () => {
});
});
- // Same routing as the bookmark case, but publish throws NOT_FOUND (rewrapped
- // as INTERNAL_SERVER_ERROR) instead of silently no-opping when the read is
- // misrouted to the empty legacy table.
+ // Same routing as the bookmark case, but publish throws NOT_FOUND instead of
+ // silently no-opping when the read is misrouted to the empty legacy table.
it("should make a trace public that only exists in the events table", async () => {
const traceId = randomUUID();
diff --git a/web/src/__tests__/server/traces-trpc.servertest.ts b/web/src/__tests__/server/traces-trpc.servertest.ts
index f0471fa4a4..a5886d61d5 100644
--- a/web/src/__tests__/server/traces-trpc.servertest.ts
+++ b/web/src/__tests__/server/traces-trpc.servertest.ts
@@ -1305,5 +1305,18 @@ describe("traces trpc", () => {
});
}
});
+
+ it("returns NOT_FOUND when publishing a trace that does not exist", async () => {
+ await expect(
+ caller.traces.publish({
+ projectId,
+ traceId: randomUUID(),
+ public: true,
+ }),
+ ).rejects.toMatchObject({
+ code: "NOT_FOUND",
+ message: "Trace not found",
+ });
+ });
});
});
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index a71ef67187..e2ad498acf 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -54,6 +54,7 @@ import {
ScoreDataTypeArray,
ScoreDataTypeEnum,
LISTABLE_SCORE_TYPES,
+ LangfuseNotFoundError,
} from "@langfuse/shared";
import { TRPCError } from "@trpc/server";
import { createBatchActionJob } from "@/src/features/table/server";
@@ -636,50 +637,37 @@ export const traceRouter = createTRPCRouter({
projectId: input.projectId,
scope: "objects:publish",
});
- try {
- await auditLog({
- session: ctx.session,
- resourceType: "trace",
- resourceId: input.traceId,
- action: "publish",
- after: input.public,
- });
+ await auditLog({
+ session: ctx.session,
+ resourceType: "trace",
+ resourceId: input.traceId,
+ action: "publish",
+ after: input.public,
+ });
- // eslint-disable-next-line @typescript-eslint/no-deprecated
- const clickhouseTrace = await getTraceById({
- traceId: input.traceId,
- projectId: input.projectId,
- });
- if (!clickhouseTrace) {
- logger.error(
- `Trace not found in Clickhouse: ${input.traceId}. Skipping publishing.`,
- );
- throw new TRPCError({
- code: "NOT_FOUND",
- message: "Trace not found",
- });
- }
- clickhouseTrace.public = input.public;
- const promises = [
- upsertTrace(convertTraceDomainToClickhouse(clickhouseTrace)),
- ];
- if (env.LANGFUSE_MIGRATION_V4_WRITE_MODE !== "legacy") {
- promises.push(
- updateEvents(
- input.projectId,
- { traceIds: [clickhouseTrace.id] },
- { public: input.public },
- ),
- );
- }
- await Promise.all(promises);
- return clickhouseTrace;
- } catch (error) {
- logger.error("Failed to call traces.publish", error);
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- });
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
+ const clickhouseTrace = await getTraceById({
+ traceId: input.traceId,
+ projectId: input.projectId,
+ });
+ if (!clickhouseTrace) {
+ throw new LangfuseNotFoundError("Trace not found");
+ }
+ clickhouseTrace.public = input.public;
+ const promises = [
+ upsertTrace(convertTraceDomainToClickhouse(clickhouseTrace)),
+ ];
+ if (env.LANGFUSE_MIGRATION_V4_WRITE_MODE !== "legacy") {
+ promises.push(
+ updateEvents(
+ input.projectId,
+ { traceIds: [clickhouseTrace.id] },
+ { public: input.public },
+ ),
+ );
}
+ await Promise.all(promises);
+ return clickhouseTrace;
}),
getAgentGraphData: protectedGetTraceProcedure
.input(