Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix: Reduce PostHog volume and add connector analytics (#1503)
Aman Agarwald1a907cd7d32
diff --git a/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.ts b/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.tsindex 972c35fa..0202b258 100644--- a/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.ts+++ b/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.ts@@ -86,4 +86,22 @@ describe("manage-dashboard query/run route", () => {signal: "traces",});});++ it("passes the widget id as the PostHog sample key", async () => {+ const PostHogServer = (await import("@/lib/posthog")).default;++ await POST(+ jsonRequest({+ widgetId: "w1",+ filter: {},+ }) as never+ );++ expect(PostHogServer.fireEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "DASHBOARD_QUERY_RUN_SUCCESS",+ sampleKey: "w1",+ })+ );+ });});\ No newline at end of filediff --git a/src/client/src/__tests__/constants/events.test.ts b/src/client/src/__tests__/constants/events.test.tsindex 59aa7526..155dd339 100644--- a/src/client/src/__tests__/constants/events.test.ts+++ b/src/client/src/__tests__/constants/events.test.ts@@ -323,6 +323,12 @@ describe("SERVER_EVENTS", () => {);});+ it("has connector server events", () => {+ expect(SERVER_EVENTS.CONNECTOR_CREATE_SUCCESS).toBe("CONNECTOR_CREATE_SUCCESS");+ expect(SERVER_EVENTS.CONNECTOR_CREATE_FAILURE).toBe("CONNECTOR_CREATE_FAILURE");+ expect(SERVER_EVENTS.SIGNAL_ROUTING_CHANGED).toBe("SIGNAL_ROUTING_CHANGED");+ });+it("has the daily instance telemetry snapshot event", () => {expect(SERVER_EVENTS.INSTANCE_TELEMETRY_SNAPSHOT).toBe("INSTANCE_TELEMETRY_SNAPSHOT"diff --git a/src/client/src/__tests__/helpers/client/connector-analytics.test.ts b/src/client/src/__tests__/helpers/client/connector-analytics.test.tsindex 2bd1ebcc..f2985235 100644--- a/src/client/src/__tests__/helpers/client/connector-analytics.test.ts+++ b/src/client/src/__tests__/helpers/client/connector-analytics.test.ts@@ -24,6 +24,7 @@ describe("connector analytics helpers", () => {connectorCreateEventProps({ type: "tempo", environment: "Staging" })).toEqual({connector_type: "tempo",+ connector_name: "Grafana Tempo",environment: "staging",});});@@ -46,6 +47,8 @@ describe("connector analytics helpers", () => {next_source_id: "src-b",previous_connector_type: "tempo",next_connector_type: "jaeger",+ previous_connector_name: "Grafana Tempo",+ next_connector_name: "Jaeger",});});});diff --git a/src/client/src/__tests__/helpers/server/connector-analytics.test.ts b/src/client/src/__tests__/helpers/server/connector-analytics.test.tsnew file mode 100644index 00000000..59f5dac3--- /dev/null+++ b/src/client/src/__tests__/helpers/server/connector-analytics.test.ts@@ -0,0 +1,60 @@+jest.mock("@/lib/posthog", () => ({+ __esModule: true,+ default: { fireEvent: jest.fn() },+}));++import PostHogServer from "@/lib/posthog";+import { SERVER_EVENTS } from "@/constants/events";+import {+ fireConnectorCreateTelemetry,+ fireSignalRoutingChangedTelemetry,+} from "@/helpers/server/connector-analytics";++describe("server connector analytics", () => {+ beforeEach(() => {+ jest.clearAllMocks();+ });++ it("fires connector create success with safe props", () => {+ fireConnectorCreateTelemetry({+ success: true,+ type: "tempo",+ environment: "Staging",+ startTimestamp: 1_000,+ });++ expect(PostHogServer.fireEvent).toHaveBeenCalledWith({+ event: SERVER_EVENTS.CONNECTOR_CREATE_SUCCESS,+ startTimestamp: 1_000,+ properties: {+ connector_type: "tempo",+ connector_name: "Grafana Tempo",+ environment: "staging",+ },+ });+ });++ it("fires signal routing changes", () => {+ fireSignalRoutingChangedTelemetry({+ signal: "traces",+ environment: "production",+ previousSourceId: "src-a",+ nextSourceId: "src-b",+ previousConnectorType: "tempo",+ nextConnectorType: "jaeger",+ startTimestamp: 2_000,+ });++ expect(PostHogServer.fireEvent).toHaveBeenCalledWith({+ event: SERVER_EVENTS.SIGNAL_ROUTING_CHANGED,+ startTimestamp: 2_000,+ properties: expect.objectContaining({+ signal: "traces",+ change: "switched",+ next_source_id: "src-b",+ previous_connector_name: "Grafana Tempo",+ next_connector_name: "Jaeger",+ }),+ });+ });+});diff --git a/src/client/src/__tests__/lib/posthog-sampling.test.ts b/src/client/src/__tests__/lib/posthog-sampling.test.tsnew file mode 100644index 00000000..ff516797--- /dev/null+++ b/src/client/src/__tests__/lib/posthog-sampling.test.ts@@ -0,0 +1,55 @@+import { shouldCaptureServerTelemetryEvent } from "@/lib/posthog-sampling";++describe("posthog sampling", () => {+ const originalEnv = process.env.POSTHOG_DASHBOARD_QUERY_SAMPLE_RATE;++ afterEach(() => {+ if (originalEnv === undefined) {+ delete process.env.POSTHOG_DASHBOARD_QUERY_SAMPLE_RATE;+ } else {+ process.env.POSTHOG_DASHBOARD_QUERY_SAMPLE_RATE = originalEnv;+ }+ jest.restoreAllMocks();+ });++ it("always captures failures", () => {+ expect(+ shouldCaptureServerTelemetryEvent(+ "DASHBOARD_QUERY_RUN_FAILURE",+ "widget-1"+ )+ ).toBe(true);+ });++ it("captures low-volume success events by default", () => {+ expect(+ shouldCaptureServerTelemetryEvent("CONNECTOR_CREATE_SUCCESS", "tempo")+ ).toBe(true);+ });++ it("samples dashboard query successes at roughly 10%", () => {+ let captured = 0;+ for (let i = 0; i < 1_000; i += 1) {+ if (+ shouldCaptureServerTelemetryEvent(+ "DASHBOARD_QUERY_RUN_SUCCESS",+ `widget-${i}`+ )+ ) {+ captured += 1;+ }+ }+ expect(captured).toBeGreaterThan(50);+ expect(captured).toBeLessThan(200);+ });++ it("respects the dashboard query sample-rate override", () => {+ process.env.POSTHOG_DASHBOARD_QUERY_SAMPLE_RATE = "0";+ expect(+ shouldCaptureServerTelemetryEvent(+ "DASHBOARD_QUERY_RUN_SUCCESS",+ "widget-1"+ )+ ).toBe(false);+ });+});diff --git a/src/client/src/__tests__/lib/telemetry-source-crud.test.ts b/src/client/src/__tests__/lib/telemetry-source-crud.test.tsindex 025eaa26..12d252a5 100644--- a/src/client/src/__tests__/lib/telemetry-source-crud.test.ts+++ b/src/client/src/__tests__/lib/telemetry-source-crud.test.ts@@ -474,6 +474,7 @@ describe("telemetry source bindings", () => {mockBindingFindUnique.mockResolvedValue({sourceId: "src-old",databaseConfigId: null,+ source: { type: "tempo" },});mockBindingUpsert.mockResolvedValue({id: "b1",@@ -485,8 +486,14 @@ describe("telemetry source bindings", () => {expect(result).toMatchObject({sourceId: "src-1",previousSourceId: "src-old",+ previousSourceType: "tempo",+ nextSourceType: "datadog",environment: "production",});+ expect(mockBindingFindUnique).toHaveBeenCalledWith({+ where: { projectId_signal_environment: { projectId: "proj-1", signal: "traces", environment: "production" } },+ include: { source: true },+ });expect(mockFindFirst).toHaveBeenCalledWith({where: { id: "src-1", projectId: "proj-1" },});@@ -535,12 +542,18 @@ describe("telemetry source bindings", () => {mockBindingFindFirst.mockResolvedValue({sourceId: "src-9",databaseConfigId: null,+ source: { type: "tempo" },});mockBindingDeleteMany.mockResolvedValue({ count: 1 });await expect(deleteTelemetrySourceBinding("logs")).resolves.toEqual({signal: "logs",environment: "production",previousSourceId: "src-9",+ previousSourceType: "tempo",+ });+ expect(mockBindingFindFirst).toHaveBeenCalledWith({+ where: { projectId: "proj-1", signal: "logs", environment: "production" },+ include: { source: true },});expect(mockBindingDeleteMany).toHaveBeenCalledWith({where: { projectId: "proj-1", signal: "logs", environment: "production" },diff --git a/src/client/src/app/api/connectors/route.ts b/src/client/src/app/api/connectors/route.tsindex 56ff0593..042d9476 100644--- a/src/client/src/app/api/connectors/route.ts+++ b/src/client/src/app/api/connectors/route.ts@@ -18,6 +18,7 @@ import {createMemoryConnector,isMemoryConnectorType,} from "@/lib/platform/connectors/memory/crud";+import { fireConnectorCreateTelemetry } from "@/helpers/server/connector-analytics";/*** Generic connector endpoint. Datasource instances are exposed through the@@ -49,6 +50,7 @@ async function GETHandler() {}async function POSTHandler(request: NextRequest) {+ const startTimestamp = Date.now();const user = await getCurrentUser();if (!user) return Response.json("Unauthorized", { status: 401 });let body: Record<string, unknown>;@@ -59,9 +61,24 @@ async function POSTHandler(request: NextRequest) {}const category = String(body.category || "");const type = String(body.type || "");+ const environment = String(body.environment || "production");if (category === "memory" || isMemoryConnectorType(type)) {const [err, connector] = await asaw(createMemoryConnector(body));- if (err) return errorResponse(err, "Failed to create connector");+ if (err) {+ fireConnectorCreateTelemetry({+ success: false,+ type: type || "memory",+ environment,+ startTimestamp,+ });+ return errorResponse(err, "Failed to create connector");+ }+ fireConnectorCreateTelemetry({+ success: true,+ type: String((connector as { type?: string })?.type || type || "memory"),+ environment,+ startTimestamp,+ });return Response.json(connector);}if (category && category !== "datasource") {@@ -88,6 +105,12 @@ async function POSTHandler(request: NextRequest) {}));if (validationErr) {+ fireConnectorCreateTelemetry({+ success: false,+ type: "clickhouse",+ environment,+ startTimestamp,+ });return errorResponse(validationErr,"Failed to validate ClickHouse connector through OpenPlait"@@ -103,7 +126,15 @@ async function POSTHandler(request: NextRequest) {query: settings.query,password: credentials.password,} as any, typeof body.id === "string" ? body.id : undefined));- if (dbErr) return errorResponse(dbErr, "Failed to save ClickHouse connector");+ if (dbErr) {+ fireConnectorCreateTelemetry({+ success: false,+ type: "clickhouse",+ environment,+ startTimestamp,+ });+ return errorResponse(dbErr, "Failed to save ClickHouse connector");+ }const organisation = await getCurrentOrganisation();const project = organisation?.id? await getCurrentProjectForOrganisation(organisation.id)@@ -114,6 +145,12 @@ async function POSTHandler(request: NextRequest) {item.name === String(body.name || "") &&(item.environment || "production") === String(body.environment || "production"));+ fireConnectorCreateTelemetry({+ success: true,+ type: "clickhouse",+ environment,+ startTimestamp,+ });return Response.json({...(connector || { name: body.name, type: "clickhouse", environment: body.environment }),category: "datasource",@@ -121,7 +158,21 @@ async function POSTHandler(request: NextRequest) {});}const [err, source] = await asaw(createTelemetrySource(body));- if (err) return errorResponse(err, "Failed to create connector");+ if (err) {+ fireConnectorCreateTelemetry({+ success: false,+ type: type || "unknown",+ environment,+ startTimestamp,+ });+ return errorResponse(err, "Failed to create connector");+ }+ fireConnectorCreateTelemetry({+ success: true,+ type: String((source as { type?: string })?.type || type),+ environment,+ startTimestamp,+ });return Response.json({ ...source, category: "datasource", scope: "project" });}diff --git a/src/client/src/app/api/manage-dashboard/query/run/route.ts b/src/client/src/app/api/manage-dashboard/query/run/route.tsindex d8e8e927..cf9b32b4 100644--- a/src/client/src/app/api/manage-dashboard/query/run/route.ts+++ b/src/client/src/app/api/manage-dashboard/query/run/route.ts@@ -66,6 +66,7 @@ export async function POST(request: NextRequest) {? SERVER_EVENTS.DASHBOARD_QUERY_RUN_FAILURE: SERVER_EVENTS.DASHBOARD_QUERY_RUN_SUCCESS,startTimestamp,+ sampleKey: widgetId,});return Response.json(res);}diff --git a/src/client/src/app/api/telemetry-source/binding/route.ts b/src/client/src/app/api/telemetry-source/binding/route.tsindex 5ec5517a..a4395898 100644--- a/src/client/src/app/api/telemetry-source/binding/route.ts+++ b/src/client/src/app/api/telemetry-source/binding/route.ts@@ -10,6 +10,10 @@ import { TELEMETRY_SOURCE_INVALID_JSON } from "@/constants/messages/en";import { NextRequest } from "next/server";import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";+import {+ clearedRoutingSourceId,+ fireSignalRoutingChangedTelemetry,+} from "@/helpers/server/connector-analytics";async function GETHandler(request: NextRequest) {const user = await getCurrentUser();@@ -27,6 +31,7 @@ async function GETHandler(request: NextRequest) {}async function PUTHandler(request: NextRequest) {+ const startTimestamp = Date.now();const user = await getCurrentUser();if (!user) return Response.json("Unauthorized", { status: 401 });@@ -41,23 +46,43 @@ async function PUTHandler(request: NextRequest) {setTelemetrySourceBinding(body?.signal, String(body?.sourceId ?? ""), body?.environment));if (err) return errorResponse(err, "Failed to set telemetry source binding");+ fireSignalRoutingChangedTelemetry({+ signal: String(binding?.signal || body?.signal || ""),+ environment: String(binding?.environment || body?.environment || "production"),+ previousSourceId: binding?.previousSourceId,+ nextSourceId: String(binding?.sourceId ?? body?.sourceId ?? ""),+ previousConnectorType: binding?.previousSourceType,+ nextConnectorType: binding?.nextSourceType,+ startTimestamp,+ });return Response.json(binding);}async function DELETEHandler(request: NextRequest) {+ const startTimestamp = Date.now();const user = await getCurrentUser();if (!user) return Response.json("Unauthorized", { status: 401 });const signal = request.nextUrl.searchParams.get("signal");+ const environment =+ request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) ||+ request.nextUrl.searchParams.get("environment") ||+ "production";const [err, result] = await asaw(deleteTelemetrySourceBinding(signal,- request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) ||- request.nextUrl.searchParams.get("environment") ||- undefined+ environment));if (err) return errorResponse(err, "Failed to delete telemetry source binding");+ fireSignalRoutingChangedTelemetry({+ signal: String(result?.signal || signal || ""),+ environment: String(result?.environment || environment),+ previousSourceId: result?.previousSourceId,+ nextSourceId: clearedRoutingSourceId(),+ previousConnectorType: result?.previousSourceType,+ startTimestamp,+ });return Response.json(result);}diff --git a/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsx b/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsxindex 26aa19b2..da033c2c 100644--- a/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsx+++ b/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsx@@ -50,13 +50,7 @@ import {DialogTitle,} from "@/components/ui/dialog";import getMessage from "@/constants/messages";-import { CLIENT_EVENTS } from "@/constants/events";import FeatureAccess from "@/components/rbac/feature-access";-import {- connectorCreateEventProps,- signalRoutingChangedEventProps,-} from "@/helpers/client/connector-analytics";-import { usePostHog } from "posthog-js/react";import type { FieldDef } from "@/lib/platform/connectors/datasource/types";import { getDatabaseConfigList } from "@/selectors/database-config";@@ -260,7 +254,6 @@ export default function DataSourcesPage({onOpenTypeHandled?: () => void;}) {const messages = getMessage();- const posthog = usePostHog();const currentProjectEnvironment = useRootStore(getCurrentProjectEnvironment);const databaseConfigs = useRootStore(getDatabaseConfigList) || [];const [loading, setLoading] = useState(true);@@ -356,15 +349,6 @@ export default function DataSourcesPage({);const setBinding = async (signal: Signal, sourceId: string) => {- const previous = bindingForSignal(signal);- const previousSourceId = previous?.sourceId || null;- const previousConnectorType = previous?.sourceType || null;- const nextConnectorType =- sourceId === BUILTIN- ? null- : sourceId.startsWith("builtin:")- ? "clickhouse"- : sources.find((source) => signalRoutingSelectValue(source.id) === sourceId || source.id === sourceId)?.type || null;toast.loading(messages.DATA_SOURCE_BINDING_SAVED, { id: "ds-bind" });try {if (sourceId === BUILTIN) {@@ -387,17 +371,6 @@ export default function DataSourcesPage({}),});}- posthog?.capture(- CLIENT_EVENTS.SIGNAL_ROUTING_CHANGED,- signalRoutingChangedEventProps({- signal,- environment,- previousSourceId,- nextSourceId: sourceId,- previousConnectorType,- nextConnectorType,- })- );toast.success(messages.DATA_SOURCE_BINDING_SAVED, { id: "ds-bind" });await load();} catch (e: any) {@@ -1067,7 +1040,6 @@ export function SourceFormDialog({onSaved: () => void;}) {const messages = getMessage();- const posthog = usePostHog();const isEdit = !!source;const [name, setName] = useState(source?.name || "");const [environment, setEnvironment] = useState(source?.environment || initialEnvironment || "production");@@ -1189,13 +1161,6 @@ export function SourceFormDialog({headers: { "Content-Type": "application/json" },body: JSON.stringify(payload),});- posthog?.capture(- CLIENT_EVENTS.CONNECTOR_CREATE_SUCCESS,- connectorCreateEventProps({- type: "clickhouse",- environment,- })- );toast.success(messages.DATA_SOURCE_SAVED, { id: "ds-save" });onSaved();return;@@ -1225,26 +1190,10 @@ export function SourceFormDialog({headers: { "Content-Type": "application/json" },body: JSON.stringify(payload),});- posthog?.capture(- CLIENT_EVENTS.CONNECTOR_CREATE_SUCCESS,- connectorCreateEventProps({- type,- environment,- })- );}toast.success(messages.DATA_SOURCE_SAVED, { id: "ds-save" });onSaved();} catch (e: any) {- if (!isEdit) {- posthog?.capture(- CLIENT_EVENTS.CONNECTOR_CREATE_FAILURE,- connectorCreateEventProps({- type: type || "unknown",- environment,- })- );- }toast.error(e?.message || messages.DATA_SOURCE_SAVE_FAILED, {id: "ds-save",});diff --git a/src/client/src/constants/events.ts b/src/client/src/constants/events.tsindex e7a9bec9..70fc7bfc 100644--- a/src/client/src/constants/events.ts+++ b/src/client/src/constants/events.ts@@ -284,7 +284,14 @@ export const SERVER_EVENTS = {// Daily anonymous instance snapshot (inventory + world-total ingestion).// Fired once per day per install by the telemetry-snapshot cron.- INSTANCE_TELEMETRY_SNAPSHOT: "INSTANCE_TELEMETRY_SNAPSHOT", RULE_LIST_SUCCESS: "RULE_LIST_SUCCESS",+ INSTANCE_TELEMETRY_SNAPSHOT: "INSTANCE_TELEMETRY_SNAPSHOT",++ // Connectors + signal routing (product analytics — no secrets)+ CONNECTOR_CREATE_SUCCESS: "CONNECTOR_CREATE_SUCCESS",+ CONNECTOR_CREATE_FAILURE: "CONNECTOR_CREATE_FAILURE",+ SIGNAL_ROUTING_CHANGED: "SIGNAL_ROUTING_CHANGED",++ RULE_LIST_SUCCESS: "RULE_LIST_SUCCESS",RULE_LIST_FAILURE: "RULE_LIST_FAILURE",RULE_GET_SUCCESS: "RULE_GET_SUCCESS",RULE_GET_FAILURE: "RULE_GET_FAILURE",diff --git a/src/client/src/helpers/client/connector-analytics.ts b/src/client/src/helpers/client/connector-analytics.tsindex 5ace866c..051f7134 100644--- a/src/client/src/helpers/client/connector-analytics.ts+++ b/src/client/src/helpers/client/connector-analytics.ts@@ -3,10 +3,24 @@* No secrets, endpoints, or credential fields — only product-intent metadata.*/+import { getSourceTypeDescriptor } from "@/lib/platform/connectors/datasource/registry";+import { getMemoryTypeDescriptor } from "@/lib/platform/connectors/memory/registry";+export type SignalRoutingChangeKind = "bound" | "switched" | "cleared";export const BUILTIN_ROUTING_VALUE = "builtin";+const CONNECTOR_DISPLAY_NAMES: Record<string, string> = {+ clickhouse: "ClickHouse",+ tempo: "Grafana Tempo",+ loki: "Grafana Loki",+ prometheus: "Prometheus",+ jaeger: "Jaeger",+ claude: "Claude",+ mem0: "Mem0",+ zep: "Zep",+};+export function classifySignalRoutingChange(previousSourceId: string | null | undefined,nextSourceId: string@@ -19,12 +33,26 @@ export function classifySignalRoutingChange(return previous === next ? "bound" : "switched";}+export function resolveConnectorDisplayName(+ type: string | null | undefined+): string | null {+ const slug = String(type || "").trim().toLowerCase();+ if (!slug) return null;+ const datasource = getSourceTypeDescriptor(slug);+ if (datasource?.displayName) return datasource.displayName;+ const memory = getMemoryTypeDescriptor(slug);+ if (memory?.displayName) return memory.displayName;+ return CONNECTOR_DISPLAY_NAMES[slug] || slug.charAt(0).toUpperCase() + slug.slice(1);+}+export function connectorCreateEventProps(input: {type: string;environment?: string;}) {+ const connectorType = String(input.type || "").trim().toLowerCase() || "unknown";return {- connector_type: input.type,+ connector_type: connectorType,+ connector_name: resolveConnectorDisplayName(connectorType),environment: (input.environment || "production").toLowerCase(),};}@@ -41,13 +69,17 @@ export function signalRoutingChangedEventProps(input: {input.previousSourceId,input.nextSourceId);+ const previousConnectorType = input.previousConnectorType || null;+ const nextConnectorType = input.nextConnectorType || null;return {signal: input.signal,environment: (input.environment || "production").toLowerCase(),change: kind,previous_source_id: input.previousSourceId || null,next_source_id: input.nextSourceId || null,- previous_connector_type: input.previousConnectorType || null,- next_connector_type: input.nextConnectorType || null,+ previous_connector_type: previousConnectorType,+ next_connector_type: nextConnectorType,+ previous_connector_name: resolveConnectorDisplayName(previousConnectorType),+ next_connector_name: resolveConnectorDisplayName(nextConnectorType),};}diff --git a/src/client/src/helpers/server/connector-analytics.ts b/src/client/src/helpers/server/connector-analytics.tsnew file mode 100644index 00000000..d3a605e7--- /dev/null+++ b/src/client/src/helpers/server/connector-analytics.ts@@ -0,0 +1,52 @@+import { SERVER_EVENTS } from "@/constants/events";+import {+ BUILTIN_ROUTING_VALUE,+ connectorCreateEventProps,+ signalRoutingChangedEventProps,+} from "@/helpers/client/connector-analytics";+import PostHogServer from "@/lib/posthog";++export function fireConnectorCreateTelemetry(input: {+ success: boolean;+ type: string;+ environment?: string;+ startTimestamp: number;+}) {+ PostHogServer.fireEvent({+ event: input.success+ ? SERVER_EVENTS.CONNECTOR_CREATE_SUCCESS+ : SERVER_EVENTS.CONNECTOR_CREATE_FAILURE,+ startTimestamp: input.startTimestamp,+ properties: connectorCreateEventProps({+ type: input.type,+ environment: input.environment,+ }),+ });+}++export function fireSignalRoutingChangedTelemetry(input: {+ signal: string;+ environment?: string;+ previousSourceId?: string | null;+ nextSourceId: string;+ previousConnectorType?: string | null;+ nextConnectorType?: string | null;+ startTimestamp: number;+}) {+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.SIGNAL_ROUTING_CHANGED,+ startTimestamp: input.startTimestamp,+ properties: signalRoutingChangedEventProps({+ signal: input.signal,+ environment: input.environment,+ previousSourceId: input.previousSourceId,+ nextSourceId: input.nextSourceId,+ previousConnectorType: input.previousConnectorType,+ nextConnectorType: input.nextConnectorType,+ }),+ });+}++export function clearedRoutingSourceId() {+ return BUILTIN_ROUTING_VALUE;+}diff --git a/src/client/src/lib/posthog-sampling.ts b/src/client/src/lib/posthog-sampling.tsnew file mode 100644index 00000000..f937e65d--- /dev/null+++ b/src/client/src/lib/posthog-sampling.ts@@ -0,0 +1,48 @@+import { createHash } from "crypto";++/**+ * Per-event success sample rates (0–1). Failures are always captured.+ * High-volume widget queries are bucketed hourly so repeated refreshes in the+ * same window do not flood PostHog while still spreading coverage across widgets.+ */+const SUCCESS_SAMPLE_RATES: Record<string, number> = {+ DASHBOARD_QUERY_RUN_SUCCESS: 0.1,+};++function parseSampleRate(value: string | undefined): number | undefined {+ if (value === undefined || value.trim() === "") return undefined;+ const parsed = Number.parseFloat(value);+ if (!Number.isFinite(parsed)) return undefined;+ if (parsed <= 0) return 0;+ if (parsed >= 1) return 1;+ return parsed;+}++function resolveSuccessSampleRate(event: string): number {+ const envOverride = parseSampleRate(+ process.env.POSTHOG_DASHBOARD_QUERY_SAMPLE_RATE+ );+ if (event === "DASHBOARD_QUERY_RUN_SUCCESS" && envOverride !== undefined) {+ return envOverride;+ }+ return SUCCESS_SAMPLE_RATES[event] ?? 1;+}++export function shouldCaptureServerTelemetryEvent(+ event: string,+ sampleKey?: string+): boolean {+ if (event.endsWith("_FAILURE")) return true;++ const rate = resolveSuccessSampleRate(event);+ if (rate >= 1) return true;+ if (rate <= 0) return false;++ const key = sampleKey || "global";+ const hourBucket = Math.floor(Date.now() / 3_600_000);+ const hash = createHash("sha256")+ .update(`${event}:${key}:${hourBucket}`)+ .digest();+ const normalized = hash.readUInt32BE(0) / 0xffffffff;+ return normalized < rate;+}diff --git a/src/client/src/lib/posthog.ts b/src/client/src/lib/posthog.tsindex 60ed6a9f..43033dec 100644--- a/src/client/src/lib/posthog.ts+++ b/src/client/src/lib/posthog.ts@@ -2,6 +2,7 @@ import {POSTHOG_API_HOST,POSTHOG_API_KEY,} from "@/constants/posthog";+import { shouldCaptureServerTelemetryEvent } from "@/lib/posthog-sampling";import { jsonStringify } from "@/utils/json";import { consoleLog } from "@/utils/log";import { randomUUID } from "crypto";@@ -54,11 +55,17 @@ export default class PostHogServer {event,properties = {},startTimestamp,+ sampleKey,}: {event: string;properties?: Record<string, unknown>;startTimestamp: number;+ /** Dedupes / samples high-volume success events (e.g. per widget id). */+ sampleKey?: string;}) {+ if (!shouldCaptureServerTelemetryEvent(event, sampleKey)) {+ return;+ }await PostHogServer.capture({event,timestamp: new Date(startTimestamp),diff --git a/src/client/src/lib/telemetry-source-crud.ts b/src/client/src/lib/telemetry-source-crud.tsindex 7b2c411e..d21baae3 100644--- a/src/client/src/lib/telemetry-source-crud.ts+++ b/src/client/src/lib/telemetry-source-crud.ts@@ -596,6 +596,17 @@ function publicBindingSourceId(row: {return null;}+function bindingSourceType(row: {+ sourceId: string | null;+ databaseConfigId: string | null;+ source?: { type: string } | null;+} | null | undefined): string | null {+ if (!row) return null;+ if (row.source?.type) return row.source.type;+ if (row.databaseConfigId) return "clickhouse";+ return null;+}+/** List the current project's per-signal source bindings. */export async function listTelemetrySourceBindings(environmentInput?: unknown) {const projectId = await requireCurrentProjectId();@@ -630,8 +641,10 @@ export async function setTelemetrySourceBinding(const environment = normalizeEnvironment(environmentInput);const existing = await prisma.telemetrySourceBinding.findUnique({where: { projectId_signal_environment: { projectId, signal, environment } },+ include: { source: true },});const previousSourceId = publicBindingSourceId(existing);+ const previousSourceType = bindingSourceType(existing);const normalizedSourceId = sourceId.startsWith("builtin:")? sourceId: normalizeTelemetrySourceId(sourceId);@@ -666,6 +679,8 @@ export async function setTelemetrySourceBinding(sourceId: normalizedSourceId,environment,previousSourceId,+ previousSourceType,+ nextSourceType: "clickhouse",};}if (!source) throw new Error(TELEMETRY_SOURCE_NOT_FOUND);@@ -703,6 +718,8 @@ export async function setTelemetrySourceBinding(sourceId: binding.sourceId,environment: binding.environment,previousSourceId,+ previousSourceType,+ nextSourceType: source.type,};}@@ -713,11 +730,13 @@ export async function deleteTelemetrySourceBinding(signalInput: unknown, environconst environment = normalizeEnvironment(environmentInput);const existing = await prisma.telemetrySourceBinding.findFirst({where: { projectId, signal, environment },+ include: { source: true },});const previousSourceId = publicBindingSourceId(existing);+ const previousSourceType = bindingSourceType(existing);await prisma.telemetrySourceBinding.deleteMany({where: { projectId, signal, environment },});invalidateTelemetryReadCaches();- return { signal, environment, previousSourceId };+ return { signal, environment, previousSourceId, previousSourceType };}