Skip to content

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

Externa id for traces (create trace, create score) (#33)

Marc Klingene1d04621b211
diff --git a/fern/client/definition/score.yml b/fern/client/definition/score.yml
index 738ed56c5d..8657923f83 100644
--- a/fern/client/definition/score.yml
+++ b/fern/client/definition/score.yml
@@ -15,13 +15,19 @@ types:
CreateScoreRequest:
properties:
traceId: string
+ traceIdType: optional<TraceIdType>
name: string
value: integer
observationId: optional<string>
Score:
properties:
id: string
+ traceId: string
name: string
value: integer
observationId: optional<string>
timestamp: datetime
+ TraceIdType:
+ enum:
+ - LANGFUSE
+ - EXTERNAL
diff --git a/fern/server/definition/score.yml b/fern/server/definition/score.yml
index 738ed56c5d..8657923f83 100644
--- a/fern/server/definition/score.yml
+++ b/fern/server/definition/score.yml
@@ -15,13 +15,19 @@ types:
CreateScoreRequest:
properties:
traceId: string
+ traceIdType: optional<TraceIdType>
name: string
value: integer
observationId: optional<string>
Score:
properties:
id: string
+ traceId: string
name: string
value: integer
observationId: optional<string>
timestamp: datetime
+ TraceIdType:
+ enum:
+ - LANGFUSE
+ - EXTERNAL
diff --git a/fern/server/definition/trace.yml b/fern/server/definition/trace.yml
index 2f4214e166..0bb4bf56ec 100644
--- a/fern/server/definition/trace.yml
+++ b/fern/server/definition/trace.yml
@@ -15,10 +15,12 @@ types:
CreateTraceRequest:
properties:
name: optional<string>
+ externalId: optional<string>
metadata: optional<unknown>
Trace:
properties:
id: string
timestamp: datetime
+ externalId: optional<string>
name: optional<string>
metadata: optional<unknown>
diff --git a/prisma/migrations/20230626095337_external_trace_id/migration.sql b/prisma/migrations/20230626095337_external_trace_id/migration.sql
new file mode 100644
index 0000000000..0811ad3ea9
--- /dev/null
+++ b/prisma/migrations/20230626095337_external_trace_id/migration.sql
@@ -0,0 +1,2 @@
+ALTER TABLE "traces" ADD COLUMN "external_id" TEXT;
+CREATE UNIQUE INDEX "traces_project_id_external_id_key" ON "traces"("project_id", "external_id");
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 52c8d95e80..789377c9aa 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -112,16 +112,18 @@ enum MembershipRole {
}
model Trace {
- id String @id @default(cuid())
- timestamp DateTime @default(now())
- name String?
- metadata Json?
- projectId String @map("project_id")
- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ id String @id @default(cuid())
+ externalId String? @map("external_id")
+ timestamp DateTime @default(now())
+ name String?
+ metadata Json?
+ projectId String @map("project_id")
+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
observations Observation[]
scores Score[]
+ @@unique([projectId, externalId])
@@map("traces")
}
diff --git a/src/pages/api/public/scores.ts b/src/pages/api/public/scores.ts
index 44a363a273..4981ee6e1f 100644
--- a/src/pages/api/public/scores.ts
+++ b/src/pages/api/public/scores.ts
@@ -10,7 +10,8 @@ const ScoreSchema = z.object({
name: z.string(),
value: z.number().int(),
traceId: z.string(),
- observationId: z.string().optional(),
+ traceIdType: z.enum(["LANGFUSE", "EXTERNAL"]).nullish(),
+ observationId: z.string().nullish(),
});
export default async function handler(
@@ -37,11 +38,26 @@ export default async function handler(
try {
const obj = ScoreSchema.parse(req.body);
+ // If externalTraceId is provided, find the traceId
+ const traceId =
+ obj.traceIdType === "EXTERNAL"
+ ? (
+ await prisma.trace.findUniqueOrThrow({
+ where: {
+ projectId_externalId: {
+ projectId: authCheck.scope.projectId,
+ externalId: obj.traceId,
+ },
+ },
+ })
+ ).id
+ : obj.traceId;
+
// CHECK ACCESS SCOPE
const accessCheck = await checkApiAccessScope(
authCheck.scope,
[
- { type: "trace", id: obj.traceId },
+ { type: "trace", id: traceId },
...(obj.observationId
? [{ type: "observation" as const, id: obj.observationId }]
: []),
@@ -59,7 +75,7 @@ export default async function handler(
timestamp: new Date(),
value: obj.value,
name: obj.name,
- trace: { connect: { id: obj.traceId } },
+ trace: { connect: { id: traceId } },
...(obj.observationId && {
observation: { connect: { id: obj.observationId } },
}),
diff --git a/src/pages/api/public/traces.ts b/src/pages/api/public/traces.ts
index aff2e5ea3b..7c7fca1cab 100644
--- a/src/pages/api/public/traces.ts
+++ b/src/pages/api/public/traces.ts
@@ -6,6 +6,7 @@ import { verifyAuthHeaderAndReturnScope } from "@/src/features/publicApi/server/
const CreateTraceSchema = z.object({
name: z.string().nullish(),
+ externalId: z.string().nullish(),
metadata: z.unknown().nullish(),
});
@@ -32,7 +33,7 @@ export default async function handler(
// END CHECK AUTH
try {
- const { name, metadata } = CreateTraceSchema.parse(req.body);
+ const { name, metadata, externalId } = CreateTraceSchema.parse(req.body);
// CHECK ACCESS SCOPE
if (authCheck.scope.accessLevel !== "all")
@@ -46,6 +47,7 @@ export default async function handler(
data: {
timestamp: new Date(),
projectId: authCheck.scope.projectId,
+ externalId: externalId ?? undefined,
name: name ?? undefined,
metadata: metadata ?? undefined,
},
diff --git a/src/pages/project/[projectId]/traces.tsx b/src/pages/project/[projectId]/traces.tsx
index da5ec55400..4d8b129008 100644
--- a/src/pages/project/[projectId]/traces.tsx
+++ b/src/pages/project/[projectId]/traces.tsx
@@ -23,6 +23,7 @@ import {
type ScoreFilter,
} from "@/src/utils/tanstack";
import { type Trace, type Score } from "@prisma/client";
+import { lastCharacters } from "@/src/utils/string";
export type TableScore = {
id: string;
@@ -32,6 +33,7 @@ export type TableScore = {
export type TraceTableRow = {
id: string;
+ externalId?: string;
timestamp: string;
name: string;
metadata?: string;
@@ -77,6 +79,7 @@ export default function Traces() {
): TraceTableRow => {
return {
id: trace.id,
+ externalId: trace.externalId ?? undefined,
timestamp: trace.timestamp.toISOString(),
name: trace.name ?? "",
metadata: JSON.stringify(trace.metadata),
@@ -127,6 +130,16 @@ export default function Traces() {
},
},
},
+ {
+ accessorKey: "externalId",
+ header: "External ID",
+ cell: ({ row }) =>
+ row.getValue("externalId") ? (
+ <span>...{lastCharacters(row.getValue("externalId"), 7)}</span>
+ ) : (
+ <span></span>
+ ),
+ },
{
accessorKey: "timestamp",
header: "Timestamp",
diff --git a/src/pages/project/[projectId]/traces/[traceId].tsx b/src/pages/project/[projectId]/traces/[traceId].tsx
index c8325e0017..85a3645959 100644
--- a/src/pages/project/[projectId]/traces/[traceId].tsx
+++ b/src/pages/project/[projectId]/traces/[traceId].tsx
@@ -30,6 +30,14 @@ export default function TracePage() {
label: "Timestamp",
value: trace.data.timestamp.toLocaleString(),
},
+ ...(trace.data.externalId
+ ? [
+ {
+ label: "External ID",
+ value: trace.data.externalId,
+ },
+ ]
+ : []),
{
label: "Name",
value: trace.data.name,