🪢 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(web): tag pageload spans from cached v4BetaEnabled (#16369)
Nikita Kabardinc837041280ac
diff --git a/web/instrumentation-client.ts b/web/instrumentation-client.tsindex e00641e7ab..6630f4ecfa 100644--- a/web/instrumentation-client.ts+++ b/web/instrumentation-client.ts@@ -7,6 +7,12 @@ import {isStaleChunkParseErrorEvent,STALE_CHUNK_PARSE_FINGERPRINT,} from "@/src/utils/sentryFilters";+import { applyCachedV4BetaEnabledSentryTag } from "@/src/utils/sentryV4BetaTag";++// Isolation-scope tags are copied onto the pageload transaction at start.+// Session hydrate (and the v4 flag) arrives after that, so apply the last-known+// cache before init. Missing cache leaves the tag unset rather than guessing false.+applyCachedV4BetaEnabledSentryTag();Sentry.init({dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,diff --git a/web/src/__tests__/instrumentation-client-replay-mask.clienttest.ts b/web/src/__tests__/instrumentation-client-replay-mask.clienttest.tsindex 793ea79095..b0b1f0d78f 100644--- a/web/src/__tests__/instrumentation-client-replay-mask.clienttest.ts+++ b/web/src/__tests__/instrumentation-client-replay-mask.clienttest.ts@@ -47,6 +47,9 @@ vi.mock("@sentry/nextjs", () => ({captureConsoleIntegration: vi.fn(() => ({ name: "CaptureConsole" })),browserProfilingIntegration: vi.fn(() => ({ name: "BrowserProfiling" })),captureRouterTransitionStart: vi.fn(),+ setTag: vi.fn(),+ getActiveSpan: vi.fn(),+ getRootSpan: vi.fn(),}));/**diff --git a/web/src/__tests__/instrumentation-client-v4-tag.clienttest.ts b/web/src/__tests__/instrumentation-client-v4-tag.clienttest.tsnew file mode 100644index 0000000000..e2dc8fe0b8--- /dev/null+++ b/web/src/__tests__/instrumentation-client-v4-tag.clienttest.ts@@ -0,0 +1,96 @@+// @vitest-environment node++/**+ * Pageload transactions start at `Sentry.init`. Isolation-scope tags applied+ * after session hydrate miss that span. This guard imports the real+ * `web/instrumentation-client.ts` and asserts the cached v4 flag is applied+ * to the isolation scope before init.+ */++import {+ V4_BETA_ENABLED_SENTRY_TAG,+ V4_BETA_ENABLED_STORAGE_KEY,+} from "@/src/utils/sentryV4BetaTag";++const { initMock, setTagMock, callOrder } = vi.hoisted(() => {+ const callOrder: string[] = [];+ return {+ callOrder,+ initMock: vi.fn(() => {+ callOrder.push("init");+ }),+ setTagMock: vi.fn(() => {+ callOrder.push("setTag");+ }),+ };+});++vi.mock("@sentry/nextjs", () => ({+ init: initMock,+ replayIntegration: vi.fn(() => ({ name: "Replay" })),+ browserTracingIntegration: vi.fn(() => ({ name: "BrowserTracing" })),+ httpClientIntegration: vi.fn(() => ({ name: "HttpClient" })),+ captureConsoleIntegration: vi.fn(() => ({ name: "CaptureConsole" })),+ browserProfilingIntegration: vi.fn(() => ({ name: "BrowserProfiling" })),+ captureRouterTransitionStart: vi.fn(),+ setTag: setTagMock,+ getActiveSpan: vi.fn(),+ getRootSpan: vi.fn(),+}));++const memory = new Map<string, string>();++const localStorageMock = {+ getItem: (key: string) => memory.get(key) ?? null,+ setItem: (key: string, value: string) => {+ memory.set(key, value);+ },+ removeItem: (key: string) => {+ memory.delete(key);+ },+};++async function loadInstrumentationClient() {+ vi.resetModules();+ initMock.mockClear();+ setTagMock.mockClear();+ callOrder.length = 0;+ vi.stubGlobal("window", { localStorage: localStorageMock });++ await import("@/instrumentation-client");+}++afterEach(() => {+ memory.clear();+ vi.unstubAllGlobals();+ vi.unstubAllEnvs();+});++describe("instrumentation-client applies cached v4BetaEnabled before Sentry.init", () => {+ it("tags true from localStorage before init so pageload starts labeled", async () => {+ memory.set(V4_BETA_ENABLED_STORAGE_KEY, "true");++ await loadInstrumentationClient();++ expect(setTagMock).toHaveBeenCalledWith(V4_BETA_ENABLED_SENTRY_TAG, true);+ expect(initMock).toHaveBeenCalledTimes(1);+ expect(callOrder).toEqual(["setTag", "init"]);+ });++ it("tags false from localStorage before init", async () => {+ memory.set(V4_BETA_ENABLED_STORAGE_KEY, "false");++ await loadInstrumentationClient();++ expect(setTagMock).toHaveBeenCalledWith(V4_BETA_ENABLED_SENTRY_TAG, false);+ expect(callOrder).toEqual(["setTag", "init"]);+ });++ it("does not guess false when the cache is missing", async () => {+ await loadInstrumentationClient();++ expect(setTagMock).not.toHaveBeenCalled();+ expect(initMock).toHaveBeenCalledTimes(1);+ expect(callOrder).toEqual(["init"]);+ });+});diff --git a/web/src/components/layouts/app-layout/index.tsx b/web/src/components/layouts/app-layout/index.tsxindex 08a1b98796..48ecd4a993 100644--- a/web/src/components/layouts/app-layout/index.tsx+++ b/web/src/components/layouts/app-layout/index.tsx@@ -13,6 +13,7 @@ import { type PropsWithChildren, useEffect } from "react";import { useRouter } from "next/router";import { signOut, useSession } from "next-auth/react";import { signOutCleanly } from "@/src/features/auth/lib/signOut";+import { clearV4BetaEnabledSentryTag } from "@/src/utils/sentryV4BetaTag";import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";import { ErrorPage } from "@/src/components/error-page";@@ -72,6 +73,10 @@ export function AppLayout(props: PropsWithChildren) {if (authGuard.action === "redirect") {router.replace(authGuard.url);} else if (authGuard.action === "sign-out") {+ // Invalid JWT user: stay on this page (redirect: false) but still drop+ // the pageload v4 cache so a later hard load is not tagged as the+ // previous user.+ clearV4BetaEnabledSentryTag();signOut({ redirect: false });}}, [authGuard, router]);diff --git a/web/src/features/auth/lib/signOut.clienttest.ts b/web/src/features/auth/lib/signOut.clienttest.tsnew file mode 100644index 0000000000..da6c8a06d3--- /dev/null+++ b/web/src/features/auth/lib/signOut.clienttest.ts@@ -0,0 +1,46 @@+// @vitest-environment node++import { signOutCleanly } from "@/src/features/auth/lib/signOut";++const { signOutMock, clearMock, sessionStorageClear } = vi.hoisted(() => ({+ signOutMock: vi.fn(async () => undefined),+ clearMock: vi.fn(),+ sessionStorageClear: vi.fn(),+}));++vi.mock("next-auth/react", () => ({+ signOut: signOutMock,+}));++vi.mock("posthog-js", () => ({+ default: { reset: vi.fn() },+}));++vi.mock("@/src/env.mjs", () => ({ env: {} }));++vi.mock("@/src/utils/sentryV4BetaTag", () => ({+ clearV4BetaEnabledSentryTag: clearMock,+}));++describe("signOutCleanly", () => {+ beforeEach(() => {+ signOutMock.mockClear();+ clearMock.mockClear();+ sessionStorageClear.mockClear();+ vi.stubGlobal("sessionStorage", { clear: sessionStorageClear });+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("clears the v4 pageload cache before NextAuth redirects", async () => {+ await signOutCleanly();++ expect(clearMock).toHaveBeenCalledTimes(1);+ expect(signOutMock).toHaveBeenCalledTimes(1);+ expect(clearMock.mock.invocationCallOrder[0]).toBeLessThan(+ signOutMock.mock.invocationCallOrder[0],+ );+ });+});diff --git a/web/src/features/auth/lib/signOut.ts b/web/src/features/auth/lib/signOut.tsindex 37ef1a9f9c..f310e8dd4d 100644--- a/web/src/features/auth/lib/signOut.ts+++ b/web/src/features/auth/lib/signOut.ts@@ -1,6 +1,7 @@import { signOut } from "next-auth/react";import posthog from "posthog-js";import { env } from "@/src/env.mjs";+import { clearV4BetaEnabledSentryTag } from "@/src/utils/sentryV4BetaTag";/*** Canonical client-side sign-out.@@ -14,6 +15,10 @@ import { env } from "@/src/env.mjs";*/export const signOutCleanly = async () => {sessionStorage.clear();+ // NextAuth redirecting signOut unloads the page before UserTracking can+ // see `unauthenticated`. Drop the pageload cache here so the sign-in+ // hard load is not tagged with the previous user's v4 state.+ clearV4BetaEnabledSentryTag();if (env.NEXT_PUBLIC_POSTHOG_KEY && env.NEXT_PUBLIC_POSTHOG_HOST) {posthog.reset();}diff --git a/web/src/pages/account/settings/index.tsx b/web/src/pages/account/settings/index.tsxindex dd4efd5228..53734cdfe4 100644--- a/web/src/pages/account/settings/index.tsx+++ b/web/src/pages/account/settings/index.tsx@@ -25,7 +25,8 @@ import {DialogTitle,DialogTrigger,} from "@/src/components/ui/dialog";-import { useSession, signOut } from "next-auth/react";+import { useSession } from "next-auth/react";+import { signOutCleanly } from "@/src/features/auth/lib/signOut";import { SettingsDangerZone } from "@/src/components/SettingsDangerZone";import ContainerPage from "@/src/components/layouts/container-page";import { useRouter } from "next/router";@@ -154,7 +155,7 @@ function DeleteAccountButton() {description: "Your account has been successfully deleted.",});await new Promise((resolve) => setTimeout(resolve, 2000));- await signOut();+ await signOutCleanly();} catch (error) {reportNonTrpcError(error, "account");showErrorToast(diff --git a/web/src/utils/sentryV4BetaTag.clienttest.ts b/web/src/utils/sentryV4BetaTag.clienttest.tsindex fdea290308..5f751f9d9a 100644--- a/web/src/utils/sentryV4BetaTag.clienttest.ts+++ b/web/src/utils/sentryV4BetaTag.clienttest.ts@@ -2,6 +2,8 @@import {V4_BETA_ENABLED_SENTRY_TAG,+ V4_BETA_ENABLED_STORAGE_KEY,+ applyCachedV4BetaEnabledSentryTag,clearV4BetaEnabledSentryTag,setV4BetaEnabledSentryTag,} from "@/src/utils/sentryV4BetaTag";@@ -20,6 +22,26 @@ vi.mock("@sentry/nextjs", () => ({getRootSpan: getRootSpanMock,}));+const memory = new Map<string, string>();++const localStorageMock = {+ getItem: vi.fn((key: string) => memory.get(key) ?? null),+ setItem: vi.fn((key: string, value: string) => {+ memory.set(key, value);+ }),+ removeItem: vi.fn((key: string) => {+ memory.delete(key);+ }),+};++function stubLocalStorage() {+ memory.clear();+ localStorageMock.getItem.mockClear();+ localStorageMock.setItem.mockClear();+ localStorageMock.removeItem.mockClear();+ vi.stubGlobal("window", { localStorage: localStorageMock });+}+describe("setV4BetaEnabledSentryTag", () => {beforeEach(() => {setTagMock.mockClear();@@ -29,6 +51,10 @@ describe("setV4BetaEnabledSentryTag", () => {getRootSpanMock.mockReturnValue({ setAttribute: setAttributeMock });});+ afterEach(() => {+ vi.unstubAllGlobals();+ });+it("sets a boolean true tag without user or project ids", () => {getActiveSpanMock.mockReturnValue(undefined);@@ -78,6 +104,40 @@ describe("setV4BetaEnabledSentryTag", () => {expect(getRootSpanMock).not.toHaveBeenCalled();expect(setAttributeMock).not.toHaveBeenCalled();});++ it("persists true and false to localStorage for the next pageload", () => {+ stubLocalStorage();+ getActiveSpanMock.mockReturnValue(undefined);++ setV4BetaEnabledSentryTag(true);+ expect(memory.get(V4_BETA_ENABLED_STORAGE_KEY)).toBe("true");++ setV4BetaEnabledSentryTag(false);+ expect(memory.get(V4_BETA_ENABLED_STORAGE_KEY)).toBe("false");++ setV4BetaEnabledSentryTag(undefined);+ expect(memory.get(V4_BETA_ENABLED_STORAGE_KEY)).toBe("false");+ });++ it("still tags when localStorage writes throw", () => {+ vi.stubGlobal("window", {+ localStorage: {+ getItem: () => {+ throw new Error("quota");+ },+ setItem: () => {+ throw new Error("quota");+ },+ removeItem: () => {+ throw new Error("quota");+ },+ },+ });+ getActiveSpanMock.mockReturnValue(undefined);++ expect(() => setV4BetaEnabledSentryTag(true)).not.toThrow();+ expect(setTagMock).toHaveBeenCalledWith(V4_BETA_ENABLED_SENTRY_TAG, true);+ });});describe("clearV4BetaEnabledSentryTag", () => {@@ -89,6 +149,10 @@ describe("clearV4BetaEnabledSentryTag", () => {getRootSpanMock.mockReturnValue({ setAttribute: setAttributeMock });});+ afterEach(() => {+ vi.unstubAllGlobals();+ });+it("unsets the tag instead of labeling anonymous events as v3", () => {getActiveSpanMock.mockReturnValue(undefined);@@ -113,4 +177,77 @@ describe("clearV4BetaEnabledSentryTag", () => {undefined,);});++ it("removes the cached flag so the next pageload is not labeled as v3", () => {+ stubLocalStorage();+ memory.set(V4_BETA_ENABLED_STORAGE_KEY, "true");+ getActiveSpanMock.mockReturnValue(undefined);++ clearV4BetaEnabledSentryTag();++ expect(memory.has(V4_BETA_ENABLED_STORAGE_KEY)).toBe(false);+ });+});++describe("applyCachedV4BetaEnabledSentryTag", () => {+ beforeEach(() => {+ setTagMock.mockClear();+ getActiveSpanMock.mockClear();+ getRootSpanMock.mockClear();+ setAttributeMock.mockClear();+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("tags true from cache without stamping a root span", () => {+ stubLocalStorage();+ memory.set(V4_BETA_ENABLED_STORAGE_KEY, "true");++ applyCachedV4BetaEnabledSentryTag();++ expect(setTagMock).toHaveBeenCalledTimes(1);+ expect(setTagMock).toHaveBeenCalledWith(V4_BETA_ENABLED_SENTRY_TAG, true);+ expect(getActiveSpanMock).not.toHaveBeenCalled();+ });++ it("tags false from cache", () => {+ stubLocalStorage();+ memory.set(V4_BETA_ENABLED_STORAGE_KEY, "false");++ applyCachedV4BetaEnabledSentryTag();++ expect(setTagMock).toHaveBeenCalledWith(V4_BETA_ENABLED_SENTRY_TAG, false);+ });++ it("does not tag when the cache is missing", () => {+ stubLocalStorage();++ applyCachedV4BetaEnabledSentryTag();++ expect(setTagMock).not.toHaveBeenCalled();+ });++ it("does not tag garbage cache values", () => {+ stubLocalStorage();+ memory.set(V4_BETA_ENABLED_STORAGE_KEY, "yes");++ applyCachedV4BetaEnabledSentryTag();++ expect(setTagMock).not.toHaveBeenCalled();+ });++ it("does not throw when localStorage is denied", () => {+ vi.stubGlobal("window", {+ get localStorage() {+ throw new Error(+ "Failed to read the 'localStorage' property from 'Window': Access is denied for this document.",+ );+ },+ });++ expect(() => applyCachedV4BetaEnabledSentryTag()).not.toThrow();+ expect(setTagMock).not.toHaveBeenCalled();+ });});diff --git a/web/src/utils/sentryV4BetaTag.ts b/web/src/utils/sentryV4BetaTag.tsindex 5a99ab11cc..9bc8ab6fcf 100644--- a/web/src/utils/sentryV4BetaTag.ts+++ b/web/src/utils/sentryV4BetaTag.ts@@ -6,6 +6,67 @@ import { getActiveSpan, getRootSpan, setTag } from "@sentry/nextjs";*/export const V4_BETA_ENABLED_SENTRY_TAG = "v4BetaEnabled";+/**+ * Last-known v4 flag for the next hard pageload. Isolation-scope tags are+ * copied onto the pageload transaction at `Sentry.init`, which runs before+ * session hydrate. Not the intro-dialog key (`v4-beta-intro-dialog-seen`).+ */+export const V4_BETA_ENABLED_STORAGE_KEY = "v4BetaEnabled";++function getLocalStorage(): Storage | undefined {+ if (typeof window === "undefined") {+ return undefined;+ }+ try {+ return window.localStorage;+ } catch {+ return undefined;+ }+}++function readCachedV4BetaEnabled(): boolean | null {+ const storage = getLocalStorage();+ if (!storage) {+ return null;+ }+ try {+ const raw = storage.getItem(V4_BETA_ENABLED_STORAGE_KEY);+ if (raw === "true") {+ return true;+ }+ if (raw === "false") {+ return false;+ }+ return null;+ } catch {+ return null;+ }+}++function writeCachedV4BetaEnabled(enabled: boolean): void {+ const storage = getLocalStorage();+ if (!storage) {+ return;+ }+ try {+ storage.setItem(V4_BETA_ENABLED_STORAGE_KEY, enabled ? "true" : "false");+ } catch {+ // quota / private mode — tagging still proceeds+ }+}++function removeCachedV4BetaEnabled(): void {+ const storage = getLocalStorage();+ if (!storage) {+ return;+ }+ try {+ storage.removeItem(V4_BETA_ENABLED_STORAGE_KEY);+ } catch {+ // quota / private mode+ }+}+function stampActiveRootSpan(value: boolean | undefined): void {const activeSpan = getActiveSpan();if (!activeSpan) {@@ -16,20 +77,39 @@ function stampActiveRootSpan(value: boolean | undefined): void {getRootSpan(activeSpan).setAttribute(V4_BETA_ENABLED_SENTRY_TAG, value);}+/**+ * Tag the isolation scope from the last-known cache so the pageload+ * transaction that starts at `Sentry.init` is already labeled. Missing or+ * invalid cache leaves the tag unset rather than guessing false. Does not+ * stamp the root span: none exists yet.+ */+export function applyCachedV4BetaEnabledSentryTag(): void {+ try {+ const cached = readCachedV4BetaEnabled();+ if (cached === null) {+ return;+ }+ setTag(V4_BETA_ENABLED_SENTRY_TAG, cached);+ } catch {+ // Never block Sentry.init+ }+}+/*** Apply `v4BetaEnabled` to the current Sentry isolation scope and, when a- * pageload/navigation span is still open, to that root span. Isolation-scope- * tags are copied onto the transaction event at send time, which is after- * session hydrate on idle-until-quiet pageloads such as `/traces`.+ * pageload/navigation span is still open, to that root span. Also persist the+ * last-known flag so the next hard pageload can tag before `Sentry.init`.*/export function setV4BetaEnabledSentryTag(enabled: boolean | undefined): void {const value = enabled === true;+ writeCachedV4BetaEnabled(value);setTag(V4_BETA_ENABLED_SENTRY_TAG, value);stampActiveRootSpan(value);}-/** Drop the tag on logout so anonymous events are not labeled as v3. */+/** Drop the tag and cache on logout so anonymous events are not labeled as v3. */export function clearV4BetaEnabledSentryTag(): void {+ removeCachedV4BetaEnabled();setTag(V4_BETA_ENABLED_SENTRY_TAG, undefined);stampActiveRootSpan(undefined);}