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): merge display bar toggles into one traces/spans/operations control (#72336)

Jon McCallum3cce1fbf118a
diff --git a/products/tracing/frontend/TracingDisplayBar.tsx b/products/tracing/frontend/TracingDisplayBar.tsx
index f1cbc47ab891..4b5796598cc5 100644
--- a/products/tracing/frontend/TracingDisplayBar.tsx
+++ b/products/tracing/frontend/TracingDisplayBar.tsx
@@ -1,7 +1,7 @@
import { useActions, useValues } from 'kea'
-import { IconChevronLeft, IconChevronRight, IconList, IconListTree } from '@posthog/icons'
-import { LemonButton, LemonSegmentedButton } from '@posthog/lemon-ui'
+import { IconChevronLeft, IconChevronRight, IconList, IconListTree, IconStack } from '@posthog/icons'
+import { LemonButton, LemonSegmentedButton, type LemonSegmentedButtonOption } from '@posthog/lemon-ui'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
@@ -9,30 +9,57 @@ import { humanFriendlyNumber } from 'lib/utils/numbers'
import { CompareMenuButton } from './components/Comparison/CompareMenuButton'
import { tracingConfigLogic } from './tracingConfigLogic'
-import { tracingFiltersLogic, type TracingViewMode } from './tracingFiltersLogic'
-import { tracingSceneLogic } from './tracingSceneLogic'
+import { tracingSceneLogic, type TracingDisplayMode } from './tracingSceneLogic'
/**
- * The bar above the results, mirroring logs' LogsDisplayBar, grouped by scope:
+ * The bar above the results, mirroring logs' LogsDisplayBar:
*
- * - persistent-left "frame": controls that apply to the whole results region — the facet
- * rail toggle, the Traces ⇄ Operations view switch, and the matching-count indicator.
- * - contextual-right: traces-view-only controls (Traces ⇄ Spans granularity, Compare),
- * hidden entirely on the Operations view where neither applies.
+ * - left: the facet rail toggle, the row-mode selector (Traces ⇄ Spans ⇄ Operations — what
+ * each result row represents), and the matching-count indicator.
+ * - right: Compare, hidden on the Operations view where it doesn't apply.
*/
export function TracingDisplayBar(): JSX.Element {
- const { activeTracingTab, totalMatchingFilters, filters, compareActive } = useValues(tracingSceneLogic())
- const { setActiveTracingTab } = useActions(tracingSceneLogic())
- const { setViewMode } = useActions(tracingFiltersLogic())
+ const { totalMatchingFilters, compareActive, displayMode, operationsViewEnabled } = useValues(tracingSceneLogic())
+ const { setDisplayMode } = useActions(tracingSceneLogic())
const { featureFlags } = useValues(featureFlagLogic)
const { facetRailCollapsed } = useValues(tracingConfigLogic)
const { setFacetRailCollapsed } = useActions(tracingConfigLogic)
const facetRailEnabled = !!featureFlags[FEATURE_FLAGS.TRACING_FACET_RAIL]
- const operationsViewEnabled = !!featureFlags[FEATURE_FLAGS.TRACING_OPERATIONS_VIEW]
- const inTracesView = !operationsViewEnabled || activeTracingTab !== 'operations'
+ const inTracesView = displayMode !== 'operations'
const showCount = inTracesView && !compareActive && totalMatchingFilters > 0
+ // data-attrs keep the names from the two controls this one replaced, for analytics continuity.
+ const displayModeOptions: LemonSegmentedButtonOption<TracingDisplayMode>[] = [
+ {
+ value: 'traces',
+ label: 'Traces',
+ icon: <IconListTree />,
+ tooltip: 'Group matching spans by trace, one row per trace (its root span)',
+ 'data-attr': 'tracing-view-mode-traces',
+ },
+ {
+ value: 'spans',
+ label: 'Spans',
+ icon: <IconList />,
+ tooltip: 'Show every matching span individually, including child spans',
+ // The comparison table aggregates by operation, so span granularity has no effect there.
+ disabledReason: compareActive ? 'Not available while comparing' : undefined,
+ 'data-attr': 'tracing-view-mode-spans',
+ },
+ ...(operationsViewEnabled
+ ? [
+ {
+ value: 'operations' as const,
+ label: 'Operations',
+ icon: <IconStack />,
+ tooltip: 'Aggregate matching spans by operation (service and span name)',
+ 'data-attr': 'tracing-display-tab-operations',
+ },
+ ]
+ : []),
+ ]
+
return (
<div className="flex items-center justify-between gap-2 flex-wrap">
<div className="flex items-center gap-2 flex-wrap">
@@ -47,55 +74,23 @@ export function TracingDisplayBar(): JSX.Element {
{facetRailCollapsed ? 'Show filters' : 'Hide filters'}
</LemonButton>
)}
- {operationsViewEnabled && (
- <LemonSegmentedButton<'traces' | 'operations'>
- size="small"
- value={activeTracingTab}
- onChange={setActiveTracingTab}
- options={[
- { value: 'traces', label: 'Traces', 'data-attr': 'tracing-display-tab-traces' },
- {
- value: 'operations',
- label: 'Operations',
- 'data-attr': 'tracing-display-tab-operations',
- },
- ]}
- />
- )}
+ <LemonSegmentedButton<TracingDisplayMode>
+ size="small"
+ value={displayMode}
+ onChange={setDisplayMode}
+ options={displayModeOptions}
+ />
{/* No loading guard: keep the last (view-mode-independent) count visible across reloads,
so a Traces/Spans switch doesn't flicker the label out and back in. */}
{showCount && (
<span className="text-muted text-xs">
- {humanFriendlyNumber(totalMatchingFilters)} {filters.viewMode === 'spans' ? 'spans' : 'traces'}{' '}
+ {humanFriendlyNumber(totalMatchingFilters)} {displayMode === 'spans' ? 'spans' : 'traces'}{' '}
matching filters
</span>
)}
</div>
{inTracesView && (
<div className="flex items-center gap-1.5 flex-wrap">
- {!compareActive && (
- <LemonSegmentedButton<TracingViewMode>
- size="small"
- value={filters.viewMode}
- onChange={setViewMode}
- options={[
- {
- value: 'traces',
- label: 'Traces',
- icon: <IconListTree />,
- tooltip: 'Group matching spans by trace — one row per trace (its root span)',
- 'data-attr': 'tracing-view-mode-traces',
- },
- {
- value: 'spans',
- label: 'Spans',
- icon: <IconList />,
- tooltip: 'Show every matching span individually, including child spans',
- 'data-attr': 'tracing-view-mode-spans',
- },
- ]}
- />
- )}
<CompareMenuButton />
</div>
)}
diff --git a/products/tracing/frontend/tracingSceneLogic.test.ts b/products/tracing/frontend/tracingSceneLogic.test.ts
index 2273ad2aafe8..9ee0aac01cc4 100644
--- a/products/tracing/frontend/tracingSceneLogic.test.ts
+++ b/products/tracing/frontend/tracingSceneLogic.test.ts
@@ -1,6 +1,8 @@
import { router } from 'kea-router'
import api from 'lib/api'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
import { initKeaTests } from '~/test/init'
@@ -13,6 +15,9 @@ describe('tracingSceneLogic', () => {
beforeEach(() => {
initKeaTests()
+ // The featureFlags reducer persists to localStorage — reset so flags can't leak across tests.
+ featureFlagLogic.mount()
+ featureFlagLogic.actions.setFeatureFlags([], {})
jest.spyOn(api.tracing, 'listSpans').mockResolvedValue({ results: [], hasMore: false })
jest.spyOn(api.tracing, 'sparkline').mockResolvedValue({ results: [] })
jest.spyOn(api.tracing, 'count').mockResolvedValue({ count: 0, traceCount: 0 })
@@ -32,6 +37,10 @@ describe('tracingSceneLogic', () => {
return tracingFiltersLogic({ id: TRACING_SCENE_VIEWER_ID })
}
+ const enableOperationsView = (): void => {
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_OPERATIONS_VIEW]: true })
+ }
+
it('migrates legacy compare=true links to the custom time comparison', () => {
const filtersLogic = mountAt({ compare: 'true' })
expect(filtersLogic.values.comparison).toEqual({
@@ -74,6 +83,63 @@ describe('tracingSceneLogic', () => {
expect(filtersLogic.values.comparison).toMatchObject({ preset: 'yesterday' })
})
+ it('opens the operations view from a ?view=operations deep link', () => {
+ enableOperationsView()
+ mountAt({ view: 'operations' })
+ expect(logic.values.activeTracingTab).toBe('operations')
+ })
+
+ it('ignores ?view=operations when the operations view flag is off', () => {
+ const filtersLogic = mountAt({ view: 'operations' })
+ expect(logic.values.activeTracingTab).toBe('traces')
+ expect(filtersLogic.values.viewMode).toBe('traces')
+ })
+
+ it('reconciles the operations tab when the flag resolves after a ?view=operations deep link', () => {
+ // Flag not yet loaded: the deep link reads as the default traces view.
+ mountAt({ view: 'operations' })
+ expect(logic.values.activeTracingTab).toBe('traces')
+
+ // Flag resolves late — the tab and URL reconcile without a navigation.
+ enableOperationsView()
+ expect(logic.values.activeTracingTab).toBe('operations')
+ expect(logic.values.displayMode).toBe('operations')
+ expect(router.values.searchParams.view).toBe('operations')
+ })
+
+ it('resets the operations tab to traces when the flag is disabled mid-session', () => {
+ enableOperationsView()
+ mountAt({ view: 'operations' })
+ expect(logic.values.activeTracingTab).toBe('operations')
+
+ featureFlagLogic.actions.setFeatureFlags([], {})
+ expect(logic.values.activeTracingTab).toBe('traces')
+ expect(logic.values.displayMode).toBe('traces')
+ expect(router.values.searchParams).not.toHaveProperty('view')
+ })
+
+ it('writes the operations mode to the URL without clobbering the spans granularity', () => {
+ enableOperationsView()
+ const filtersLogic = mountAt({ view: 'spans' })
+ expect(logic.values.displayMode).toBe('spans')
+
+ logic.actions.setDisplayMode('operations')
+ expect(logic.values.activeTracingTab).toBe('operations')
+ expect(router.values.searchParams.view).toBe('operations')
+ // The spans granularity only applies to the list view — it must survive underneath.
+ expect(filtersLogic.values.viewMode).toBe('spans')
+
+ logic.actions.setDisplayMode('spans')
+ expect(logic.values.activeTracingTab).toBe('traces')
+ expect(router.values.searchParams.view).toBe('spans')
+
+ // Back/forward to a non-operations URL while on the operations tab resets the tab.
+ logic.actions.setDisplayMode('operations')
+ router.actions.push('/tracing', { view: 'spans' })
+ expect(logic.values.activeTracingTab).toBe('traces')
+ expect(logic.values.displayMode).toBe('spans')
+ })
+
// 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
@@ -81,6 +147,7 @@ describe('tracingSceneLogic', () => {
// two dispatches and the operations table would silently divide by the narrow compare sub-window.
it('keeps the operations aggregate on the full range when a filter changes while comparing', async () => {
silenceKeaLoadersErrors()
+ enableOperationsView()
const filtersLogic = mountAt({ comparison: JSON.stringify({ mode: 'time', preset: 'custom' }) })
expect(filtersLogic.values.compareActive).toBe(true)
@@ -104,6 +171,7 @@ describe('tracingSceneLogic', () => {
// scene's full-range refetch the operations table would divide by the narrow compare sub-window.
it('keeps the operations aggregate on the full range when filters are restored while comparing', async () => {
silenceKeaLoadersErrors()
+ enableOperationsView()
const filtersLogic = mountAt({ comparison: JSON.stringify({ mode: 'time', preset: 'custom' }) })
expect(filtersLogic.values.compareActive).toBe(true)
@@ -112,10 +180,12 @@ describe('tracingSceneLogic', () => {
await new Promise((resolve) => setTimeout(resolve, 0))
aggregateSpy.mockClear()
- // Restore a different filter set the way back/forward navigation does.
+ // Restore a different filter set the way back/forward navigation does. The URL keeps
+ // view=operations — a restored URL without it would (correctly) leave the operations tab.
router.actions.push('/tracing', {
comparison: JSON.stringify({ mode: 'time', preset: 'custom' }),
serviceNames: 'svc-a',
+ view: 'operations',
})
await new Promise((resolve) => setTimeout(resolve, 0))
diff --git a/products/tracing/frontend/tracingSceneLogic.ts b/products/tracing/frontend/tracingSceneLogic.ts
index 836d901dd29a..bea1252d873f 100644
--- a/products/tracing/frontend/tracingSceneLogic.ts
+++ b/products/tracing/frontend/tracingSceneLogic.ts
@@ -1,8 +1,11 @@
import { deepEqual as equal } from 'fast-equals'
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { router, urlToAction } from 'kea-router'
+import { subscriptions } from 'kea-subscriptions'
import { DEFAULT_UNIVERSAL_GROUP_FILTER } from 'lib/components/UniversalFilters/constants'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic, type FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { parseTagsFilter } from 'lib/utils/url'
import { Params } from 'scenes/sceneTypes'
@@ -40,8 +43,12 @@ import type {
import { tracingViewerLogic } from './tracingViewerLogic'
import type { Span } from './types'
+/** What each result row represents: a trace (its root span), an individual span, or an operation aggregate. */
+export type TracingDisplayMode = TracingViewMode | 'operations'
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tracingSceneLogicValues {
+ featureFlags: FeatureFlagsSet // featureFlagLogic
aggregation: {
current: AggregatedSpanRow[]
previous: AggregatedSpanRow[] | null
@@ -93,6 +100,8 @@ export interface tracingSceneLogicValues {
selectedTraceTs: string | null // tracingViewerLogic
activeTracingTab: 'operations' | 'traces'
breadcrumbs: Breadcrumb[]
+ displayMode: TracingDisplayMode
+ operationsViewEnabled: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -208,12 +217,32 @@ export interface tracingSceneLogicActions {
setActiveTracingTab: (tab: 'operations' | 'traces') => {
tab: 'operations' | 'traces'
}
+ setDisplayMode: (mode: TracingDisplayMode) => {
+ mode: TracingDisplayMode
+ }
syncUrl: () => {
value: true
}
}
-export type tracingSceneLogicType = MakeLogicType<tracingSceneLogicValues, tracingSceneLogicActions>
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface tracingSceneLogicMeta {
+ __keaTypeGenInternalSelectorTypes: {
+ operationsViewEnabled: (featureFlags: FeatureFlagsSet) => boolean
+ displayMode: (
+ operationsViewEnabled: boolean,
+ activeTracingTab: 'operations' | 'traces',
+ filters: TracingFilters
+ ) => TracingDisplayMode
+ }
+}
+
+export type tracingSceneLogicType = MakeLogicType<
+ tracingSceneLogicValues,
+ tracingSceneLogicActions,
+ Record<string, any>,
+ tracingSceneLogicMeta
+>
export const tracingSceneLogic = kea<tracingSceneLogicType>([
path(['products', 'tracing', 'frontend', 'tracingSceneLogic']),
@@ -224,6 +253,8 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
// Mirrors the logsSceneLogic / logsViewerLogic boundary.
connect(() => ({
values: [
+ featureFlagLogic,
+ ['featureFlags'],
tracingDataLogic({ id: TRACING_SCENE_VIEWER_ID }),
[
'spans',
@@ -299,6 +330,7 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
actions({
syncUrl: true,
setActiveTracingTab: (tab: 'traces' | 'operations') => ({ tab }),
+ setDisplayMode: (mode: TracingDisplayMode) => ({ mode }),
}),
reducers({
@@ -321,6 +353,21 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
},
],
],
+ operationsViewEnabled: [
+ (s) => [s.featureFlags],
+ (featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.TRACING_OPERATIONS_VIEW],
+ ],
+ // The one user-facing mode: what each result row represents. Spans two states —
+ // traces/spans is the viewMode filter, operations is the scene tab.
+ displayMode: [
+ (s) => [s.operationsViewEnabled, s.activeTracingTab, s.filters],
+ (
+ operationsViewEnabled: boolean,
+ activeTracingTab: 'operations' | 'traces',
+ filters: TracingFilters
+ ): TracingDisplayMode =>
+ operationsViewEnabled && activeTracingTab === 'operations' ? 'operations' : filters.viewMode,
+ ],
}),
listeners(({ actions, values }) => ({
@@ -361,6 +408,42 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
actions.fetchAggregation()
}
},
+ // Maps the one display-mode control onto the two underlying states, with exactly one
+ // URL write per interaction: setViewMode cascades its own syncUrl (via the data logic's
+ // handleFilterChange), so this only writes directly when that cascade won't run.
+ setDisplayMode: ({ mode }) => {
+ if (mode === 'operations') {
+ if (values.activeTracingTab !== 'operations') {
+ actions.setActiveTracingTab('operations')
+ }
+ actions.syncUrl()
+ return
+ }
+ if (values.activeTracingTab !== 'traces') {
+ actions.setActiveTracingTab('traces')
+ }
+ if (mode !== values.filters.viewMode) {
+ actions.setViewMode(mode)
+ } else {
+ actions.syncUrl()
+ }
+ },
+ })),
+
+ subscriptions(({ actions, values }) => ({
+ // Feature flags can resolve AFTER the initial URL parse, and urlToAction only re-runs on
+ // URL changes. Without this, a late-arriving flag leaves the tab and the URL disagreeing:
+ // enabling it on a ?view=operations page keeps the tab stuck on traces, and disabling it
+ // mid-session strands the tab on operations. Re-reconcile the tab against the current URL
+ // whenever the flag flips, then write the URL back through the flag-aware displayMode.
+ operationsViewEnabled: (enabled) => {
+ const shouldBeOperations = enabled && router.values.searchParams.view === 'operations'
+ if (shouldBeOperations === (values.activeTracingTab === 'operations')) {
+ return
+ }
+ actions.setActiveTracingTab(shouldBeOperations ? 'operations' : 'traces')
+ actions.syncUrl()
+ },
})),
urlToAction(({ actions, values }) => ({
@@ -451,10 +534,23 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
hasChanges = true
}
- const viewModeFromUrl = searchParams.view === 'spans' ? 'spans' : DEFAULT_VIEW_MODE
- if (viewModeFromUrl !== values.filters.viewMode) {
- filtersFromUrl.viewMode = viewModeFromUrl
- hasChanges = true
+ // `view` spans two states: 'operations' round-trips the scene tab, 'spans'/absent
+ // round-trips the viewMode filter. While the URL says operations the viewMode filter
+ // is left untouched — it has no effect on that view and shouldn't be reset under it.
+ // The whole operations leg is flag-gated on BOTH the read side (here) and the write
+ // side (buildUrl, via the flag-aware displayMode selector): gating only one of them
+ // would make them fight over the tab state.
+ const operationsEnabled = values.operationsViewEnabled
+ const operationsFromUrl = operationsEnabled && searchParams.view === 'operations'
+ if (operationsEnabled && operationsFromUrl !== (values.activeTracingTab === 'operations')) {
+ actions.setActiveTracingTab(operationsFromUrl ? 'operations' : 'traces')
+ }
+ if (!operationsFromUrl) {
+ const viewModeFromUrl = searchParams.view === 'spans' ? 'spans' : DEFAULT_VIEW_MODE
+ if (viewModeFromUrl !== values.filters.viewMode) {
+ filtersFromUrl.viewMode = viewModeFromUrl
+ hasChanges = true
+ }
}
const chartTypeFromUrl = searchParams.chart === 'heatmap' ? 'heatmap' : DEFAULT_CHART_TYPE
@@ -528,8 +624,9 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
if (comparisonParam) {
searchParams.comparison = comparisonParam
}
- if (values.filters.viewMode !== DEFAULT_VIEW_MODE) {
- searchParams.view = values.filters.viewMode
+ // displayMode already folds in the flag gate and resolves to viewMode off-operations.
+ if (values.displayMode !== DEFAULT_VIEW_MODE) {
+ searchParams.view = values.displayMode
}
if (values.filters.chartType !== DEFAULT_CHART_TYPE) {
searchParams.chart = values.filters.chartType