Skip to content

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

feat: move traces.hasany to clickhouse (#4265)

Max Deichmann24a549dc548a
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index a72e64e941..28d564d188 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -313,6 +313,24 @@ export const getTracesByIds = async (
return records.map(convertClickhouseToDomain);
};
+export const hasAnyTrace = async (projectId: string) => {
+ const query = `
+ SELECT count(*) as count
+ FROM traces
+ WHERE project_id = {projectId: String}
+ LIMIT 1
+ `;
+
+ const rows = await queryClickhouse<{ count: string }>({
+ query,
+ params: {
+ projectId,
+ },
+ });
+
+ return rows.length > 0 && Number(rows[0].count) > 0;
+};
+
export const getTraceByIdOrThrow = async (
traceId: string,
projectId: string,
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 5fa367ec93..fb9f44dc93 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -45,6 +45,7 @@ import {
logger,
upsertTrace,
convertTraceDomainToClickhouse,
+ hasAnyTrace,
} from "@langfuse/shared/src/server";
import { TRPCError } from "@trpc/server";
import { measureAndReturnApi } from "@/src/server/utils/checkClickhouseAccess";
@@ -68,17 +69,32 @@ export type ObservationReturnType = Omit<
export const traceRouter = createTRPCRouter({
hasAny: protectedProjectProcedure
- .input(z.object({ projectId: z.string() }))
+ .input(
+ z.object({
+ projectId: z.string(),
+ queryClickhouse: z.boolean().default(false),
+ }),
+ )
.query(async ({ input, ctx }) => {
- const hasAny = await ctx.prisma.trace.findFirst({
- where: {
- projectId: input.projectId,
+ return await measureAndReturnApi({
+ input,
+ operation: "traces.hasAny",
+ user: ctx.session.user,
+ pgExecution: async () => {
+ const hasAny = await ctx.prisma.trace.findFirst({
+ where: {
+ projectId: input.projectId,
+ },
+ select: {
+ id: true,
+ },
+ });
+ return hasAny !== null;
},
- select: {
- id: true,
+ clickhouseExecution: async () => {
+ return await hasAnyTrace(input.projectId);
},
});
- return hasAny !== null;
}),
all: protectedProjectProcedure
.input(