🪢 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
feat(cloud): add settings for posthog integration (#1778)
Marc Klingen08e576f8154b
diff --git a/ee/src/env.ts b/ee/src/env.tsindex 9c1ffc2789..74d23d4768 100644--- a/ee/src/env.ts+++ b/ee/src/env.ts@@ -1,15 +1,9 @@import { z } from "zod";+import { env as sharedEnv } from "@langfuse/shared";const EnvSchema = z.object({NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: z.string().optional(),- ENCRYPTION_KEY: z- .string()- .length(- 64,- "ENCRYPTION_KEY must be 256 bits, 64 string characters in hex format, generate via: openssl rand -hex 32"- )- .optional(),ADMIN_API_KEY: z.string().optional(),});-export const env = EnvSchema.parse(process.env);+export const env = { ...sharedEnv, ...EnvSchema.parse(process.env) };diff --git a/ee/src/sso/index.ts b/ee/src/sso/index.tsindex a866c16dfd..870ef2bfd1 100644--- a/ee/src/sso/index.ts+++ b/ee/src/sso/index.ts@@ -6,8 +6,8 @@ import Auth0Provider from "next-auth/providers/auth0";import AzureADProvider from "next-auth/providers/azure-ad";import { isEeAvailable } from "..";import { prisma } from "@langfuse/shared/src/db";+import { encrypt, decrypt } from "@langfuse/shared/encryption";import { SsoProviderSchema } from "./types";-import { decrypt, encrypt } from "../encryption";import { type NextApiRequest, type NextApiResponse } from "next";import { env } from "../env";diff --git a/packages/shared/package.json b/packages/shared/package.jsonindex b2f8be7f36..ae9f48d438 100644--- a/packages/shared/package.json+++ b/packages/shared/package.json@@ -20,6 +20,10 @@"./src/server/auth": {"import": "./dist/src/server/auth.js","require": "./dist/src/server/auth.js"+ },+ "./encryption": {+ "import": "./dist/src/encryption/index.js",+ "require": "./dist/src/encryption/index.js"}},"scripts": {@@ -28,6 +32,7 @@"lint": "eslint . --ext .js,.jsx,.ts,.tsx","lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix","db:migrate": "DISABLE_ERD=false dotenv -e ../../.env -- npx prisma migrate dev",+ "db:push": "DISABLE_ERD=false dotenv -e ../../.env -- npx prisma db push","db:reset": "dotenv -e ../../.env npx -- prisma migrate reset","db:deploy": "dotenv -e ../../.env npx -- prisma migrate deploy","db:seed": "dotenv -e ../../.env -- npx prisma db seed",diff --git a/packages/shared/prisma/generated/types.ts b/packages/shared/prisma/generated/types.tsindex 716318834b..1d4c242db5 100644--- a/packages/shared/prisma/generated/types.ts+++ b/packages/shared/prisma/generated/types.ts@@ -292,6 +292,13 @@ export type ObservationView = {calculated_total_cost: string | null;latency: number | null;};+export type PosthogIntegration = {+ project_id: string;+ encrypted_posthog_api_key: string;+ posthog_host_name: string;+ last_sync_at: Timestamp | null;+ enabled: boolean;+};export type Pricing = {id: string;model_name: string;@@ -422,6 +429,7 @@ export type DB = {models: Model;observations: Observation;observations_view: ObservationView;+ posthog_integrations: PosthogIntegration;pricings: Pricing;projects: Project;prompts: Prompt;diff --git a/packages/shared/prisma/migrations/20240419152924_posthog_integration_settings/migration.sql b/packages/shared/prisma/migrations/20240419152924_posthog_integration_settings/migration.sqlnew file mode 100644index 0000000000..a70ae25abf--- /dev/null+++ b/packages/shared/prisma/migrations/20240419152924_posthog_integration_settings/migration.sql@@ -0,0 +1,16 @@+-- CreateTable+CREATE TABLE "posthog_integrations" (+ "project_id" TEXT NOT NULL,+ "encrypted_posthog_api_key" TEXT NOT NULL,+ "posthog_host_name" TEXT NOT NULL,+ "last_sync_at" TIMESTAMP(3),+ "enabled" BOOLEAN NOT NULL,++ CONSTRAINT "posthog_integrations_pkey" PRIMARY KEY ("project_id")+);++-- CreateIndex+CREATE INDEX "posthog_integrations_project_id_idx" ON "posthog_integrations"("project_id");++-- AddForeignKey+ALTER TABLE "posthog_integrations" ADD CONSTRAINT "posthog_integrations_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects"("id") ON DELETE CASCADE ON UPDATE CASCADE;diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prismaindex e583807593..efe7c9ed3e 100644--- a/packages/shared/prisma/schema.prisma+++ b/packages/shared/prisma/schema.prisma@@ -92,25 +92,26 @@ model VerificationToken {}model Project {- id String @id @default(cuid())- createdAt DateTime @default(now()) @map("created_at")- updatedAt DateTime @default(now()) @updatedAt @map("updated_at")- name String- cloudConfig Json? @map("cloud_config") // Langfuse Cloud, for zod schema see projectsRouter.ts- members Membership[]- traces Trace[]- observations Observation[]- apiKeys ApiKey[]- dataset Dataset[]- RawEvents Events[]- invitations MembershipInvitation[]- sessions TraceSession[]- Prompt Prompt[]- Model Model[]- AuditLog AuditLog[]- EvalTemplate EvalTemplate[]- JobConfiguration JobConfiguration[]- JobExecution JobExecution[]+ id String @id @default(cuid())+ createdAt DateTime @default(now()) @map("created_at")+ updatedAt DateTime @default(now()) @updatedAt @map("updated_at")+ name String+ cloudConfig Json? @map("cloud_config") // Langfuse Cloud, for zod schema see projectsRouter.ts+ members Membership[]+ traces Trace[]+ observations Observation[]+ apiKeys ApiKey[]+ dataset Dataset[]+ RawEvents Events[]+ invitations MembershipInvitation[]+ sessions TraceSession[]+ Prompt Prompt[]+ Model Model[]+ AuditLog AuditLog[]+ EvalTemplate EvalTemplate[]+ JobConfiguration JobConfiguration[]+ JobExecution JobExecution[]+ PosthogIntegration PosthogIntegration[]@@map("projects")}@@ -699,3 +700,15 @@ model SsoConfig {@@map("sso_configs")}++model PosthogIntegration {+ projectId String @id @map("project_id")+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)+ encryptedPosthogApiKey String @map("encrypted_posthog_api_key")+ posthogHostName String @map("posthog_host_name")+ lastSyncAt DateTime? @map("last_sync_at")+ enabled Boolean++ @@index([projectId])+ @@map("posthog_integrations")+}diff --git a/ee/src/encryption/index.ts b/packages/shared/src/encryption/index.tssimilarity index 100%rename from ee/src/encryption/index.tsrename to packages/shared/src/encryption/index.tsdiff --git a/packages/shared/src/env.ts b/packages/shared/src/env.tsnew file mode 100644index 0000000000..0049d632d5--- /dev/null+++ b/packages/shared/src/env.ts@@ -0,0 +1,13 @@+import { z } from "zod";++const EnvSchema = z.object({+ ENCRYPTION_KEY: z+ .string()+ .length(+ 64,+ "ENCRYPTION_KEY must be 256 bits, 64 string characters in hex format, generate via: openssl rand -hex 32"+ )+ .optional(),+});++export const env = EnvSchema.parse(process.env);diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.tsindex 8c9ed363f4..b115cd1ebd 100644--- a/packages/shared/src/index.ts+++ b/packages/shared/src/index.ts@@ -9,6 +9,7 @@ export * from "./filterToPrisma";export * from "./tracesTable";export * from "./server/auth";export * from "./observationsTable";+export { env } from "./env";// llm apiexport * from "./server/llm/types";diff --git a/web/src/components/layouts/header.tsx b/web/src/components/layouts/header.tsxindex 46518fc455..62e1584b3c 100644--- a/web/src/components/layouts/header.tsx+++ b/web/src/components/layouts/header.tsx@@ -18,6 +18,7 @@ export default function Header({featureBetaURL?: string;actionButtons?: React.ReactNode;level?: "h2" | "h3";+ className?: string;}) {const router = useRouter();const session = useSession();@@ -42,7 +43,7 @@ export default function Header({[...props.breadcrumb.map((i) => i.href).filter(Boolean)].pop();return (- <div className={cn(level === "h2" ? "mb-4" : "mb-2")}>+ <div className={cn(level === "h2" ? "mb-4" : "mb-2", props.className)}><div>{backHref ? (<nav className="sm:hidden" aria-label="Back">diff --git a/web/src/features/audit-logs/auditLog.ts b/web/src/features/audit-logs/auditLog.tsindex f0981f143f..1e4237e03d 100644--- a/web/src/features/audit-logs/auditLog.ts+++ b/web/src/features/audit-logs/auditLog.ts@@ -15,7 +15,8 @@ export type AuditableResource =| "session"| "apiKey"| "evalTemplate"- | "job";+ | "job"+ | "posthogIntegration";type AuditLog = {resourceType: AuditableResource;diff --git a/web/src/features/posthog-integration/posthog-integration-router.ts b/web/src/features/posthog-integration/posthog-integration-router.tsnew file mode 100644index 0000000000..172f2dc197--- /dev/null+++ b/web/src/features/posthog-integration/posthog-integration-router.ts@@ -0,0 +1,117 @@+import { z } from "zod";++import { auditLog } from "@/src/features/audit-logs/auditLog";+import { throwIfNoAccess } from "@/src/features/rbac/utils/checkAccess";+import {+ createTRPCRouter,+ protectedProjectProcedure,+} from "@/src/server/api/trpc";+import { decrypt, encrypt } from "@langfuse/shared/encryption";+import { posthogIntegrationFormSchema } from "@/src/features/posthog-integration/types";+import { TRPCError } from "@trpc/server";++export const posthogIntegrationRouter = createTRPCRouter({+ get: protectedProjectProcedure+ .input(z.object({ projectId: z.string() }))+ .query(async ({ input, ctx }) => {+ throwIfNoAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "integrations:CRUD",+ });+ try {+ const dbConfig = await ctx.prisma.posthogIntegration.findFirst({+ where: {+ projectId: input.projectId,+ },+ });++ if (!dbConfig) {+ return null;+ }++ const { encryptedPosthogApiKey, ...config } = dbConfig;++ return {+ ...config,+ posthogApiKey: decrypt(encryptedPosthogApiKey),+ };+ } catch (e) {+ console.error("posthog integration get", e);+ throw new TRPCError({+ code: "INTERNAL_SERVER_ERROR",+ });+ }+ }),++ update: protectedProjectProcedure+ .input(posthogIntegrationFormSchema.extend({ projectId: z.string() }))+ .mutation(async ({ input, ctx }) => {+ try {+ throwIfNoAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "integrations:CRUD",+ });+ await auditLog({+ session: ctx.session,+ action: "update",+ resourceType: "posthogIntegration",+ resourceId: input.projectId,+ });+ const { posthogProjectApiKey, ...config } = input;++ const encryptedPosthogApiKey = encrypt(posthogProjectApiKey);++ await ctx.prisma.posthogIntegration.upsert({+ where: {+ projectId: input.projectId,+ },+ create: {+ projectId: input.projectId,+ posthogHostName: config.posthogHostname,+ encryptedPosthogApiKey,+ enabled: config.enabled,+ },+ update: {+ encryptedPosthogApiKey,+ posthogHostName: config.posthogHostname,+ enabled: config.enabled,+ },+ });+ } catch (e) {+ console.log("posthog integration update", e);+ throw new TRPCError({+ code: "INTERNAL_SERVER_ERROR",+ });+ }+ }),+ delete: protectedProjectProcedure+ .input(z.object({ projectId: z.string() }))+ .mutation(async ({ input, ctx }) => {+ try {+ throwIfNoAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "integrations:CRUD",+ });+ await auditLog({+ session: ctx.session,+ action: "delete",+ resourceType: "posthogIntegration",+ resourceId: input.projectId,+ });++ await ctx.prisma.posthogIntegration.delete({+ where: {+ projectId: input.projectId,+ },+ });+ } catch (e) {+ console.log("posthog integration delete", e);+ throw new TRPCError({+ code: "INTERNAL_SERVER_ERROR",+ });+ }+ }),+});diff --git a/web/src/features/posthog-integration/types.ts b/web/src/features/posthog-integration/types.tsnew file mode 100644index 0000000000..4720d34b45--- /dev/null+++ b/web/src/features/posthog-integration/types.ts@@ -0,0 +1,9 @@+import { z } from "zod";++export const posthogIntegrationFormSchema = z.object({+ posthogHostname: z.string().url(),+ posthogProjectApiKey: z.string().refine((v) => v.startsWith("phc_"), {+ message: "PostHog Project API Key must start with 'phc_'",+ }),+ enabled: z.boolean(),+});diff --git a/web/src/features/rbac/constants/roleAccessRights.ts b/web/src/features/rbac/constants/roleAccessRights.tsindex b63696275c..e938f0045b 100644--- a/web/src/features/rbac/constants/roleAccessRights.ts+++ b/web/src/features/rbac/constants/roleAccessRights.ts@@ -20,6 +20,7 @@ const scopes = ["project:delete","project:update","project:transfer",+ "integrations:CRUD","datasets:CUD",@@ -45,6 +46,7 @@ export const roleAccessRights: Record<MembershipRole, Scope[]> = {"apiKeys:read","apiKeys:create","apiKeys:delete",+ "integrations:CRUD","objects:publish","objects:bookmark","objects:tag",@@ -70,6 +72,7 @@ export const roleAccessRights: Record<MembershipRole, Scope[]> = {"apiKeys:read","apiKeys:create","apiKeys:delete",+ "integrations:CRUD","objects:publish","objects:bookmark","objects:tag",diff --git a/web/src/pages/project/[projectId]/settings.tsx b/web/src/pages/project/[projectId]/settings.tsxindex fb1dd9fd52..f8407e7157 100644--- a/web/src/pages/project/[projectId]/settings.tsx+++ b/web/src/pages/project/[projectId]/settings.tsx@@ -15,10 +15,6 @@ import { env } from "@/src/env.mjs";import { Card } from "@tremor/react";import { Button } from "@/src/components/ui/button";import Link from "next/link";-import {- sendUserChatMessage,- showAgentChatMessage,-} from "@/src/features/support-chat/chat";export default function SettingsPage() {const router = useRouter();@@ -31,7 +27,7 @@ export default function SettingsPage() {<ApiKeyList projectId={projectId} /><ProjectMembersTable projectId={projectId} /><ProjectUsageChart projectId={projectId} />- <Beta />+ <Integrations projectId={projectId} /><Instructions /><RenameProject projectId={projectId} /><div className="space-y-3">@@ -136,12 +132,12 @@ function Instructions() {);}-const Beta = () => {+const Integrations = (props: { projectId: string }) => {if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION === undefined) return null;return (<div>- <Header title="Early Access" level="h3" />+ <Header title="Integrations" level="h3" /><Card className="p-4 lg:w-1/2">{/* eslint-disable-next-line @next/next/no-img-element */}<img@@ -154,16 +150,12 @@ const Beta = () => {Langfuse Events/Metrics available in your Posthog Dashboards.</p><div className="flex items-center gap-2">- <Button- variant="secondary"- onClick={() => {- sendUserChatMessage(- "I am interested to join the PostHog Integration Beta",- );- showAgentChatMessage("We'll be in touch to get you set up!");- }}- >- Get Access+ <Button variant="secondary" asChild>+ <Link+ href={`/project/${props.projectId}/settings/posthog-integration`}+ >+ Configure+ </Link></Button><Button asChild variant="ghost"><Link href="https://langfuse.com/docs/analytics/posthog">diff --git a/web/src/pages/project/[projectId]/settings/posthog-integration.tsx b/web/src/pages/project/[projectId]/settings/posthog-integration.tsxnew file mode 100644index 0000000000..6b0fe0d28b--- /dev/null+++ b/web/src/pages/project/[projectId]/settings/posthog-integration.tsx@@ -0,0 +1,222 @@+import Header from "@/src/components/layouts/header";+import { Button } from "@/src/components/ui/button";+import {+ Form,+ FormControl,+ FormDescription,+ FormField,+ FormItem,+ FormLabel,+ FormMessage,+} from "@/src/components/ui/form";+import { Input } from "@/src/components/ui/input";+import { PasswordInput } from "@/src/components/ui/password-input";+import { Switch } from "@/src/components/ui/switch";+import { env } from "@/src/env.mjs";+import { posthogIntegrationFormSchema } from "@/src/features/posthog-integration/types";+import { api } from "@/src/utils/api";+import { type RouterOutput } from "@/src/utils/types";+import { zodResolver } from "@hookform/resolvers/zod";+import { Card } from "@tremor/react";+import Link from "next/link";+import { useRouter } from "next/router";+import posthog from "posthog-js";+import { useEffect } from "react";+import { useForm } from "react-hook-form";+import { type z } from "zod";++export default function PosthogIntegrationSettings() {+ const router = useRouter();+ const projectId = router.query.projectId as string;+ const state = api.posthogIntegration.get.useQuery({ projectId });++ if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION === undefined) return null;++ return (+ <div className="md:container">+ <Header+ title="PostHog Integration"+ breadcrumb={[+ { name: "Settings", href: `/project/${projectId}/settings` },+ ]}+ actionButtons={+ <Button asChild variant="secondary">+ <Link href="https://langfuse.com/docs/analytics/posthog">+ Integration Docs+ </Link>+ </Button>+ }+ status={state.data?.enabled ? "active" : "inactive"}+ />+ <p className="mb-4 text-sm text-gray-700">+ We have teamed up with{" "}+ <Link href="https://posthog.com" className="underline">+ PostHog+ </Link>{" "}+ (OSS product analytics) to make Langfuse events/metrics available in+ your Posthog Dashboards. While in Beta, this integration syncs metrics+ on a daily schedule to PostHog. When first activated, it will sync all+ historical data from the beginning of your project.+ </p>+ <div className="flex flex-col gap-10"></div>++ {!state.isInitialLoading && (+ <>+ <Header level="h3" title="Configuration" />+ <Card className="p-4">+ <PostHogIntegrationSettings+ state={state.data}+ projectId={projectId}+ />+ </Card>+ </>+ )}+ {state.data?.enabled && (+ <>+ <Header level="h3" title="Status" className="mt-8" />+ <p className="text-sm text-gray-700">+ Data synced until:{" "}+ {state.data?.lastSyncAt+ ? new Date(state.data.lastSyncAt).toLocaleString()+ : "Never (pending)"}+ </p>+ <p className="mt-2 text-sm text-gray-700">+ While in Beta, the sync is scheduled to run once a day.+ </p>+ </>+ )}+ </div>+ );+}++const PostHogIntegrationSettings = ({+ state,+ projectId,+}: {+ state?: RouterOutput["posthogIntegration"]["get"];+ projectId: string;+}) => {+ const posthogForm = useForm<z.infer<typeof posthogIntegrationFormSchema>>({+ resolver: zodResolver(posthogIntegrationFormSchema),+ defaultValues: {+ posthogHostname: state?.posthogHostName ?? "",+ posthogProjectApiKey: state?.posthogApiKey ?? "",+ enabled: state?.enabled ?? false,+ },+ });++ useEffect(() => {+ posthogForm.reset({+ posthogHostname: state?.posthogHostName ?? "",+ posthogProjectApiKey: state?.posthogApiKey ?? "",+ enabled: state?.enabled ?? false,+ });+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [state]);++ const utils = api.useUtils();+ const mut = api.posthogIntegration.update.useMutation({+ onSuccess: () => {+ utils.posthogIntegration.invalidate();+ },+ });+ const mutDelete = api.posthogIntegration.delete.useMutation({+ onSuccess: () => {+ utils.posthogIntegration.invalidate();+ },+ });++ async function onSubmit(+ values: z.infer<typeof posthogIntegrationFormSchema>,+ ) {+ posthog.capture("integrations:posthog_form_submitted");+ mut.mutate({+ projectId,+ ...values,+ });+ console.log(values);+ }++ return (+ <Form {...posthogForm}>+ <form+ className="space-y-3"+ // eslint-disable-next-line @typescript-eslint/no-misused-promises+ onSubmit={posthogForm.handleSubmit(onSubmit)}+ >+ <FormField+ control={posthogForm.control}+ name="posthogHostname"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Posthog Hostname</FormLabel>+ <FormControl>+ <Input {...field} />+ </FormControl>+ <FormDescription>+ US region: https://us.posthog.com; EU region:+ https://eu.posthog.com+ </FormDescription>+ <FormMessage />+ </FormItem>+ )}+ />+ <FormField+ control={posthogForm.control}+ name="posthogProjectApiKey"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Posthog Project API Key</FormLabel>+ <FormControl>+ <PasswordInput {...field} />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ <FormField+ control={posthogForm.control}+ name="enabled"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Enabled</FormLabel>+ <FormControl>+ <Switch+ id="posthog-integration-enabled"+ checked={field.value}+ onCheckedChange={() => {+ field.onChange(!field.value);+ }}+ className="ml-4 mt-1"+ />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ </form>+ <div className="mt-8 flex gap-2">+ <Button+ loading={mut.isLoading}+ onClick={posthogForm.handleSubmit(onSubmit)}+ >+ Save+ </Button>+ <Button+ variant="ghost"+ loading={mutDelete.isLoading}+ onClick={() => {+ if (+ confirm(+ "Are you sure you want to reset the PostHog integration for this project?",+ )+ )+ mutDelete.mutate({ projectId });+ }}+ >+ Reset+ </Button>+ </div>+ </Form>+ );+};diff --git a/web/src/server/api/root.ts b/web/src/server/api/root.tsindex 939b2db9a0..4bb1db549e 100644--- a/web/src/server/api/root.ts+++ b/web/src/server/api/root.ts@@ -15,6 +15,7 @@ import { sessionRouter } from "@/src/server/api/routers/sessions";import { promptRouter } from "@/src/features/prompts/server/prompt-router";import { modelRouter } from "@/src/server/api/routers/models";import { evalRouter } from "@/src/features/evals/server/router";+import { posthogIntegrationRouter } from "@/src/features/posthog-integration/posthog-integration-router";/*** This is the primary router for your server.@@ -38,6 +39,7 @@ export const appRouter = createTRPCRouter({prompts: promptRouter,models: modelRouter,evals: evalRouter,+ posthogIntegration: posthogIntegrationRouter,});// export type definition of API