Skip to content

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

feat: adjust trace redirect to uniqueness of trace ids to projects (#4598)

Max Deichmann241d1119f937
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index 89706ffff4..c4b8e04211 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -24,7 +24,6 @@ import { convertDateToClickhouseDateTime } from "../clickhouse/client";
import { convertClickhouseToDomain } from "./traces_converters";
import { clickhouseSearchCondition } from "../queries/clickhouse-sql/search";
import { TRACE_TO_OBSERVATIONS_INTERVAL } from "./constants";
-import { FetchTracesTableProps } from "../services/traces-ui-table-service";
export const checkTraceExists = async (
projectId: string,
@@ -757,3 +756,26 @@ export const getUserMetrics = async (projectId: string, userIds: string[]) => {
totalCost: Number(row.sum_total_cost),
}));
};
+
+export const getTracesByIdsForAnyProject = async (traceIds: string[]) => {
+ const query = `
+ SELECT id, project_id
+ FROM traces
+ WHERE id IN ({traceIds: Array(String)})
+ ORDER BY event_ts DESC
+ LIMIT 1 by id, project_id;`;
+ const records = await queryClickhouse<{
+ id: string;
+ project_id: string;
+ }>({
+ query,
+ params: {
+ traceIds,
+ },
+ });
+
+ return records.map((record) => ({
+ id: record.id,
+ projectId: record.project_id,
+ }));
+};
diff --git a/web/src/pages/trace/[traceId].tsx b/web/src/pages/trace/[traceId].tsx
index f009b70534..ab206a3bb4 100644
--- a/web/src/pages/trace/[traceId].tsx
+++ b/web/src/pages/trace/[traceId].tsx
@@ -1,50 +1,50 @@
-import { prisma } from "@langfuse/shared/src/db";
+import { ErrorPage } from "@/src/components/error-page";
+import { getTracesByIdsForAnyProject } from "@langfuse/shared/src/server";
import { type GetServerSideProps } from "next";
import { useRouter } from "next/router";
export const getServerSideProps: GetServerSideProps = async (context) => {
if (!context.params) {
return {
- notFound: true,
+ props: {
+ notFound: true,
+ },
};
}
const traceId = context.params.traceId as string;
- const trace = await prisma.trace.findUnique({
- where: {
- id: traceId,
- },
- select: {
- project: {
- select: {
- id: true,
- },
- },
- },
- });
+ const traces = await getTracesByIdsForAnyProject([traceId]);
- if (!trace) {
+ if (!traces || traces.length === 0 || traces.length > 1) {
return {
- notFound: true,
+ props: {
+ notFound: true,
+ },
};
}
return {
redirect: {
- destination: `/project/${trace.project.id}/traces/${traceId}`,
+ destination: `/project/${traces[0].projectId}/traces/${traceId}`,
permanent: false,
},
};
};
-const TraceRedirectPage = () => {
+const TraceRedirectPage = ({ notFound }: { notFound?: boolean }) => {
const router = useRouter();
if (router.isFallback) {
return <div>Loading...</div>;
}
- return <div>Redirecting...</div>;
+ if (notFound) {
+ return (
+ <ErrorPage message="Trace not found. Please upgrade the SDK as we changed the URL schema." />
+ );
+ }
+
+ return null;
};
export default TraceRedirectPage;