🪢 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(mixpanel): add project name to Mixpanel integration events (#12038)
shmulik-apiirobce4ce652125
diff --git a/packages/shared/src/server/analytics-integrations/types.ts b/packages/shared/src/server/analytics-integrations/types.tsindex 53474b7b62..1651fad128 100644--- a/packages/shared/src/server/analytics-integrations/types.ts+++ b/packages/shared/src/server/analytics-integrations/types.ts@@ -11,6 +11,7 @@ export type AnalyticsTraceEvent = {langfuse_count_observations?: unknown;langfuse_session_id?: unknown;langfuse_project_id?: unknown;+ langfuse_project_name?: unknown;langfuse_user_id?: unknown;langfuse_latency?: unknown;langfuse_release?: unknown;@@ -36,6 +37,7 @@ export type AnalyticsGenerationEvent = {langfuse_total_units?: unknown;langfuse_session_id?: unknown;langfuse_project_id?: unknown;+ langfuse_project_name?: unknown;langfuse_user_id?: unknown;langfuse_latency?: unknown;langfuse_time_to_first_token?: unknown;@@ -64,6 +66,7 @@ export type AnalyticsScoreEvent = {langfuse_user_url?: unknown;langfuse_session_id?: unknown;langfuse_project_id?: unknown;+ langfuse_project_name?: unknown;langfuse_user_id?: unknown;langfuse_release?: unknown;langfuse_tags?: unknown;@@ -89,6 +92,7 @@ export type AnalyticsObservationEvent = {langfuse_total_units?: unknown;langfuse_session_id?: unknown;langfuse_project_id?: unknown;+ langfuse_project_name?: unknown;langfuse_user_id?: unknown;langfuse_latency?: unknown;langfuse_time_to_first_token?: unknown;diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.tsindex 380c973f3b..55a46bcda1 100644--- a/packages/shared/src/server/repositories/events.ts+++ b/packages/shared/src/server/repositories/events.ts@@ -2482,6 +2482,7 @@ export const getEventsForBlobStorageExport = function (*/export const getEventsForAnalyticsIntegrations = async function* (projectId: string,+ projectName: string,minTimestamp: Date,maxTimestamp: Date,) {@@ -2543,6 +2544,7 @@ export const getEventsForAnalyticsIntegrations = async function* (langfuse_total_units: record.total_tokens,langfuse_session_id: record.session_id,langfuse_project_id: projectId,+ langfuse_project_name: projectName,langfuse_user_id: record.user_id || null,langfuse_latency: record.latency,langfuse_time_to_first_token: record.time_to_first_token,diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.tsindex 0a7a5f8eb6..4b7758297c 100644--- a/packages/shared/src/server/repositories/observations.ts+++ b/packages/shared/src/server/repositories/observations.ts@@ -1822,6 +1822,7 @@ export const getObservationsForBlobStorageExport = function (export const getGenerationsForAnalyticsIntegrations = async function* (projectId: string,+ projectName: string,minTimestamp: Date,maxTimestamp: Date,) {@@ -1902,6 +1903,7 @@ export const getGenerationsForAnalyticsIntegrations = async function* (langfuse_total_units: record.total_tokens,langfuse_session_id: record.trace_session_id,langfuse_project_id: projectId,+ langfuse_project_name: projectName,langfuse_user_id: record.trace_user_id || null,langfuse_latency: record.latency,langfuse_time_to_first_token: record.time_to_first_token,diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.tsindex fbddc276d5..94ccd14647 100644--- a/packages/shared/src/server/repositories/scores.ts+++ b/packages/shared/src/server/repositories/scores.ts@@ -1575,6 +1575,7 @@ export const getScoresForBlobStorageExport = function (export const getScoresForAnalyticsIntegrations = async function* (projectId: string,+ projectName: string,minTimestamp: Date,maxTimestamp: Date,) {@@ -1671,6 +1672,7 @@ export const getScoresForAnalyticsIntegrations = async function* (langfuse_id: record.id,langfuse_session_id: effectiveSessionId,langfuse_project_id: projectId,+ langfuse_project_name: projectName,langfuse_user_id: record.trace_user_id || null,langfuse_release: record.trace_release,langfuse_tags: record.trace_tags,diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.tsindex 2964249075..ee286b279f 100644--- a/packages/shared/src/server/repositories/traces.ts+++ b/packages/shared/src/server/repositories/traces.ts@@ -1375,6 +1375,7 @@ export const getTracesForBlobStorageExport = function (export const getTracesForAnalyticsIntegrations = async function* (projectId: string,+ projectName: string,minTimestamp: Date,maxTimestamp: Date,) {@@ -1453,6 +1454,7 @@ export const getTracesForAnalyticsIntegrations = async function* (langfuse_count_observations: record.observation_count,langfuse_session_id: record.session_id,langfuse_project_id: projectId,+ langfuse_project_name: projectName,langfuse_user_id: record.user_id || null,langfuse_latency: record.latency,langfuse_release: record.release,diff --git a/worker/src/__tests__/mixpanelTransformers.test.ts b/worker/src/__tests__/mixpanelTransformers.test.tsindex 5ef721159a..fd96d6787d 100644--- a/worker/src/__tests__/mixpanelTransformers.test.ts+++ b/worker/src/__tests__/mixpanelTransformers.test.ts@@ -32,6 +32,7 @@ describe("Mixpanel transformers", () => {langfuse_total_units: 150,langfuse_session_id: "session-abc",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: "user-789",langfuse_latency: 1.5,langfuse_time_to_first_token: 0.3,@@ -72,6 +73,7 @@ describe("Mixpanel transformers", () => {timestamp: new Date("2024-01-15T10:00:00Z"),langfuse_observation_name: "anonymous-event",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: null,langfuse_event_version: "1.0.0",posthog_session_id: null,@@ -94,6 +96,7 @@ describe("Mixpanel transformers", () => {timestamp: new Date("2024-01-15T10:00:00Z"),langfuse_observation_name: "consistent-event",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: null,langfuse_event_version: "1.0.0",posthog_session_id: null,@@ -113,6 +116,7 @@ describe("Mixpanel transformers", () => {langfuse_observation_name: "session-event",langfuse_session_id: "langfuse-session-123",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: "user-456",langfuse_event_version: "1.0.0",posthog_session_id: null,@@ -131,6 +135,7 @@ describe("Mixpanel transformers", () => {langfuse_observation_name: "session-event",langfuse_session_id: "langfuse-session-123",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: "user-456",langfuse_event_version: "1.0.0",posthog_session_id: "posthog-session-789",@@ -141,6 +146,26 @@ describe("Mixpanel transformers", () => {expect(result.properties.session_id).toBe("mixpanel-session-456");});++ it("should include langfuse_project_name in properties", () => {+ const event: AnalyticsObservationEvent = {+ langfuse_id: "event-with-project-name",+ timestamp: new Date("2024-01-15T10:00:00Z"),+ langfuse_observation_name: "test-event",+ langfuse_project_id: projectId,+ langfuse_project_name: "My Custom Project Name",+ langfuse_user_id: "user-123",+ langfuse_event_version: "1.0.0",+ posthog_session_id: null,+ mixpanel_session_id: null,+ };++ const result = transformEventForMixpanel(event, projectId);++ expect(result.properties.langfuse_project_name).toBe(+ "My Custom Project Name",+ );+ });});describe("transformTraceForMixpanel", () => {@@ -155,6 +180,7 @@ describe("Mixpanel transformers", () => {langfuse_count_observations: 5,langfuse_session_id: "session-abc",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: "user-789",langfuse_latency: 2.5,langfuse_release: "v1.0.0",@@ -192,6 +218,7 @@ describe("Mixpanel transformers", () => {langfuse_total_units: 300,langfuse_session_id: "session-abc",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: "user-789",langfuse_latency: 1.2,langfuse_time_to_first_token: 0.2,@@ -232,6 +259,7 @@ describe("Mixpanel transformers", () => {langfuse_user_url: "https://langfuse.com/project/test/users/user-789",langfuse_session_id: "session-abc",langfuse_project_id: projectId,+ langfuse_project_name: "Test Project",langfuse_user_id: "user-789",langfuse_release: "v1.0.0",langfuse_tags: ["human-eval"],diff --git a/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts b/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.tsindex af3f0f7a07..c8cc11982c 100644--- a/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts+++ b/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts@@ -21,6 +21,7 @@ import {type MixpanelExecutionConfig = {projectId: string;+ projectName: string;minTimestamp: Date;maxTimestamp: Date;decryptedMixpanelProjectToken: string;@@ -30,6 +31,7 @@ type MixpanelExecutionConfig = {const processMixpanelTraces = async (config: MixpanelExecutionConfig) => {const traces = getTracesForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -65,6 +67,7 @@ const processMixpanelTraces = async (config: MixpanelExecutionConfig) => {const processMixpanelGenerations = async (config: MixpanelExecutionConfig) => {const generations = getGenerationsForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -100,6 +103,7 @@ const processMixpanelGenerations = async (config: MixpanelExecutionConfig) => {const processMixpanelScores = async (config: MixpanelExecutionConfig) => {const scores = getScoresForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -135,6 +139,7 @@ const processMixpanelScores = async (config: MixpanelExecutionConfig) => {const processMixpanelEvents = async (config: MixpanelExecutionConfig) => {const events = getEventsForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -188,6 +193,11 @@ export const handleMixpanelIntegrationProjectJob = async (projectId,enabled: true,},+ include: {+ project: {+ select: { name: true },+ },+ },});if (!mixpanelIntegration) {@@ -197,9 +207,17 @@ export const handleMixpanelIntegrationProjectJob = async (return;}+ if (!mixpanelIntegration.project) {+ logger.warn(+ `[MIXPANEL] Project not found for Mixpanel integration ${projectId}`,+ );+ return;+ }+// Fetch relevant data and send it to Mixpanelconst executionConfig: MixpanelExecutionConfig = {projectId,+ projectName: mixpanelIntegration.project.name,// Start from 2000-01-01 if no lastSyncAt. Workaround because 1970-01-01 leads to subtle bugs in ClickHouseminTimestamp: mixpanelIntegration.lastSyncAt || new Date("2000-01-01"),maxTimestamp: new Date(new Date().getTime() - 30 * 60 * 1000), // 30 minutes agodiff --git a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.tsindex 9a6c8e98d3..e259d2ac9d 100644--- a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts+++ b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts@@ -22,6 +22,7 @@ import { PostHog } from "posthog-node";type PostHogExecutionConfig = {projectId: string;+ projectName: string;minTimestamp: Date;maxTimestamp: Date;decryptedPostHogApiKey: string;@@ -35,6 +36,7 @@ const postHogSettings = {const processPostHogTraces = async (config: PostHogExecutionConfig) => {const traces = getTracesForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -81,6 +83,7 @@ const processPostHogTraces = async (config: PostHogExecutionConfig) => {const processPostHogGenerations = async (config: PostHogExecutionConfig) => {const generations = getGenerationsForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -127,6 +130,7 @@ const processPostHogGenerations = async (config: PostHogExecutionConfig) => {const processPostHogScores = async (config: PostHogExecutionConfig) => {const scores = getScoresForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -173,6 +177,7 @@ const processPostHogScores = async (config: PostHogExecutionConfig) => {const processPostHogEvents = async (config: PostHogExecutionConfig) => {const events = getEventsForAnalyticsIntegrations(config.projectId,+ config.projectName,config.minTimestamp,config.maxTimestamp,);@@ -236,6 +241,11 @@ export const handlePostHogIntegrationProjectJob = async (projectId,enabled: true,},+ include: {+ project: {+ select: { name: true },+ },+ },});if (!postHogIntegration) {@@ -245,6 +255,13 @@ export const handlePostHogIntegrationProjectJob = async (return;}+ if (!postHogIntegration.project) {+ logger.warn(+ `[POSTHOG] Project not found for PostHog integration ${projectId}`,+ );+ return;+ }+// Validate PostHog hostname to prevent SSRF attacks before sending datatry {await validateWebhookURL(postHogIntegration.posthogHostName);@@ -260,6 +277,7 @@ export const handlePostHogIntegrationProjectJob = async (// Fetch relevant data and send it to PostHogconst executionConfig: PostHogExecutionConfig = {projectId,+ projectName: postHogIntegration.project.name,// Start from 2000-01-01 if no lastSyncAt. Workaround because 1970-01-01 leads to subtle bugs in ClickHouseminTimestamp: postHogIntegration.lastSyncAt || new Date("2000-01-01"),maxTimestamp: new Date(new Date().getTime() - 30 * 60 * 1000), // 30 minutes ago