🪢 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
fix(analytics): cache backend activity hourly (#16332)
Max Deichmann249b25734235
diff --git a/web/src/__tests__/server/unit/backendActivity.servertest.ts b/web/src/__tests__/server/unit/backendActivity.servertest.tsindex 05ace9e208..8d84de154f 100644--- a/web/src/__tests__/server/unit/backendActivity.servertest.ts+++ b/web/src/__tests__/server/unit/backendActivity.servertest.ts@@ -45,7 +45,7 @@ const createTracker = (};describe("backend activity tracking", () => {- it("captures a project-scoped activity event once per UTC day", async () => {+ it("captures a project-scoped activity event once per UTC hour", async () => {const { capture, setIfAbsent, tracker } = createTracker();await tracker({@@ -61,8 +61,8 @@ describe("backend activity tracking", () => {expect(setIfAbsent).toHaveBeenCalledOnce();expect(setIfAbsent).toHaveBeenCalledWith(- "langfuse:backend-activity:2026-08-10:user-1:project:project-1",- 172_800,+ "langfuse:backend-activity:v2:2026-08-10T12:user-1:project:project-1",+ 3_600,);expect(capture).toHaveBeenCalledOnce();expect(capture).toHaveBeenCalledWith({@@ -116,8 +116,8 @@ describe("backend activity tracking", () => {);});- it("captures the same project again on the next UTC day", async () => {- let now = new Date("2026-08-10T23:59:59.000Z");+ it("captures the same project again in the next UTC hour", async () => {+ let now = new Date("2026-08-10T12:59:59.000Z");const capture = vi.fn();const setIfAbsent = vi.fn().mockResolvedValue(true);const tracker = createBackendActivityTracker({@@ -132,7 +132,7 @@ describe("backend activity tracking", () => {organizationId: "org-1",projectId: "project-1",});- now = new Date("2026-08-11T00:00:01.000Z");+ now = new Date("2026-08-10T13:00:01.000Z");await tracker({userId: "user-1",organizationId: "org-1",@@ -142,13 +142,13 @@ describe("backend activity tracking", () => {expect(setIfAbsent).toHaveBeenCalledTimes(2);expect(setIfAbsent).toHaveBeenNthCalledWith(1,- "langfuse:backend-activity:2026-08-10:user-1:project:project-1",- 172_800,+ "langfuse:backend-activity:v2:2026-08-10T12:user-1:project:project-1",+ 3_600,);expect(setIfAbsent).toHaveBeenNthCalledWith(2,- "langfuse:backend-activity:2026-08-11:user-1:project:project-1",- 172_800,+ "langfuse:backend-activity:v2:2026-08-10T13:user-1:project:project-1",+ 3_600,);expect(capture).toHaveBeenCalledTimes(2);});diff --git a/web/src/features/posthog-analytics/server/backendActivity.ts b/web/src/features/posthog-analytics/server/backendActivity.tsindex 2bbf9e96dc..b9550fd054 100644--- a/web/src/features/posthog-analytics/server/backendActivity.ts+++ b/web/src/features/posthog-analytics/server/backendActivity.ts@@ -4,7 +4,7 @@ import { env } from "@/src/env.mjs";import { ServerPosthog } from "@/src/features/posthog-analytics/ServerPosthog";const BACKEND_ACTIVITY_EVENT = "backend:activity";-const DEDUPLICATION_TTL_SECONDS = 2 * 24 * 60 * 60;+const DEDUPLICATION_TTL_SECONDS = 60 * 60;const LOCAL_CACHE_MAX_ENTRIES = 100_000;type BackendActivity = {@@ -46,11 +46,12 @@ export const createBackendActivityTracker = ({const timestamp = now();const activityScope = projectId ? "project" : "organization";const scopeId = projectId ?? organizationId;- const utcDate = timestamp.toISOString().slice(0, 10);+ const utcHour = timestamp.toISOString().slice(0, 13);const deduplicationKey = ["langfuse","backend-activity",- utcDate,+ "v2",+ utcHour,userId,activityScope,scopeId,