🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
added instrumentation (#357)
Max Deichmannc9b829e96cf6
diff --git a/src/features/public-api/server/apiAuth.ts b/src/features/public-api/server/apiAuth.tsindex 4b6c64e06e..c8f6de10c7 100644--- a/src/features/public-api/server/apiAuth.ts+++ b/src/features/public-api/server/apiAuth.ts@@ -1,6 +1,7 @@import { verifySecretKey } from "@/src/features/public-api/lib/apiKeys";import { type ApiAccessScope } from "@/src/features/public-api/server/types";import { prisma } from "@/src/server/db";+import { instrumentAsync } from "@/src/utils/instrumentation";type AuthHeaderVerificationResult =| {@@ -15,59 +16,67 @@ type AuthHeaderVerificationResult =export async function verifyAuthHeaderAndReturnScope(authHeader: string | undefined,): Promise<AuthHeaderVerificationResult> {- if (!authHeader) {- console.error("No authorization header");- return {- validKey: false,- error: "No authorization header",- };- }+ return instrumentAsync(+ { name: "verifyAuthHeaderAndReturnScope" },+ async () => {+ if (!authHeader) {+ console.error("No authorization header");+ return {+ validKey: false,+ error: "No authorization header",+ };+ }- try {- // Basic auth, full scope, needs secret key and public key- if (authHeader.startsWith("Basic ")) {- const { username: publicKey, password: secretKey } =- extractBasicAuthCredentials(authHeader);+ try {+ // Basic auth, full scope, needs secret key and public key+ if (authHeader.startsWith("Basic ")) {+ const { username: publicKey, password: secretKey } =+ extractBasicAuthCredentials(authHeader);- const dbKey = await findDbKeyOrThrow(publicKey);+ const dbKey = await findDbKeyOrThrow(publicKey);- const isValid = await verifySecretKey(secretKey, dbKey.hashedSecretKey);- if (!isValid) throw new Error("Invalid credentials");+ const isValid = await verifySecretKey(+ secretKey,+ dbKey.hashedSecretKey,+ );+ if (!isValid) throw new Error("Invalid credentials");- return {- validKey: true,- scope: {- projectId: dbKey.projectId,- accessLevel: "all",- },- };- }- // Bearer auth, limited scope, only needs public key- if (authHeader.startsWith("Bearer ")) {- const publicKey = authHeader.replace("Bearer ", "");+ return {+ validKey: true,+ scope: {+ projectId: dbKey.projectId,+ accessLevel: "all",+ },+ };+ }+ // Bearer auth, limited scope, only needs public key+ if (authHeader.startsWith("Bearer ")) {+ const publicKey = authHeader.replace("Bearer ", "");- const dbKey = await findDbKeyOrThrow(publicKey);+ const dbKey = await findDbKeyOrThrow(publicKey);++ return {+ validKey: true,+ scope: {+ projectId: dbKey.projectId,+ accessLevel: "scores",+ },+ };+ }+ } catch (error: unknown) {+ console.error("Error verifying auth header: ", error);+ return {+ validKey: false,+ error: error instanceof Error ? error.message : "Authorization error",+ };+ }return {- validKey: true,- scope: {- projectId: dbKey.projectId,- accessLevel: "scores",- },+ validKey: false,+ error: "Invalid authorization header",};- }- } catch (error: unknown) {- console.error("Error verifying auth header: ", error);- return {- validKey: false,- error: error instanceof Error ? error.message : "Authorization error",- };- }-- return {- validKey: false,- error: "Invalid authorization header",- };+ },+ );}function extractBasicAuthCredentials(basicAuthHeader: string): {diff --git a/src/utils/instrumentation.ts b/src/utils/instrumentation.tsnew file mode 100644index 0000000000..1dbacbe3ef--- /dev/null+++ b/src/utils/instrumentation.ts@@ -0,0 +1,30 @@+import { type Span } from "@sentry/nextjs/types/server";+import * as Sentry from "@sentry/nextjs";++type CallbackFn<T> = (span?: Span) => T;++export function instrument<T>(+ ctx: { name: string },+ callback: CallbackFn<T>,+): T {+ if (process.env.NEXT_PUBLIC_SENTRY_DSN) {+ return Sentry.startSpan(ctx, callback);+ } else {+ return callback();+ }+}++type CallbackAsyncFn<T> = (span?: Span) => Promise<T>;++export async function instrumentAsync<T>(+ ctx: { name: string },+ callback: CallbackAsyncFn<T>,+): Promise<T> {+ if (process.env.NEXT_PUBLIC_SENTRY_DSN) {+ return Sentry.startSpan(ctx, async (span) => {+ return callback(span);+ });+ } else {+ return callback();+ }+}