Skip to content

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

feat(web): show the persisted PostHog export fault in settings (#16096)

Niklas Semmlerfc4be6507ed5
diff --git a/web/src/features/posthog-integration/components/PostHogStatusSection.clienttest.tsx b/web/src/features/posthog-integration/components/PostHogStatusSection.clienttest.tsx
new file mode 100644
index 0000000000..5734326a7a
--- /dev/null
+++ b/web/src/features/posthog-integration/components/PostHogStatusSection.clienttest.tsx
@@ -0,0 +1,145 @@
+import { render, screen } from "@testing-library/react";
+import { AnalyticsIntegrationExportSource } from "@langfuse/shared";
+import { type RouterOutputs } from "@/src/utils/api";
+import { PostHogStatusSection } from "./PostHogStatusSection";
+
+type PostHogIntegrationConfig = NonNullable<
+ RouterOutputs["posthogIntegration"]["get"]["config"]
+>;
+
+// A healthy, enabled integration. Each test overrides only the fields it is
+// about, so an assertion never depends on an unrelated default.
+const baseConfig: PostHogIntegrationConfig = {
+ projectId: "project-1",
+ posthogHostName: "https://us.posthog.com",
+ posthogApiKeyDisplay: "phc_...cdef",
+ exportSource: AnalyticsIntegrationExportSource.TRACES_OBSERVATIONS,
+ enabled: true,
+ createdAt: new Date("2024-01-01T00:00:00Z"),
+ lastSyncAt: new Date("2024-06-01T12:00:00Z"),
+ lastError: null,
+ lastErrorAt: null,
+};
+
+const renderSection = (overrides: Partial<PostHogIntegrationConfig> = {}) =>
+ render(<PostHogStatusSection config={{ ...baseConfig, ...overrides }} />);
+
+// The destructive styling is the user-visible "this is a problem" signal, so
+// the variant is asserted alongside the alert role rather than the class alone.
+const expectDestructiveAlert = (): HTMLElement => {
+ const alert = screen.getByRole("alert");
+ expect(alert.className).toContain("destructive");
+ return alert;
+};
+
+describe("PostHogStatusSection fault surfacing", () => {
+ // Load-bearing: this is the exact state the current page gets wrong. After a
+ // customer-config fault the worker sets enabled=false, and the page's only
+ // status block is gated behind enabled — so the explanation disappears at the
+ // precise moment there is finally something to explain.
+ it("renders the fault alert when the integration was auto-disabled", () => {
+ renderSection({
+ enabled: false,
+ lastError: "Blocked hostname detected",
+ lastErrorAt: new Date("2024-06-02T09:30:00Z"),
+ });
+
+ const alert = expectDestructiveAlert();
+ expect(alert).toHaveTextContent("Blocked hostname detected");
+ expect(alert).toHaveTextContent(
+ new Date("2024-06-02T09:30:00Z").toLocaleString(),
+ );
+ });
+
+ // A fault can be persisted on a still-enabled integration: only classified
+ // customer faults disable, so a transient failure leaves enabled=true. The
+ // alert must not be conditional on the disabled state either.
+ it("renders the fault alert while the integration is still enabled", () => {
+ renderSection({
+ enabled: true,
+ lastError: "Blocked hostname detected",
+ lastErrorAt: new Date("2024-06-02T09:30:00Z"),
+ });
+
+ expect(expectDestructiveAlert()).toHaveTextContent(
+ "Blocked hostname detected",
+ );
+ });
+
+ // Guards against an always-on empty alert — the failure mode of hoisting the
+ // alert out of its conditional.
+ it("renders no alert when there is no persisted fault", () => {
+ renderSection({ lastError: null, lastErrorAt: null });
+
+ expect(screen.queryByRole("alert")).toBeNull();
+ });
+
+ // The two columns are written together by the worker but are independently
+ // nullable in the schema, so the timestamp must be treated as optional rather
+ // than fed unguarded into a Date.
+ it("renders the fault alert without a bogus date when the timestamp is missing", () => {
+ renderSection({
+ enabled: false,
+ lastError: "Blocked hostname detected",
+ lastErrorAt: null,
+ });
+
+ expect(expectDestructiveAlert()).toHaveTextContent(
+ "Blocked hostname detected",
+ );
+ expect(screen.queryByText(/Invalid Date/i)).toBeNull();
+ });
+});
+
+describe("PostHogStatusSection sync line", () => {
+ it("shows the last sync time", () => {
+ renderSection({ lastSyncAt: new Date("2024-06-01T12:00:00Z") });
+
+ expect(
+ screen.getByText(
+ new RegExp(
+ `Data synced until:\\s*${new Date(
+ "2024-06-01T12:00:00Z",
+ ).toLocaleString()}`,
+ ),
+ ),
+ ).toBeInTheDocument();
+ });
+
+ it("shows the never-synced fallback when no sync has completed", () => {
+ renderSection({ lastSyncAt: null });
+
+ expect(
+ screen.getByText(/Data synced until:\s*Never \(pending\)/),
+ ).toBeInTheDocument();
+ });
+
+ // Ruling on the gating question: the sync line always renders, matching the
+ // blob storage status section this component mirrors. Gating it on `enabled`
+ // would leave a disabled integration with no sync line, so a user-disabled
+ // integration (disabled, no fault) would render a bare "Status" header with
+ // nothing under it. "When did data last get through?" is also precisely what
+ // an admin needs while recovering from an auto-disable.
+ it("shows the sync line even when the integration is disabled", () => {
+ renderSection({
+ enabled: false,
+ lastSyncAt: new Date("2024-06-01T12:00:00Z"),
+ });
+
+ expect(
+ screen.getByText(
+ new RegExp(
+ `Data synced until:\\s*${new Date(
+ "2024-06-01T12:00:00Z",
+ ).toLocaleString()}`,
+ ),
+ ),
+ ).toBeInTheDocument();
+ });
+
+ it("renders the Status header", () => {
+ renderSection();
+
+ expect(screen.getByRole("heading", { name: "Status" })).toBeInTheDocument();
+ });
+});
diff --git a/web/src/features/posthog-integration/components/PostHogStatusSection.tsx b/web/src/features/posthog-integration/components/PostHogStatusSection.tsx
new file mode 100644
index 0000000000..903dff02b8
--- /dev/null
+++ b/web/src/features/posthog-integration/components/PostHogStatusSection.tsx
@@ -0,0 +1,54 @@
+import Header from "@/src/components/layouts/header";
+import { Alert, AlertDescription, AlertTitle } from "@/src/components/ui/alert";
+import { type RouterOutputs } from "@/src/utils/api";
+
+type PostHogIntegrationConfig = NonNullable<
+ RouterOutputs["posthogIntegration"]["get"]["config"]
+>;
+
+/**
+ * PostHogStatusSection surfaces the persisted export fault next to the sync
+ * line. Neither is gated on `enabled`: a customer-config fault auto-disables
+ * the integration, so gating would hide the explanation at the one moment it
+ * is needed, and a manually disabled integration would render a bare header.
+ */
+export const PostHogStatusSection = ({
+ config,
+}: {
+ config: PostHogIntegrationConfig;
+}) => {
+ return (
+ <>
+ <Header title="Status" className="mt-8" />
+ {config.lastError && (
+ <Alert variant="destructive" className="mb-4">
+ {/* A fault normally arrives with the auto-disable, but the disable is
+ skipped when the host changed mid-run, leaving a fault on a still
+ enabled integration — so only promise "disabled" when it is. */}
+ <AlertTitle>
+ {config.enabled
+ ? "Last export failed"
+ : "Export disabled – action required"}
+ </AlertTitle>
+ <AlertDescription>
+ {config.lastError}
+ {config.lastErrorAt && (
+ <>
+ <br />
+ <span className="text-xs opacity-70">
+ {new Date(config.lastErrorAt).toLocaleString()}
+ </span>
+ </>
+ )}
+ </AlertDescription>
+ </Alert>
+ )}
+ <p className="text-primary text-sm">
+ Data synced until:{" "}
+ {config.lastSyncAt
+ ? new Date(config.lastSyncAt).toLocaleString()
+ : "Never (pending)"}
+ </p>
+ </>
+ );
+};
diff --git a/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx b/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx
index b13ddd1e85..5c670a61e3 100644
--- a/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx
+++ b/web/src/pages/project/[projectId]/settings/integrations/posthog.tsx
@@ -29,6 +29,7 @@ import {
} from "@/src/components/ui/tooltip";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { posthogIntegrationFormSchema } from "@/src/features/posthog-integration/types";
+import { PostHogStatusSection } from "@/src/features/posthog-integration/components/PostHogStatusSection";
import {
AnalyticsIntegrationExportSource,
validateExportSource,
@@ -72,12 +73,16 @@ export default function PosthogIntegrationSettings() {
},
);
+ // A persisted fault outranks active/inactive: it is the state the admin has
+ // to act on, and it is cleared by the next successful sync.
const status =
state.isLoading || !hasAccess
? undefined
- : state.data?.config?.enabled
- ? "active"
- : "inactive";
+ : state.data?.config?.lastError
+ ? "error"
+ : state.data?.config?.enabled
+ ? "active"
+ : "inactive";
return (
<ContainerPage
@@ -127,16 +132,8 @@ export default function PosthogIntegrationSettings() {
</Card>
</>
)}
- {state.data?.config?.enabled && (
- <>
- <Header title="Status" className="mt-8" />
- <p className="text-primary text-sm">
- Data synced until:{" "}
- {state.data?.config?.lastSyncAt
- ? new Date(state.data.config.lastSyncAt).toLocaleString()
- : "Never (pending)"}
- </p>
- </>
+ {state.data?.config && (
+ <PostHogStatusSection config={state.data.config} />
)}
</ContainerPage>
);