:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(aio): switch between the old and new trace view (#110841)
Carlos Marchal47a5ffbdae4c
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 0611e4fc9aa5..c7ff38fa5ccb 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -8754,6 +8754,14 @@ snapshots:hash: v1.k794b7964.70302b15caaea759a5d9db2ae961f99b0ad7213e975e226aee93c0c359315510.zf3TUMVc-R1rm5IyKav2tNPs54xcTB98Dvm2mGeWEjwscenes-app-ai-observability-trace-view-trace-view--timeline-mode--light:hash: v1.k794b7964.efb8b37e950b518d97cd742ef44b75d249ac94d327c5a2965d4a7697ec60c01e._j6psG-8_2R2QiOtYdPV4doYlMJNoP5LsmxS5A7OCFw+ scenes-app-ai-observability-trace-view-trace-view-switch--on-new-view--dark:+ hash: v1.k794b7964.705e0647746424a98de2bbf79737ac43b3f60b57ae45b8f0e5d0cb5f01226aad.vSallcE6n-MHVAkWEOsfs2D1X--pp40NBjawl5otiOk+ scenes-app-ai-observability-trace-view-trace-view-switch--on-new-view--light:+ hash: v1.k794b7964.a0a6aa1d50cf3ba8efab106b863b781ec7963f26b4ac15c081846ef1a5f26be8.FENbz_MteTxpQoOtuabm33w_0L5OXGTEPrn-PnWRpVs+ scenes-app-ai-observability-trace-view-trace-view-switch--on-old-view--dark:+ hash: v1.k794b7964.13ae77563960d8df1279788e5a1b2a17b0f38c4469bb92d3a22f3cc569903681.v3gbz8mdvwkQ2INsmiXInoQZTCBj3KGPrqcynRaxFCU+ scenes-app-ai-observability-trace-view-trace-view-switch--on-old-view--light:+ hash: v1.k794b7964.de72868c75c31f3ba3d9813b5f86064d7dbf37bc1a896b8f13032dc102758876.Xpri2jtS8YkCfKQmwMkmtGGWN8oTE_vfUOaiXtOwJK0scenes-app-aigateway--empty--dark:hash: v1.k794b7964.daa11f1fa049181ba0f2a1682df377cca565a9d072188b77107ad9d0649e9623.mzdAzwhxfNqVM1hHDGRozAkPE5jqxA8pkYj_h67yw7cscenes-app-aigateway--empty--light:diff --git a/products/ai_observability/frontend/AIObservabilityTraceScene.tsx b/products/ai_observability/frontend/AIObservabilityTraceScene.tsxindex d9143891b5dc..e66e9b395bb6 100644--- a/products/ai_observability/frontend/AIObservabilityTraceScene.tsx+++ b/products/ai_observability/frontend/AIObservabilityTraceScene.tsx@@ -95,7 +95,9 @@ import { LLMInputOutput } from './LLMInputOutput'import { llmPersonsLazyLoaderLogic } from './llmPersonsLazyLoaderLogic'import { normalizeMessages } from './messageNormalization'import { openInPlayground } from './playground/llmPlaygroundPromptsLogic'+import { TraceViewSwitch } from './redesign/trace/components/TraceViewSwitch'import { TraceScene } from './redesign/trace/TraceScene'+import { traceViewPreferenceLogic } from './redesign/trace/traceViewPreferenceLogic'import { ReviewQueuePickerModal } from './reviewQueues/ReviewQueuePickerModal'import { reviewQueuesApi } from './reviewQueues/reviewQueuesApi'import { SearchHighlight } from './SearchHighlight'@@ -423,8 +425,19 @@ function TraceNavigation(): JSX.Element {}function TraceSceneForFlag(): JSX.Element {- const { featureFlags } = useValues(featureFlagLogic)- return featureFlags[FEATURE_FLAGS.AI_OBSERVABILITY_TRACE_REDESIGN] ? <TraceScene /> : <AIObservabilityTraceScene />+ const { activeView } = useValues(traceViewPreferenceLogic)+ const { switchView } = useActions(traceViewPreferenceLogic)+ if (activeView === 'legacy') {+ return <AIObservabilityTraceScene />+ }+ return (+ <>+ <div className="mb-2">+ <TraceViewSwitch view="new" onSwitch={switchView} />+ </div>+ <TraceScene />+ </>+ )}export const scene: SceneExport = {@@ -503,6 +516,8 @@ function TraceSceneWrapper(): JSX.Element {? { name: previousQueueTraceId ? 'Previous trace' : 'Reviews', path: backPath, key: 'reviews' }: { name: 'Traces', path: backPath, key: 'traces' }const showTraceNavigation = !!featureFlags[FEATURE_FLAGS.LLM_ANALYTICS_TRACE_NAVIGATION]+ const { isSwitchAvailable } = useValues(traceViewPreferenceLogic)+ const { switchView } = useActions(traceViewPreferenceLogic)return (<>@@ -519,7 +534,12 @@ function TraceSceneWrapper(): JSX.Element {name={trace.id}resourceType={{ type: 'llm_analytics' }}forceBackTo={forceBackTo}- actions={showTraceNavigation ? <TraceNavigation /> : undefined}+ actions={+ <>+ {isSwitchAvailable && <TraceViewSwitch view="legacy" onSwitch={switchView} />}+ {showTraceNavigation && <TraceNavigation />}+ </>+ }noBorder/><div className="flex items-start justify-between">diff --git a/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx b/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsxindex c81b9b1b917c..186bd79ce5f9 100644--- a/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx+++ b/products/ai_observability/frontend/redesign/trace/adapter/traceSceneWiring.test.tsx@@ -1,6 +1,8 @@-import { cleanup, render, waitFor } from '@testing-library/react'+import { cleanup, fireEvent, render, waitFor } from '@testing-library/react'import { router } from 'kea-router'+import { FEATURE_FLAGS } from 'lib/constants'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { urls } from 'scenes/urls'import { useMocks } from '~/mocks/jest'@@ -11,6 +13,8 @@ import { aiObservabilityTraceLogic } from '../../../aiObservabilityTraceLogic'import { TraceScene } from '../TraceScene'import { makeEvent, makeTrace, makeTraceResource } from './testFixtures'+const FLAG = FEATURE_FLAGS.AI_OBSERVABILITY_TRACE_REDESIGN+jest.mock('~/queries/query', () => ({ ...jest.requireActual('~/queries/query'), performQuery: jest.fn() }))jest.mock('../../../utils', () => ({...jest.requireActual('../../../utils'),@@ -53,4 +57,42 @@ describe('TraceScene', () => {expect((await findAllByText('answer-billing-question')).length).toBeGreaterThan(0)await waitFor(() => expect(queryByText('Two seats were added.')).not.toBeNull())})++ describe('behind the flag gate', () => {+ beforeEach(() => {+ useMocks({+ get: { '/api/projects/:team_id/ai_observability/traces/:id/': () => [200, makeTraceResource()] },+ })+ })++ async function renderTraceRoute(flagEnabled: boolean): Promise<ReturnType<typeof render>> {+ localStorage.clear()+ initKeaTests()+ featureFlagLogic.mount()+ featureFlagLogic.actions.setFeatureFlags([FLAG], { [FLAG]: flagEnabled })+ jest.mocked(performQuery).mockResolvedValue({ results: [makeTrace({ events: [generation] })] })+ aiObservabilityTraceLogic.mount()+ router.actions.push(urls.aiObservabilityTrace('trace-1'))+ // The old scene reads the project id while its modules load, so the import must follow initKeaTests.+ const { scene } = await import('../../../AIObservabilityTraceScene')+ const TraceRoute = scene.component+ return render(<TraceRoute />)+ }++ it('switches to the old view and back when the flag is on', async () => {+ const { findByText } = await renderTraceRoute(true)++ fireEvent.click(await findByText('Switch to the old view'))+ fireEvent.click(await findByText('Switch to the new view'))++ expect(await findByText('Switch to the old view')).toBeTruthy()+ })++ it('offers no switch on the old view when the flag is off', async () => {+ const { container, queryByText } = await renderTraceRoute(false)++ await waitFor(() => expect(container.querySelector('.AIObservabilityTraceScene__wrapper')).not.toBeNull())+ expect(queryByText('Switch to the new view')).toBeNull()+ })+ })})diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceViewSwitch.stories.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceViewSwitch.stories.tsxnew file mode 100644index 000000000000..08f2b7be9fd0--- /dev/null+++ b/products/ai_observability/frontend/redesign/trace/components/TraceViewSwitch.stories.tsx@@ -0,0 +1,16 @@+import type { Meta, StoryObj } from '@storybook/react'++import { TraceViewSwitch } from './TraceViewSwitch'++const meta: Meta<typeof TraceViewSwitch> = {+ title: 'Scenes-App/AI observability/Trace view/Trace view switch',+ component: TraceViewSwitch,+ args: { onSwitch: () => {} },+}+export default meta++type Story = StoryObj<typeof TraceViewSwitch>++export const OnNewView: Story = { args: { view: 'new' } }++export const OnOldView: Story = { args: { view: 'legacy' } }diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceViewSwitch.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceViewSwitch.tsxnew file mode 100644index 000000000000..9bb3cae783b7--- /dev/null+++ b/products/ai_observability/frontend/redesign/trace/components/TraceViewSwitch.tsx@@ -0,0 +1,26 @@+import { Link } from '@posthog/lemon-ui'++import { TraceViewChoice } from '../types'++export interface TraceViewSwitchProps {+ view: TraceViewChoice+ onSwitch: (view: TraceViewChoice) => void+}++export function TraceViewSwitch({ view, onSwitch }: TraceViewSwitchProps): JSX.Element {+ const isNewView = view === 'new'+ return (+ <div+ className="flex flex-wrap items-center justify-end gap-x-2 text-xs text-secondary"+ data-attr="trace-view-switch"+ >+ <span>{isNewView ? "You're on the new trace view." : "There's a new trace view."}</span>+ <Link+ onClick={() => onSwitch(isNewView ? 'legacy' : 'new')}+ data-attr={isNewView ? 'trace-view-switch-to-legacy' : 'trace-view-switch-to-new'}+ >+ {isNewView ? 'Switch to the old view' : 'Switch to the new view'}+ </Link>+ </div>+ )+}diff --git a/products/ai_observability/frontend/redesign/trace/traceViewPreferenceLogic.test.ts b/products/ai_observability/frontend/redesign/trace/traceViewPreferenceLogic.test.tsnew file mode 100644index 000000000000..765e27a4b06c--- /dev/null+++ b/products/ai_observability/frontend/redesign/trace/traceViewPreferenceLogic.test.ts@@ -0,0 +1,63 @@+import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'++import { FEATURE_FLAGS } from 'lib/constants'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'++import { initKeaTests } from '~/test/init'++import { traceViewPreferenceLogic } from './traceViewPreferenceLogic'+import { TraceViewChoice } from './types'++const FLAG = FEATURE_FLAGS.AI_OBSERVABILITY_TRACE_REDESIGN++describe('traceViewPreferenceLogic', () => {+ let logic: ReturnType<typeof traceViewPreferenceLogic.build>++ function mountWithFlag(enabled: boolean): void {+ initKeaTests()+ featureFlagLogic.mount()+ featureFlagLogic.actions.setFeatureFlags([FLAG], { [FLAG]: enabled })+ logic = traceViewPreferenceLogic()+ logic.mount()+ }++ beforeEach(() => {+ localStorage.clear()+ })++ afterEach(() => {+ logic.unmount()+ jest.restoreAllMocks()+ })++ it.each<{ flag: boolean; switches: TraceViewChoice[]; activeView: TraceViewChoice; isSwitchAvailable: boolean }>([+ { flag: true, switches: [], activeView: 'new', isSwitchAvailable: true },+ { flag: true, switches: ['legacy'], activeView: 'legacy', isSwitchAvailable: true },+ { flag: true, switches: ['legacy', 'new'], activeView: 'new', isSwitchAvailable: true },+ { flag: false, switches: [], activeView: 'legacy', isSwitchAvailable: false },+ ])(+ 'shows the $activeView view when the flag is $flag after switches $switches',+ ({ flag, switches, activeView, isSwitchAvailable }) => {+ mountWithFlag(flag)+ switches.forEach((view) => logic.actions.switchView(view))+ expectLogic(logic).toMatchValues({ activeView, isSwitchAvailable })+ }+ )++ it('keeps the chosen view after a reload', () => {+ mountWithFlag(true)+ logic.actions.switchView('legacy')++ logic.unmount()+ mountWithFlag(true)+ expectLogic(logic).toMatchValues({ activeView: 'legacy' })+ })++ it('reports each switch with the view it switched to', () => {+ const capture = jest.spyOn(posthog, 'capture')+ mountWithFlag(true)+ logic.actions.switchView('legacy')+ expect(capture).toHaveBeenCalledWith('llma trace view switched', { to: 'legacy' })+ })+})diff --git a/products/ai_observability/frontend/redesign/trace/traceViewPreferenceLogic.ts b/products/ai_observability/frontend/redesign/trace/traceViewPreferenceLogic.tsnew file mode 100644index 000000000000..a4820219de6f--- /dev/null+++ b/products/ai_observability/frontend/redesign/trace/traceViewPreferenceLogic.ts@@ -0,0 +1,72 @@+import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'+import posthog from 'posthog-js'++import { FEATURE_FLAGS } from 'lib/constants'+import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'++import { TraceViewChoice } from './types'++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface traceViewPreferenceLogicValues {+ featureFlags: FeatureFlagsSet // featureFlagLogic+ activeView: TraceViewChoice+ isSwitchAvailable: boolean+ preferredView: TraceViewChoice+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface traceViewPreferenceLogicActions {+ switchView: (view: TraceViewChoice) => {+ view: TraceViewChoice+ }+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface traceViewPreferenceLogicMeta {+ __keaTypeGenInternalSelectorTypes: {+ isSwitchAvailable: (featureFlags: FeatureFlagsSet) => boolean+ activeView: (isSwitchAvailable: boolean, preferredView: TraceViewChoice) => TraceViewChoice+ }+}++export type traceViewPreferenceLogicType = MakeLogicType<+ traceViewPreferenceLogicValues,+ traceViewPreferenceLogicActions,+ Record<string, any>,+ traceViewPreferenceLogicMeta+>++export const traceViewPreferenceLogic = kea<traceViewPreferenceLogicType>([+ path(['products', 'ai_observability', 'frontend', 'redesign', 'trace', 'traceViewPreferenceLogic']),+ connect(() => ({ values: [featureFlagLogic, ['featureFlags']] })),+ actions({+ switchView: (view: TraceViewChoice) => ({ view }),+ }),+ reducers({+ preferredView: [+ 'new' as TraceViewChoice,+ // The key is fixed so that moving this logic does not reset the stored choice.+ { persist: true, storageKey: 'ai_observability.trace_view_choice' },+ {+ switchView: (_, { view }) => view,+ },+ ],+ }),+ selectors({+ isSwitchAvailable: [+ (s) => [s.featureFlags],+ (featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.AI_OBSERVABILITY_TRACE_REDESIGN],+ ],+ activeView: [+ (s) => [s.isSwitchAvailable, s.preferredView],+ (isSwitchAvailable: boolean, preferredView: TraceViewChoice): TraceViewChoice =>+ isSwitchAvailable ? preferredView : 'legacy',+ ],+ }),+ listeners(() => ({+ switchView: ({ view }) => {+ // A rename of this event or of the `to` property breaks every insight that filters on them.+ posthog.capture('llma trace view switched', { to: view })+ },+ })),+])diff --git a/products/ai_observability/frontend/redesign/trace/types.ts b/products/ai_observability/frontend/redesign/trace/types.tsindex 5a6693351aef..9956c67ca896 100644--- a/products/ai_observability/frontend/redesign/trace/types.ts+++ b/products/ai_observability/frontend/redesign/trace/types.ts@@ -4,6 +4,8 @@ export type TraceNodeKind = TraceNodeApi['kind']export type TraceMode = 'spans' | 'thread' | 'timeline'+export type TraceViewChoice = 'new' | 'legacy'+export type NodeDetailTab = 'messages' | 'details' | 'evals' | 'raw'export type MessageRole = 'system' | 'user' | 'assistant' | 'tool'