Skip to content

: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.

feat(tracing): add viewer and sql scene tabs behind a flag (#105033)

Jon McCallumc25b27f874a1
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index ed3c8c53197b..11f975293577 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -532,6 +532,7 @@ export const FEATURE_FLAGS = {
TRACING_METRICS_TAB: 'tracing-metrics-tab', // owner: #team-apm — gates the Metrics tab (metrics correlated via exemplars) in the trace drawer
TRACING_OPERATIONS_VIEW: 'tracing-operations-view', // owner: #team-apm — gates the Operations (per-operation aggregate) tab in tracing
TRACING_SAVED_VIEWS: 'tracing-saved-views', // owner: #team-apm — gates saved views (saved filter sets) in tracing
+ TRACING_SCENE_TABS: 'tracing-scene-tabs', // owner: #team-apm — gates the Viewer and SQL tabs in the tracing scene
TRACING_SESSION_PERSON_LINKS: 'tracing-session-person-links', // owner: #team-apm — gates span attribute links to session replay and persons, plus the tracing correlation settings
TRACING_SPAN_ERROR_BADGES: 'tracing-span-error-badges', // owner: #team-apm, badges span rows whose session hit errors and adds the Errors tab to the trace drawer
TRACING_UI_V2: 'tracing-ui-v2',
diff --git a/products/tracing/frontend/TracingAgentIntegration.tsx b/products/tracing/frontend/TracingAgentIntegration.tsx
index b99398dd851d..4842976ded13 100644
--- a/products/tracing/frontend/TracingAgentIntegration.tsx
+++ b/products/tracing/frontend/TracingAgentIntegration.tsx
@@ -21,7 +21,7 @@ import { tracingViewerLogic } from './tracingViewerLogic'
* query-apm-spans call back onto the open viewer, and opens the trace an apm-trace-get call
* fetched in the drawer. Resolves the viewer instance from the enclosing BindLogic. Renders nothing.
*/
-export function TracingAgentIntegration(): null {
+export function TracingAgentIntegration({ sceneTabIsViewer }: { sceneTabIsViewer: boolean }): null {
const { filters } = useValues(tracingFiltersLogic)
const { setFilters } = useActions(tracingFiltersLogic)
const { selectedTraceId, selectedSpanId } = useValues(tracingViewerLogic)
@@ -47,6 +47,9 @@ export function TracingAgentIntegration(): null {
useMcpToolApplyBack({
tools: ['query-apm-spans'],
targetKey: `tracing-viewer:${TRACING_SCENE_VIEWER_ID}`,
+ // Only mirror onto the viewer while the user is looking at it, so a query the agent runs while
+ // the user is on the SQL tab does not silently rewrite the hidden viewer's filters.
+ active: sceneTabIsViewer,
onApply: (_event, { innerInput }) => {
if (!innerInput) {
return
diff --git a/products/tracing/frontend/TracingScene.tsx b/products/tracing/frontend/TracingScene.tsx
index df4636d479c5..95feb0fe3b0a 100644
--- a/products/tracing/frontend/TracingScene.tsx
+++ b/products/tracing/frontend/TracingScene.tsx
@@ -3,12 +3,13 @@ import { router } from 'kea-router'
import posthog from 'posthog-js'
import { useCallback, useMemo } from 'react'
-import { LemonButton, LemonModal, Link } from '@posthog/lemon-ui'
+import { LemonButton, LemonModal, LemonTabs, Link } from '@posthog/lemon-ui'
import { FEATURE_FLAGS } from 'lib/constants'
import { IconFeedback } from 'lib/lemon-ui/icons'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
+import { cn } from 'lib/utils/css-classes'
import { SceneExport } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
@@ -35,7 +36,7 @@ import { tracingDataLogic } from './tracingDataLogic'
import { TracingDisplayBar } from './TracingDisplayBar'
import { TracingFilterBar } from './TracingFilterBar'
import { TRACING_SCENE_VIEWER_ID, tracingFiltersLogic } from './tracingFiltersLogic'
-import { tracingSceneLogic } from './tracingSceneLogic'
+import { type TracingSceneTab, tracingSceneLogic } from './tracingSceneLogic'
import { TracingSparkline } from './TracingSparkline'
import { tracingViewerLogic } from './tracingViewerLogic'
import type { Span, SpanInspectorTab } from './types'
@@ -52,6 +53,7 @@ export const scene: SceneExport = {
export default function TracingScene(): JSX.Element {
const { featureFlags } = useValues(featureFlagLogic)
const sceneLogic = tracingSceneLogic()
+ const { activeSceneTab } = useValues(sceneLogic)
// Keep filters + data + viewer logic alive across React unmounts by attaching them to the scene root.
useAttachedLogic(tracingFiltersLogic({ id: TRACING_SCENE_VIEWER_ID }), sceneLogic)
useAttachedLogic(tracingDataLogic({ id: TRACING_SCENE_VIEWER_ID }), sceneLogic)
@@ -68,7 +70,7 @@ export default function TracingScene(): JSX.Element {
<BindLogic logic={tracingFiltersLogic} props={{ id: TRACING_SCENE_VIEWER_ID }}>
<BindLogic logic={tracingDataLogic} props={{ id: TRACING_SCENE_VIEWER_ID }}>
<BindLogic logic={tracingViewerLogic} props={{ id: TRACING_SCENE_VIEWER_ID }}>
- <TracingAgentIntegration />
+ <TracingAgentIntegration sceneTabIsViewer={activeSceneTab === 'viewer'} />
<TracingSceneContents />
</BindLogic>
</BindLogic>
@@ -115,6 +117,8 @@ function TracingSceneContents(): JSX.Element {
showHeatmap,
activeTracingTab,
compareActive,
+ sceneTabsEnabled,
+ activeSceneTab,
} = useValues(tracingSceneLogic())
const { featureFlags } = useValues(featureFlagLogic)
const {
@@ -132,6 +136,7 @@ function TracingSceneContents(): JSX.Element {
setSort,
setChartType,
applyHeatmapBrush,
+ selectSceneTab,
} = useActions(tracingSceneLogic())
const { addProductIntent } = useActions(teamLogic)
const { facetRailCollapsed } = useValues(tracingConfigLogic)
@@ -231,7 +236,18 @@ function TracingSceneContents(): JSX.Element {
</>
}
/>
- <>
+ {sceneTabsEnabled && (
+ <LemonTabs<TracingSceneTab>
+ activeKey={activeSceneTab}
+ onChange={selectSceneTab}
+ tabs={[
+ { key: 'viewer', label: 'Viewer' },
+ { key: 'sql', label: 'SQL' },
+ ]}
+ sceneInset
+ />
+ )}
+ <div className={cn('flex flex-col gap-y-4 flex-1 min-h-0', activeSceneTab !== 'viewer' && 'hidden')}>
<TracingFilterBar />
<SceneDivider />
<TracingSparkline
@@ -310,7 +326,7 @@ function TracingSceneContents(): JSX.Element {
)}
</div>
</div>
- </>
+ </div>
<TraceDrawer
isOpen={isTraceOpen}
traceId={selectedTraceId}
diff --git a/products/tracing/frontend/tracingSceneLogic.test.ts b/products/tracing/frontend/tracingSceneLogic.test.ts
index 83efa555bbaf..8580db120e32 100644
--- a/products/tracing/frontend/tracingSceneLogic.test.ts
+++ b/products/tracing/frontend/tracingSceneLogic.test.ts
@@ -160,6 +160,42 @@ describe('tracingSceneLogic', () => {
expect(logic.values.displayMode).toBe('spans')
})
+ it.each([
+ ['already loaded', () => mountAt({ tab: 'sql', serviceNames: JSON.stringify(['checkout']) })],
+ [
+ 'arriving after the URL parse',
+ () => {
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_SCENE_TABS]: true })
+ mountAt({ tab: 'sql', serviceNames: JSON.stringify(['checkout']) })
+ featureFlagLogic.actions.setFeatureFlags([], {})
+ },
+ ],
+ ])('drops a tab=sql deep link when the scene tabs flag is off, with flags %s', (_, mount) => {
+ mount()
+ expect(logic.values.sceneTab).toBe('viewer')
+ expect(router.values.searchParams).not.toHaveProperty('tab')
+ expect(router.values.searchParams.serviceNames).toEqual(['checkout'])
+ })
+
+ it('restores the SQL tab from a deep link without dropping the filter params', () => {
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_SCENE_TABS]: true })
+ mountAt({ tab: 'sql', serviceNames: JSON.stringify(['checkout']) })
+ expect(logic.values.activeSceneTab).toBe('sql')
+ expect(router.values.searchParams).toMatchObject({ tab: 'sql', serviceNames: ['checkout'] })
+
+ logic.actions.selectSceneTab('viewer')
+ expect(router.values.searchParams).not.toHaveProperty('tab')
+ expect(router.values.searchParams.serviceNames).toEqual(['checkout'])
+
+ logic.actions.selectSceneTab('sql')
+ featureFlagLogic.actions.setFeatureFlags([], {})
+ expect(logic.values.activeSceneTab).toBe('viewer')
+ expect(router.values.searchParams).not.toHaveProperty('tab')
+
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_SCENE_TABS]: true })
+ expect(logic.values.activeSceneTab).toBe('viewer')
+ })
+
// Guards the operations-tab rate denominator: on the operations tab the aggregate must always
// cover the whole selected range (compare: false), even with a comparison active. A filter
// change fires both the windowed aggregate (via runQuery) and the explicit full-range one, and
diff --git a/products/tracing/frontend/tracingSceneLogic.ts b/products/tracing/frontend/tracingSceneLogic.ts
index 5e1097ea3a49..a46e237f3090 100644
--- a/products/tracing/frontend/tracingSceneLogic.ts
+++ b/products/tracing/frontend/tracingSceneLogic.ts
@@ -50,9 +50,14 @@ import type { Span, SpanInspectorTab } from './types'
/** What each result row represents: a trace (its root span), an individual span, or an operation aggregate. */
export type TracingDisplayMode = TracingViewMode | 'operations'
+const TRACING_SCENE_TABS = ['viewer', 'sql'] as const
+export type TracingSceneTab = (typeof TRACING_SCENE_TABS)[number]
+const DEFAULT_SCENE_TAB: TracingSceneTab = 'viewer'
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tracingSceneLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
+ receivedFeatureFlags: boolean // featureFlagLogic
errorBadgeByRow: Map<string, SpanErrorBadge> // spanErrorsLogic
aggregation: {
current: AggregatedSpanRow[]
@@ -108,10 +113,13 @@ export interface tracingSceneLogicValues {
sessionErrorBadgesEnabled: boolean // tracingViewerLogic
traceIdentity: TraceIdentity // tracingViewerLogic
traceSessionId: string | null // tracingViewerLogic
+ activeSceneTab: TracingSceneTab
activeTracingTab: 'operations' | 'traces'
breadcrumbs: Breadcrumb[]
displayMode: TracingDisplayMode
operationsViewEnabled: boolean
+ sceneTab: TracingSceneTab
+ sceneTabsEnabled: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -246,12 +254,18 @@ export interface tracingSceneLogicActions {
selectSpan: (spanId: string | null) => {
spanId: string | null
} // tracingViewerLogic
+ selectSceneTab: (tab: TracingSceneTab) => {
+ tab: 'sql' | 'viewer'
+ }
setActiveTracingTab: (tab: 'operations' | 'traces') => {
tab: 'operations' | 'traces'
}
setDisplayMode: (mode: TracingDisplayMode) => {
mode: TracingDisplayMode
}
+ setSceneTab: (tab: TracingSceneTab) => {
+ tab: 'sql' | 'viewer'
+ }
syncUrl: () => {
value: true
}
@@ -266,6 +280,8 @@ export interface tracingSceneLogicMeta {
activeTracingTab: 'operations' | 'traces',
filters: TracingFilters
) => TracingDisplayMode
+ sceneTabsEnabled: (featureFlags: FeatureFlagsSet) => boolean
+ activeSceneTab: (sceneTabsEnabled: boolean, sceneTab: 'sql' | 'viewer') => TracingSceneTab
}
}
@@ -286,7 +302,7 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
connect(() => ({
values: [
featureFlagLogic,
- ['featureFlags'],
+ ['featureFlags', 'receivedFeatureFlags'],
tracingDataLogic({ id: TRACING_SCENE_VIEWER_ID }),
[
'spans',
@@ -372,6 +388,8 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
syncUrl: true,
setActiveTracingTab: (tab: 'traces' | 'operations') => ({ tab }),
setDisplayMode: (mode: TracingDisplayMode) => ({ mode }),
+ setSceneTab: (tab: TracingSceneTab) => ({ tab }),
+ selectSceneTab: (tab: TracingSceneTab) => ({ tab }),
}),
reducers({
@@ -381,6 +399,12 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
setActiveTracingTab: (_, { tab }) => tab,
},
],
+ sceneTab: [
+ DEFAULT_SCENE_TAB as TracingSceneTab,
+ {
+ setSceneTab: (_, { tab }) => tab,
+ },
+ ],
}),
selectors({
@@ -409,9 +433,22 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
): TracingDisplayMode =>
operationsViewEnabled && activeTracingTab === 'operations' ? 'operations' : filters.viewMode,
],
+ sceneTabsEnabled: [
+ (s) => [s.featureFlags],
+ (featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.TRACING_SCENE_TABS],
+ ],
+ activeSceneTab: [
+ (s) => [s.sceneTabsEnabled, s.sceneTab],
+ (sceneTabsEnabled: boolean, sceneTab: TracingSceneTab): TracingSceneTab =>
+ sceneTabsEnabled ? sceneTab : DEFAULT_SCENE_TAB,
+ ],
}),
listeners(({ actions, values }) => ({
+ selectSceneTab: ({ tab }) => {
+ actions.setSceneTab(tab)
+ actions.syncUrl()
+ },
// The keyed data logic already captured the interaction and re-ran the query;
// the scene's jobs are the URL write and scene-tab side effects.
handleFilterChange: () => {
@@ -453,6 +490,11 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
) {
actions.fetchAggregation({ fullRange: true })
}
+ // Flags can arrive after the URL parse, so a tab=sql deep link waits here for the verdict.
+ if (!values.sceneTabsEnabled && values.sceneTab !== DEFAULT_SCENE_TAB) {
+ actions.setSceneTab(DEFAULT_SCENE_TAB)
+ actions.syncUrl()
+ }
},
setActiveTracingTab: ({ tab }) => {
if (tab === 'operations') {
@@ -614,6 +656,14 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
hasChanges = true
}
+ const sceneTabsDisabled = values.receivedFeatureFlags && !values.sceneTabsEnabled
+ const sceneTabFromUrl = sceneTabsDisabled
+ ? DEFAULT_SCENE_TAB
+ : (TRACING_SCENE_TABS.find((tab) => tab === searchParams.tab) ?? DEFAULT_SCENE_TAB)
+ if (sceneTabFromUrl !== values.sceneTab) {
+ actions.setSceneTab(sceneTabFromUrl)
+ }
+
if (hasChanges) {
actions.setFilters(filtersFromUrl)
} else if (!values.hasRunQuery) {
@@ -636,6 +686,10 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
} else if (!traceFromUrl && values.selectedTraceId) {
actions.closeTrace()
}
+
+ if (sceneTabsDisabled && searchParams.tab !== undefined) {
+ actions.syncUrl()
+ }
},
})),
@@ -686,6 +740,9 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
if (values.filters.chartType !== DEFAULT_CHART_TYPE) {
searchParams.chart = values.filters.chartType
}
+ if (values.activeSceneTab !== DEFAULT_SCENE_TAB) {
+ searchParams.tab = values.activeSceneTab
+ }
return [router.values.location.pathname, searchParams, router.values.hashParams, { replace: true }]
}