Skip to content

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

Add feature flagging (#47)

Marc Klingene759754238fc
diff --git a/.gitignore b/.gitignore
index e19de8ba69..60ec5f424c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,6 +34,7 @@ yarn-error.log*
# do not commit any .env files to git, except for the .env.example file. https://create.t3.gg/en/usage/env-variables#using-environment-variables
.env
.env.local
+.env*.local
# vercel
.vercel
diff --git a/package.json b/package.json
index 4120a74e65..8d75a587d7 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,9 @@
"prettier": "prettier --write ./src",
"clean": "rm -rf node_modules",
"start": "next start",
- "migrate:prod": "dotenv -e .env.local -- npx prisma migrate deploy"
+ "migrate:dev": "dotenv -e .env.local -- npx prisma migrate dev",
+ "migrate:prod": "dotenv -e .env.local -- npx prisma migrate deploy",
+ "prisma-studio": "dotenv -e .env.local -- npx prisma studio"
},
"prisma": {
"seed": "ts-node -r tsconfig-paths/register --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
diff --git a/prisma/migrations/20230717190411_users_feature_flags/migration.sql b/prisma/migrations/20230717190411_users_feature_flags/migration.sql
new file mode 100644
index 0000000000..cfec20c38a
--- /dev/null
+++ b/prisma/migrations/20230717190411_users_feature_flags/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "users" ADD COLUMN "feature_flags" TEXT[] DEFAULT ARRAY[]::TEXT[];
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index cea843ba72..7b88d5fd7e 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -58,6 +58,7 @@ model User {
memberships Membership[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
+ featureFlags String[] @default([]) @map("feature_flags")
@@map("users")
}
diff --git a/src/components/layouts/layout.tsx b/src/components/layouts/layout.tsx
index 339edd3360..1a126762e5 100644
--- a/src/components/layouts/layout.tsx
+++ b/src/components/layouts/layout.tsx
@@ -45,6 +45,11 @@ export default function Layout(props: PropsWithChildren) {
const projectId = router.query.projectId as string | undefined;
const navigation = [
{ name: "Dashboard", pathname: `/project/[projectId]`, icon: HomeIcon },
+ {
+ name: "Analytics (alpha)",
+ pathname: `/project/[projectId]/analytics`,
+ icon: LineChart,
+ },
{
name: "Traces",
pathname: `/project/[projectId]/traces`,
diff --git a/src/features/featureFlags/availableFlags.ts b/src/features/featureFlags/availableFlags.ts
new file mode 100644
index 0000000000..1b9fba274c
--- /dev/null
+++ b/src/features/featureFlags/availableFlags.ts
@@ -0,0 +1 @@
+export const availableFlags = ["analytics-alpha"] as const;
diff --git a/src/features/featureFlags/components/FeatureFlagToggle.tsx b/src/features/featureFlags/components/FeatureFlagToggle.tsx
new file mode 100644
index 0000000000..d04f2c035b
--- /dev/null
+++ b/src/features/featureFlags/components/FeatureFlagToggle.tsx
@@ -0,0 +1,19 @@
+import { type Flag } from "@/src/features/featureFlags/types";
+import { useSession } from "next-auth/react";
+
+export const FeatureFlagToggle = (props: {
+ featureFlag: Flag;
+ whenEnabled?: React.ReactNode;
+ whenDisabled?: React.ReactNode;
+ whenLoading?: React.ReactNode;
+}) => {
+ const session = useSession();
+ const flags = session.data?.user?.featureFlags;
+ const isEnabled = flags !== undefined && flags[props.featureFlag];
+
+ if (session.status === "loading") {
+ return props.whenLoading ?? <div>Loading ...</div>;
+ }
+
+ return isEnabled ? props.whenEnabled ?? <></> : props.whenDisabled ?? <></>;
+};
diff --git a/src/features/featureFlags/types.ts b/src/features/featureFlags/types.ts
new file mode 100644
index 0000000000..57021adee9
--- /dev/null
+++ b/src/features/featureFlags/types.ts
@@ -0,0 +1,6 @@
+import { type availableFlags } from "./availableFlags";
+
+export type Flag = (typeof availableFlags)[number];
+export type Flags = {
+ [key in (typeof availableFlags)[number]]: boolean;
+};
diff --git a/src/features/featureFlags/utils.ts b/src/features/featureFlags/utils.ts
new file mode 100644
index 0000000000..10a169c3ed
--- /dev/null
+++ b/src/features/featureFlags/utils.ts
@@ -0,0 +1,12 @@
+import { availableFlags } from "./availableFlags";
+import { type Flags } from "./types";
+
+export const parseFlags = (dbFlags: string[]): Flags => {
+ const parsedFlags = {} as Flags;
+
+ availableFlags.forEach((flag) => {
+ parsedFlags[flag] = dbFlags.includes(flag);
+ });
+
+ return parsedFlags;
+};
diff --git a/src/server/auth.ts b/src/server/auth.ts
index ce8fa25ed3..7683998bab 100644
--- a/src/server/auth.ts
+++ b/src/server/auth.ts
@@ -9,6 +9,7 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "@/src/server/db";
import CredentialsProvider from "next-auth/providers/credentials";
import { verifyPassword } from "@/src/features/auth/lib/emailPassword";
+import { parseFlags } from "@/src/features/featureFlags/utils";
/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
@@ -31,6 +32,7 @@ export const authOptions: NextAuthOptions = {
name: true,
email: true,
image: true,
+ featureFlags: true,
memberships: {
include: {
project: true,
@@ -54,6 +56,7 @@ export const authOptions: NextAuthOptions = {
name: membership.project.name,
role: membership.role,
})),
+ featureFlags: parseFlags(dbUser.featureFlags),
}
: null,
};
@@ -109,6 +112,7 @@ export const authOptions: NextAuthOptions = {
email: dbUser.email,
image: dbUser.image,
emailVerified: dbUser.emailVerified,
+ featureFlags: parseFlags(dbUser.featureFlags),
projects: [],
};
diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts
index ab81eba378..46acf8e5af 100644
--- a/types/next-auth.d.ts
+++ b/types/next-auth.d.ts
@@ -4,6 +4,7 @@ import {
type Membership as PrismaMembership,
type Project as PrismaProject,
} from "@prisma/client";
+import { type Flags } from "@/src/features/featureFlags/types";
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
@@ -27,6 +28,7 @@ declare module "next-auth" {
name: PrismaProject["name"];
role: PrismaMembership["role"];
}[];
+ featureFlags: Flags;
}
}