Skip to content

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

fix(web): route tracing onboarding check to events tables in events_only mode (#14901)

Steffen Schmitz23620540aec7
diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.ts
index 08015c4857..a4c349df31 100644
--- a/packages/shared/src/server/repositories/events.ts
+++ b/packages/shared/src/server/repositories/events.ts
@@ -26,6 +26,9 @@ import {
import {
getTraceByIdFromTracesTable,
getTracesIdentifierForSessionFromTracesTable,
+ hasAnyTrace,
+ persistProjectHasTracesFlag,
+ readProjectHasTracesFlag,
} from "./traces";
import {
DateTimeFilter,
@@ -1194,6 +1197,64 @@ export const getTracesIdentifierForSession = async (
return getTracesIdentifierForSessionFromEvents(projectId, sessionId);
};
+/**
+ * Check if any tracing data exists in the events table.
+ */
+export const hasAnyTraceFromEventsTable = async (
+ projectId: string,
+): Promise<boolean> => {
+ const query = `
+ SELECT 1
+ FROM events_core
+ WHERE project_id = {projectId: String}
+ AND is_deleted = 0
+ LIMIT 1
+ `;
+
+ const rows = await measureAndReturn({
+ operationName: "hasAnyTraceFromEventsTable",
+ projectId,
+ input: { params: { projectId } },
+ fn: async (input) => {
+ return queryClickhouse<{ 1: number }>({
+ query,
+ params: input.params,
+ tags: { projectId },
+ preferredClickhouseService: "EventsReadOnly",
+ clickhouseSettings: {
+ max_threads: 1,
+ },
+ });
+ },
+ });
+
+ return rows.length > 0;
+};
+
+/**
+ * Routing wrapper for the tracing onboarding gate ("has this project ingested
+ * any tracing data yet?").
+ *
+ * If data is only written into the events tables, we look there and go to the
+ * legacy traces table otherwise. Both paths share the project's `hasTraces`
+ * flag as a read-through cache, so steady-state checks skip ClickHouse.
+ */
+export const hasAnyTracingData = async (projectId: string) => {
+ if (env.LANGFUSE_MIGRATION_V4_WRITE_MODE !== "events_only") {
+ return hasAnyTrace(projectId);
+ }
+
+ if (await readProjectHasTracesFlag(projectId)) {
+ return true;
+ }
+
+ const result = await hasAnyTraceFromEventsTable(projectId);
+ if (result) {
+ await persistProjectHasTracesFlag(projectId);
+ }
+ return result;
+};
+
type PublicApiObservationsQuery = {
projectId: string;
page: number;
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index 650d84e179..72bd07ab6d 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -313,22 +313,54 @@ export const getTracesBySessionId = async (
return traces;
};
-export const hasAnyTrace = async (projectId: string) => {
- // Check PostgreSQL flag first — once set, it's never reverted
+/**
+ * Read-through shortcut on the project's `hasTraces` flag — once set, the flag
+ * is never reverted, so a true value can skip ClickHouse entirely.
+ */
+export const readProjectHasTracesFlag = async (
+ projectId: string,
+): Promise<boolean> => {
try {
const project = await prisma.project.findUnique({
where: { id: projectId },
select: { hasTraces: true },
});
- if (project?.hasTraces) {
- return true;
- }
+ return project?.hasTraces === true;
} catch (error) {
traceException(error);
logger.error("Failed to read hasTraces flag from PostgreSQL", {
projectId,
error,
});
+ return false;
+ }
+};
+
+/**
+ * Persist a positive tracing-data detection on the project's `hasTraces` flag.
+ * Only updates if not already set to avoid unnecessary writes.
+ */
+export const persistProjectHasTracesFlag = async (
+ projectId: string,
+): Promise<void> => {
+ try {
+ await prisma.project.updateMany({
+ where: { id: projectId, hasTraces: false },
+ data: { hasTraces: true },
+ });
+ } catch (error) {
+ traceException(error);
+ logger.error("Failed to persist hasTraces flag to PostgreSQL", {
+ projectId,
+ error,
+ });
+ }
+};
+
+export const hasAnyTrace = async (projectId: string) => {
+ // Check PostgreSQL flag first — once set, it's never reverted
+ if (await readProjectHasTracesFlag(projectId)) {
+ return true;
}
const result = await measureAndReturn({
@@ -362,20 +394,8 @@ export const hasAnyTrace = async (projectId: string) => {
});
// Persist positive result in PostgreSQL — once a project has traces, it stays true
- // Only update if not already set to avoid unnecessary writes
if (result) {
- try {
- await prisma.project.updateMany({
- where: { id: projectId, hasTraces: false },
- data: { hasTraces: true },
- });
- } catch (error) {
- traceException(error);
- logger.error("Failed to persist hasTraces flag to PostgreSQL", {
- projectId,
- error,
- });
- }
+ await persistProjectHasTracesFlag(projectId);
}
return result;
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 cf1588c82f..90d69fa6e3 100644
--- a/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
+++ b/web/src/__tests__/server/traces-trpc-events-only.servertest.ts
@@ -101,6 +101,86 @@ maybe("traces trpc (events_only write mode)", () => {
expect(env.LANGFUSE_MIGRATION_V4_WRITE_MODE).toBe("events_only");
});
+ // On a fresh events_only deployment tracing data is written ONLY to the
+ // events tables; the legacy `traces` table stays intentionally empty. The
+ // onboarding gate must detect data in the events table, otherwise the
+ // Traces UI is stuck on the "Set up tracing" screen forever (#14827).
+ it("should clear the tracing onboarding gate from events-table data", async () => {
+ // Fresh project: `hasTraces` flag unset, no legacy rows, no retention.
+ const freshProjectId = randomUUID();
+ await prisma.project.create({
+ data: {
+ id: freshProjectId,
+ name: "events-only-onboarding",
+ orgId: "seed-org-id",
+ },
+ });
+
+ const freshSession: Session = {
+ ...session,
+ user: {
+ ...session.user!,
+ organizations: [
+ {
+ ...session.user!.organizations[0],
+ projects: [
+ {
+ id: freshProjectId,
+ role: "ADMIN",
+ retentionDays: null,
+ deletedAt: null,
+ name: "events-only-onboarding",
+ },
+ ],
+ },
+ ],
+ },
+ };
+ const freshCtx = createInnerTRPCContext({ session: freshSession });
+ const freshCaller = appRouter.createCaller({ ...freshCtx, prisma });
+
+ try {
+ // Gate stays closed before any data is ingested.
+ await expect(
+ freshCaller.traces.hasTracingConfigured({
+ projectId: freshProjectId,
+ }),
+ ).resolves.toBe(false);
+
+ // Trace is written to the events table only - NOT the legacy table.
+ const traceId = randomUUID();
+ await createEventsCh([
+ createEvent({
+ id: traceId,
+ span_id: traceId,
+ trace_id: traceId,
+ project_id: freshProjectId,
+ parent_span_id: null,
+ }),
+ ]);
+
+ // Gate must open from events-table data alone (ClickHouse insert
+ // visibility can lag).
+ await waitForExpect(async () => {
+ expect(
+ await freshCaller.traces.hasTracingConfigured({
+ projectId: freshProjectId,
+ }),
+ ).toBe(true);
+ });
+
+ // A positive detection persists to the project's hasTraces flag so the
+ // UI can stop polling ClickHouse.
+ const project = await prisma.project.findUnique({
+ where: { id: freshProjectId },
+ select: { hasTraces: true },
+ });
+ expect(project?.hasTraces).toBe(true);
+ } finally {
+ await prisma.project.delete({ where: { id: freshProjectId } });
+ }
+ });
+
// A trace ingested after the legacy->events cutover lives ONLY in the events
// table. In events_only mode getTraceById must route the mutation read to the
// events table; otherwise the legacy `traces` read returns undefined and the
diff --git a/web/src/__tests__/server/traces-trpc.servertest.ts b/web/src/__tests__/server/traces-trpc.servertest.ts
index 1d31fa124e..4a246cff5b 100644
--- a/web/src/__tests__/server/traces-trpc.servertest.ts
+++ b/web/src/__tests__/server/traces-trpc.servertest.ts
@@ -721,6 +721,79 @@ describe("traces trpc", () => {
});
});
+ describe("traces.hasTracingConfigured", () => {
+ // In legacy/dual write modes the legacy `traces` table is still written and
+ // is the freshest source, so the onboarding gate must open from a legacy
+ // trace row alone. The events_only routing lives in
+ // traces-trpc-events-only.servertest.ts.
+ it("should clear the tracing onboarding gate from legacy traces-table data", async () => {
+ // Fresh project: `hasTraces` flag unset, no rows, no retention.
+ const freshProjectId = randomUUID();
+ await prisma.project.create({
+ data: {
+ id: freshProjectId,
+ name: "legacy-onboarding",
+ orgId: "seed-org-id",
+ },
+ });
+
+ const freshSession: Session = {
+ ...session,
+ user: {
+ ...session.user!,
+ organizations: [
+ {
+ ...session.user!.organizations[0],
+ projects: [
+ {
+ id: freshProjectId,
+ role: "ADMIN",
+ retentionDays: null,
+ deletedAt: null,
+ name: "legacy-onboarding",
+ },
+ ],
+ },
+ ],
+ },
+ };
+ const freshCtx = createInnerTRPCContext({ session: freshSession });
+ const freshCaller = appRouter.createCaller({ ...freshCtx, prisma });
+
+ try {
+ // Gate stays closed before any data is ingested.
+ await expect(
+ freshCaller.traces.hasTracingConfigured({
+ projectId: freshProjectId,
+ }),
+ ).resolves.toBe(false);
+
+ // Trace is written to the legacy table only.
+ await createTracesCh([createTrace({ project_id: freshProjectId })]);
+
+ // Gate must open from legacy traces-table data alone (ClickHouse
+ // insert visibility can lag).
+ await waitForExpect(async () => {
+ expect(
+ await freshCaller.traces.hasTracingConfigured({
+ projectId: freshProjectId,
+ }),
+ ).toBe(true);
+ });
+
+ // A positive detection persists to the project's hasTraces flag so
+ // the UI can stop polling ClickHouse.
+ const project = await prisma.project.findUnique({
+ where: { id: freshProjectId },
+ select: { hasTraces: true },
+ });
+ expect(project?.hasTraces).toBe(true);
+ } finally {
+ await prisma.project.delete({ where: { id: freshProjectId } });
+ }
+ });
+ });
+
describe("traces flags", () => {
const useEventsTable =
env.LANGFUSE_MIGRATION_V4_ALLOW_PREVIEW_OPT_IN === "true";
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index fb812f1ee8..05c7c4e920 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -40,7 +40,7 @@ import {
logger,
upsertTrace,
convertTraceDomainToClickhouse,
- hasAnyTrace,
+ hasAnyTracingData,
traceDeletionProcessor,
getTracesTableMetrics,
getCategoricalScoresGroupedByName,
@@ -106,8 +106,8 @@ export const traceRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- // Check if there are any traces in the database
- const hasTraces = await hasAnyTrace(input.projectId);
+ // Check if there is any tracing data in the database
+ const hasTraces = await hasAnyTracingData(input.projectId);
if (hasTraces) {
return true;