Skip to content

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

fix(worker): cap Mixpanel export window at next UTC day boundary (#18347)

Niklas Semmlerde6b71c4aad6
diff --git a/worker/src/__tests__/mixpanelIntegrationProjectJob.test.ts b/worker/src/__tests__/mixpanelIntegrationProjectJob.test.ts
index a631f40d57..fc6815ee0b 100644
--- a/worker/src/__tests__/mixpanelIntegrationProjectJob.test.ts
+++ b/worker/src/__tests__/mixpanelIntegrationProjectJob.test.ts
@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach, vi } from "vitest";
+import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
/**
* Regression tests for the Mixpanel analytics export throttling fix (issue #12786).
@@ -52,7 +52,7 @@ const h = vi.hoisted(() => {
mixpanelRegion: "api",
encryptedMixpanelProjectToken: "enc",
lastSyncAt: new Date("2024-01-01"),
- project: { name: "Test Project" },
+ project: { name: "Test Project", createdAt: new Date("2023-06-15") },
});
// Mutable row returned by the prisma findFirst mock so individual tests can
@@ -143,6 +143,7 @@ import {
MIXPANEL_INTEGRATION_CUSTOMER_FAULT_METRIC,
} from "../features/mixpanel/handleMixpanelIntegrationProjectJob";
import {
+ getEventsForAnalyticsIntegrations,
getScoresForAnalyticsIntegrations,
recordIncrement,
} from "@langfuse/shared/src/server";
@@ -363,3 +364,85 @@ describe("handleMixpanelIntegrationProjectJob customer-fault observability", ()
expect(recordIncrement).not.toHaveBeenCalled();
});
});
+
+describe("handleMixpanelIntegrationProjectJob sync window", () => {
+ const now = new Date("2026-10-06T12:00:00Z");
+
+ beforeEach(() => {
+ vi.useFakeTimers({ toFake: ["Date"] });
+ vi.setSystemTime(now);
+ h.mixpanelIntegrationUpdate.mockClear();
+ vi.mocked(getEventsForAnalyticsIntegrations).mockClear();
+ h.db.integration = h.defaultIntegration();
+ (env as any).LANGFUSE_MIGRATION_V4_WRITE_MODE = "dual";
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it("caps a lagging sync at the next UTC day boundary", async () => {
+ h.db.integration = {
+ ...h.defaultIntegration(),
+ lastSyncAt: new Date("2026-10-01T15:30:00Z"),
+ };
+
+ await handleMixpanelIntegrationProjectJob(makeJob());
+
+ const dayBoundary = new Date("2026-10-02T00:00:00Z");
+ expect(vi.mocked(getEventsForAnalyticsIntegrations)).toHaveBeenCalledWith(
+ "project-1",
+ "Test Project",
+ new Date("2026-10-01T15:30:00Z"),
+ dayBoundary,
+ );
+ expect(h.mixpanelIntegrationUpdate).toHaveBeenCalledWith(
+ expect.objectContaining({
+ data: { lastSyncAt: dayBoundary },
+ }),
+ );
+ });
+
+ it("advances a caught-up sync to 30 minutes ago", async () => {
+ h.db.integration = {
+ ...h.defaultIntegration(),
+ lastSyncAt: new Date("2026-10-06T10:00:00Z"),
+ };
+
+ await handleMixpanelIntegrationProjectJob(makeJob());
+
+ expect(h.mixpanelIntegrationUpdate).toHaveBeenCalledWith(
+ expect.objectContaining({
+ data: {
+ lastSyncAt: new Date("2026-10-06T11:30:00Z"),
+ backfill: false,
+ },
+ }),
+ );
+ });
+
+ it("starts the first sync at the project's creation date", async () => {
+ h.db.integration = { ...h.defaultIntegration(), lastSyncAt: null };
+
+ await handleMixpanelIntegrationProjectJob(makeJob());
+
+ expect(vi.mocked(getEventsForAnalyticsIntegrations)).toHaveBeenCalledWith(
+ "project-1",
+ "Test Project",
+ new Date("2023-06-15T00:00:00Z"),
+ new Date("2023-06-16T00:00:00Z"),
+ );
+ });
+
+ it("skips the run without exporting when the window is empty", async () => {
+ h.db.integration = {
+ ...h.defaultIntegration(),
+ lastSyncAt: new Date("2026-10-06T11:45:00Z"),
+ };
+
+ await handleMixpanelIntegrationProjectJob(makeJob());
+
+ expect(vi.mocked(getEventsForAnalyticsIntegrations)).not.toHaveBeenCalled();
+ expect(h.mixpanelIntegrationUpdate).not.toHaveBeenCalled();
+ });
+});
diff --git a/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts b/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts
index afedbac6f6..5cc005f5aa 100644
--- a/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts
+++ b/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts
@@ -231,7 +231,7 @@ export const handleMixpanelIntegrationProjectJob = async (
},
include: {
project: {
- select: { name: true },
+ select: { name: true, createdAt: true },
},
},
});
@@ -253,13 +253,45 @@ export const handleMixpanelIntegrationProjectJob = async (
const runStartTime = new Date();
try {
+ // Resume from lastSyncAt. On first run, fall back to the project's
+ // createdAt since no trace data can precede it.
+ const minTimestamp =
+ mixpanelIntegration.lastSyncAt || mixpanelIntegration.project.createdAt;
+ const uncappedMaxTimestamp = new Date(Date.now() - 30 * 60 * 1000); // 30 minutes ago
+
+ // Cap maxTimestamp at the next UTC day boundary after minTimestamp so a
+ // lagging integration advances one day per run instead of re-reading an
+ // ever-growing window until the ClickHouse query times out. Healthy
+ // integrations are unaffected because uncappedMaxTimestamp wins whenever
+ // the sync is within one day of present.
+ const nextDayBoundary = new Date(
+ Date.UTC(
+ minTimestamp.getUTCFullYear(),
+ minTimestamp.getUTCMonth(),
+ minTimestamp.getUTCDate() + 1,
+ ),
+ );
+ const maxTimestamp = new Date(
+ Math.min(nextDayBoundary.getTime(), uncappedMaxTimestamp.getTime()),
+ );
+
+ if (maxTimestamp <= minTimestamp) {
+ logger.info(
+ `[MIXPANEL] Skipping Mixpanel integration for project ${projectId}: empty sync window (min: ${minTimestamp.toISOString()}, max: ${maxTimestamp.toISOString()})`,
+ );
+ return;
+ }
+
+ logger.info(
+ `[MIXPANEL] Syncing project ${projectId} from ${minTimestamp.toISOString()} to ${maxTimestamp.toISOString()}`,
+ );
+
// Fetch relevant data and send it to Mixpanel
const 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 ClickHouse
- minTimestamp: mixpanelIntegration.lastSyncAt || new Date("2000-01-01"),
- maxTimestamp: new Date(new Date().getTime() - 30 * 60 * 1000), // 30 minutes ago
+ minTimestamp,
+ maxTimestamp,
decryptedMixpanelProjectToken: decrypt(
mixpanelIntegration.encryptedMixpanelProjectToken,
),