Skip to content

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

refactor: add sentry users to API (#1269)

Max Deichmannfcc0ee922fbc
diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.ts
index f75bcd5f78..1b7082d2b9 100644
--- a/web/src/features/public-api/server/apiAuth.ts
+++ b/web/src/features/public-api/server/apiAuth.ts
@@ -6,6 +6,7 @@ import {
import { type ApiAccessScope } from "@/src/features/public-api/server/types";
import { prisma } from "@/src/server/db";
import { isPrismaException } from "@/src/utils/exceptions";
+import * as Sentry from "@sentry/node";
export type AuthHeaderVerificationResult =
| {
@@ -66,6 +67,10 @@ export async function verifyAuthHeaderAndReturnScope(
throw new Error("Invalid credentials");
}
+ Sentry.setUser({
+ id: projectId,
+ });
+
return {
validKey: true,
scope: {
@@ -79,6 +84,9 @@ export async function verifyAuthHeaderAndReturnScope(
const publicKey = authHeader.replace("Bearer ", "");
const dbKey = await findDbKeyOrThrow(publicKey);
+ Sentry.setUser({
+ id: dbKey.projectId,
+ });
return {
validKey: true,
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index ad1ac48e07..c08aba2036 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -19,7 +19,7 @@ import { type Session } from "next-auth";
import { getServerAuthSession } from "@/src/server/auth";
import { DB, prisma } from "@/src/server/db";
-
+import * as Sentry from "@sentry/node";
import * as z from "zod";
type CreateContextOptions = {
@@ -56,6 +56,12 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => {
// Get the session from the server using the getServerSession wrapper function
const session = await getServerAuthSession({ req, res });
+ Sentry.setUser({
+ id: session?.user?.id,
+ email: session?.user?.email ?? undefined,
+ username: session?.user?.name ?? undefined,
+ });
+
return createInnerTRPCContext({
session,
});