🪢 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
feat(web): tag Sentry browser transactions with v4BetaEnabled (#16348)
Nikita Kabardin26c055153f3b
diff --git a/web/src/features/events/hooks/useV4Beta.ts b/web/src/features/events/hooks/useV4Beta.tsindex 37d48aebf8..c4313c9b41 100644--- a/web/src/features/events/hooks/useV4Beta.ts+++ b/web/src/features/events/hooks/useV4Beta.ts@@ -3,6 +3,7 @@ import { api } from "@/src/utils/api";import { useCallback, useState } from "react";import posthog from "posthog-js";import { V4_BETA_ENABLED_POSTHOG_PROPERTY } from "@/src/features/posthog-analytics/usePostHogClientCapture";+import { setV4BetaEnabledSentryTag } from "@/src/utils/sentryV4BetaTag";type SetV4EnabledOptions = {onSuccess?: () => void | Promise<void>;@@ -38,6 +39,7 @@ export function useV4Beta() {posthog.register({[V4_BETA_ENABLED_POSTHOG_PROPERTY]: v4BetaEnabled,});+ setV4BetaEnabledSentryTag(v4BetaEnabled);await updateSession();await options?.onSuccess?.();},diff --git a/web/src/pages/_app.tsx b/web/src/pages/_app.tsxindex 29a8e4f091..0d289dabbe 100644--- a/web/src/pages/_app.tsx+++ b/web/src/pages/_app.tsx@@ -8,6 +8,10 @@ import Head from "next/head";import { type Session } from "next-auth";import { SessionProvider } from "next-auth/react";import { setUser } from "@sentry/nextjs";+import {+ clearV4BetaEnabledSentryTag,+ setV4BetaEnabledSentryTag,+} from "@/src/utils/sentryV4BetaTag";import { useSession } from "next-auth/react";import { TooltipProvider } from "@/src/components/ui/tooltip";import { CommandMenuProvider } from "@/src/features/command-k-menu/CommandMenuProvider";@@ -252,16 +256,18 @@ function UserTracking() {});}- // Sentry+ // Sentry — user identity stays on setUser; v4 is a boolean tag onlysetUser({email: sessionUser.email ?? undefined,id: sessionUser.id ?? undefined,});+ setV4BetaEnabledSentryTag(sessionUser.v4BetaEnabled);} else if (session.status === "unauthenticated") {lastIdentifiedUser.current = null;posthog.unregister(V4_BETA_ENABLED_POSTHOG_PROPERTY);// SentrysetUser(null);+ clearV4BetaEnabledSentryTag();}}, [sessionUser, session.status, region]);diff --git a/web/src/utils/sentryV4BetaTag.clienttest.ts b/web/src/utils/sentryV4BetaTag.clienttest.tsnew file mode 100644index 0000000000..fdea290308--- /dev/null+++ b/web/src/utils/sentryV4BetaTag.clienttest.ts@@ -0,0 +1,116 @@+// @vitest-environment node++import {+ V4_BETA_ENABLED_SENTRY_TAG,+ clearV4BetaEnabledSentryTag,+ setV4BetaEnabledSentryTag,+} from "@/src/utils/sentryV4BetaTag";++const { setTagMock, getActiveSpanMock, getRootSpanMock, setAttributeMock } =+ vi.hoisted(() => ({+ setTagMock: vi.fn(),+ getActiveSpanMock: vi.fn(),+ getRootSpanMock: vi.fn(),+ setAttributeMock: vi.fn(),+ }));++vi.mock("@sentry/nextjs", () => ({+ setTag: setTagMock,+ getActiveSpan: getActiveSpanMock,+ getRootSpan: getRootSpanMock,+}));++describe("setV4BetaEnabledSentryTag", () => {+ beforeEach(() => {+ setTagMock.mockClear();+ getActiveSpanMock.mockClear();+ getRootSpanMock.mockClear();+ setAttributeMock.mockClear();+ getRootSpanMock.mockReturnValue({ setAttribute: setAttributeMock });+ });++ it("sets a boolean true tag without user or project ids", () => {+ getActiveSpanMock.mockReturnValue(undefined);++ setV4BetaEnabledSentryTag(true);++ expect(setTagMock).toHaveBeenCalledTimes(1);+ expect(setTagMock).toHaveBeenCalledWith("v4BetaEnabled", true);+ expect(typeof setTagMock.mock.calls[0]![1]).toBe("boolean");+ });++ it("coerces missing / false session values to boolean false", () => {+ getActiveSpanMock.mockReturnValue(undefined);++ setV4BetaEnabledSentryTag(false);+ setV4BetaEnabledSentryTag(undefined);++ expect(setTagMock).toHaveBeenNthCalledWith(+ 1,+ V4_BETA_ENABLED_SENTRY_TAG,+ false,+ );+ expect(setTagMock).toHaveBeenNthCalledWith(+ 2,+ V4_BETA_ENABLED_SENTRY_TAG,+ false,+ );+ });++ it("stamps the in-flight root span so pageload spans pick up the tag after hydrate", () => {+ const childSpan = { name: "child" };+ getActiveSpanMock.mockReturnValue(childSpan);++ setV4BetaEnabledSentryTag(true);++ expect(getRootSpanMock).toHaveBeenCalledWith(childSpan);+ expect(setAttributeMock).toHaveBeenCalledWith(+ V4_BETA_ENABLED_SENTRY_TAG,+ true,+ );+ });++ it("skips span stamping when no pageload/navigation span is active", () => {+ getActiveSpanMock.mockReturnValue(undefined);++ setV4BetaEnabledSentryTag(true);++ expect(getRootSpanMock).not.toHaveBeenCalled();+ expect(setAttributeMock).not.toHaveBeenCalled();+ });+});++describe("clearV4BetaEnabledSentryTag", () => {+ beforeEach(() => {+ setTagMock.mockClear();+ getActiveSpanMock.mockClear();+ getRootSpanMock.mockClear();+ setAttributeMock.mockClear();+ getRootSpanMock.mockReturnValue({ setAttribute: setAttributeMock });+ });++ it("unsets the tag instead of labeling anonymous events as v3", () => {+ getActiveSpanMock.mockReturnValue(undefined);++ clearV4BetaEnabledSentryTag();++ expect(setTagMock).toHaveBeenCalledTimes(1);+ expect(setTagMock).toHaveBeenCalledWith(+ V4_BETA_ENABLED_SENTRY_TAG,+ undefined,+ );+ });++ it("removes the attribute from an in-flight root span on logout", () => {+ const childSpan = { name: "child" };+ getActiveSpanMock.mockReturnValue(childSpan);++ clearV4BetaEnabledSentryTag();++ expect(getRootSpanMock).toHaveBeenCalledWith(childSpan);+ expect(setAttributeMock).toHaveBeenCalledWith(+ V4_BETA_ENABLED_SENTRY_TAG,+ undefined,+ );+ });+});diff --git a/web/src/utils/sentryV4BetaTag.ts b/web/src/utils/sentryV4BetaTag.tsnew file mode 100644index 0000000000..5a99ab11cc--- /dev/null+++ b/web/src/utils/sentryV4BetaTag.ts@@ -0,0 +1,35 @@+import { getActiveSpan, getRootSpan, setTag } from "@sentry/nextjs";++/**+ * Isolation-scope tag so browser pageload/navigation metric alerts can split+ * v3 vs v4. Boolean only — never user or project IDs (those stay on `setUser`).+ */+export const V4_BETA_ENABLED_SENTRY_TAG = "v4BetaEnabled";++function stampActiveRootSpan(value: boolean | undefined): void {+ const activeSpan = getActiveSpan();+ if (!activeSpan) {+ return;+ }+ // `undefined` removes the attribute so logout does not leave a stale v3/v4+ // label on an in-flight pageload/navigation span.+ getRootSpan(activeSpan).setAttribute(V4_BETA_ENABLED_SENTRY_TAG, value);+}++/**+ * 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`.+ */+export function setV4BetaEnabledSentryTag(enabled: boolean | undefined): void {+ const value = enabled === true;+ setTag(V4_BETA_ENABLED_SENTRY_TAG, value);+ stampActiveRootSpan(value);+}++/** Drop the tag on logout so anonymous events are not labeled as v3. */+export function clearV4BetaEnabledSentryTag(): void {+ setTag(V4_BETA_ENABLED_SENTRY_TAG, undefined);+ stampActiveRootSpan(undefined);+}