: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(tracing): add a flame graph view to the trace view (#120236)
Roy Cohen482ea4ac6878
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsxindex 3c8f48b23fbb..f4119f9050cf 100644--- a/frontend/src/lib/constants.tsx+++ b/frontend/src/lib/constants.tsx@@ -559,6 +559,7 @@ export const FEATURE_FLAGS = {TRACING_SETTINGS_RETENTION: 'tracing-settings-retention', // owner: #team-apm — gates the traces retention setting; mirrored by `TRACES_RETENTION_FLAG` in posthog/api/team.pyTRACING_SETTINGS_RETENTION_RULES: 'tracing-settings-retention-rules', // owner: #team-apm — gates the span retention rules in tracing settingsTRACING_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_TRACE_CALL_TREE: 'tracing-trace-call-tree', // owner: #team-apm — gates the call tree view toggle in the trace viewTRACING_TRACE_DIALOG: 'tracing-trace-dialog', // owner: #team-apm — opens traces in a dialog over the current page instead of navigating to tracingTRACING_TRACE_OPERATIONS_TAB: 'tracing-trace-operations-tab', // owner: #team-apm — gates the Operations tab in the trace view's span inspectorTRACING_UI_V2: 'tracing-ui-v2',diff --git a/products/tracing/frontend/TraceCompareFlame.test.tsx b/products/tracing/frontend/TraceCompareFlame.test.tsxnew file mode 100644index 000000000000..d2e95370ea2a--- /dev/null+++ b/products/tracing/frontend/TraceCompareFlame.test.tsx@@ -0,0 +1,48 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'++import type { SpanTreeNode } from '~/queries/schema/schema-general'++import { TraceCompareFlame } from './TraceCompareFlame'++const row = (name: string, count: number): SpanTreeNode => ({+ parent_service: '',+ parent_name: '<ROOT>',+ service_name: 'checkout-api',+ name,+ count,+ total_duration_nano: count * 1_000_000,+ avg_duration_nano: 1_000_000,+ p50_duration_nano: 1_000_000,+ p95_duration_nano: 1_000_000,+ p99_duration_nano: 1_000_000,+ p999_duration_nano: 1_000_000,+ error_count: 0,+ avg_start_offset_nano: 0,+ calls_per_parent_invocation: null,+})++describe('TraceCompareFlame', () => {+ afterEach(() => {+ cleanup()+ })++ // The compare modal passes a null `previous` before its first tree fetch ends, and when the response has no+ // comparison rows. Only a given call tree is a single trace, so a flame that reads a null `previous` as one drops+ // the modal's legend and relabels its bars.+ it('keeps the compare legend and plain labels in the compare modal without previous rows', () => {+ render(+ <TraceCompareFlame+ current={[row('cache GET', 3)]}+ previous={null}+ loading={false}+ surface="compare_modal"+ />+ )++ expect(screen.getByText('vanished')).toBeInTheDocument()+ expect(screen.getByText('cache GET')).toBeInTheDocument()+ expect(screen.queryByText('cache GET ×3')).toBeNull()+ })+})diff --git a/products/tracing/frontend/TraceCompareFlame.tsx b/products/tracing/frontend/TraceCompareFlame.tsxindex 08daf2727016..4b2e35fdb871 100644--- a/products/tracing/frontend/TraceCompareFlame.tsx+++ b/products/tracing/frontend/TraceCompareFlame.tsx@@ -1,24 +1,23 @@import clsx from 'clsx'-import { useEffect, useMemo, useState } from 'react'+import posthog from 'posthog-js'+import { useEffect, useMemo, useRef, useState } from 'react'import React from 'react'import { IconChevronRight } from '@posthog/icons'import { LemonDropdown, Link, SpinnerOverlay, Tooltip } from '@posthog/lemon-ui'+import { getSeriesColor } from 'lib/colors'import { humanFriendlyNumber } from 'lib/utils/numbers'import { SpanTreeNode } from '~/queries/schema/schema-general'-import { CHANGE_THRESHOLD, MIN_BASELINE_COUNT } from './compareUtils'+import { CHANGE_THRESHOLD, compareRowKey, MIN_BASELINE_COUNT } from './compareUtils'+import type { TraceViewSurface } from './components/TraceView/traceViewLogic'+import { countFlameNodes, findFlamePath, type FlameTreeNode } from './flameTree'import { formatDuration } from './TraceWaterfallView'-interface TreeNode {- serviceName: string- name: string- node: SpanTreeNode | null- previousNode: SpanTreeNode | null- children: TreeNode[]-}+/** Where the flame sits. Pinned: sent as the `surface` property of analytics events, so keep the values stable. */+export type TraceFlameSurface = TraceViewSurface | 'compare_modal'const ROW_HEIGHT_PX = 28const ROOT_KEY = '\u0000<ROOT>'@@ -28,23 +27,27 @@ const MIN_VISIBLE_FRACTION = 0.01const MIN_GROUPED_COUNT = 2/** Floor for the bundled cell so it's always clickable regardless of how small the bundle is. */const GROUPED_CELL_MIN_WIDTH_PX = 48+const NEUTRAL_BAR_COLOR = 'rgba(168, 168, 168, 0.6)'-function nodeKey(serviceName: string, name: string): string {- return `${serviceName}\u0000${name}`+type FlameSizeBy = 'p50' | 'total'++interface FlameDisplay {+ sizeBy: FlameSizeBy+ /** False for a single trace, which has no previous window to compare against. */+ hasBaseline: boolean+ serviceColorMap: Map<string, number> | undefined}-function nodeSize(n: TreeNode): number {- // Layout metric: max of current p50 and previous p50, so vanished and new nodes both- // claim non-zero space proportional to whichever window they appeared in.- return Math.max(n.node?.p50_duration_nano ?? 0, n.previousNode?.p50_duration_nano ?? 0)+// The same key as the operation rows of the tables, so that a caller can name a node by an operation's key.+function nodeKey(serviceName: string, name: string): string {+ return compareRowKey({ service_name: serviceName, name })}-function countDescendants(node: TreeNode): number {- let n = 1- for (const child of node.children) {- n += countDescendants(child)- }- return n+function nodeSize(n: FlameTreeNode, sizeBy: FlameSizeBy): number {+ const field = sizeBy === 'total' ? 'total_duration_nano' : 'p50_duration_nano'+ // Layout metric: max of the current and previous value, so vanished and new nodes both+ // claim non-zero space proportional to whichever window they appeared in.+ return Math.max(n.node?.[field] ?? 0, n.previousNode?.[field] ?? 0)}/**@@ -115,7 +118,7 @@ function mergeRows(rows: SpanTreeNode[]): SpanTreeNode {* Both windows contribute nodes; previous-only nodes still appear so vanished call sites* are visible in the diff.*/-function buildTree(current: SpanTreeNode[], previous: SpanTreeNode[] | null): TreeNode {+function buildTree(current: SpanTreeNode[], previous: SpanTreeNode[] | null): FlameTreeNode {const allNodes = new Map<string, { currentRows: SpanTreeNode[]; previousRows: SpanTreeNode[] }>()const childrenByParent = new Map<string, Set<string>>()@@ -149,7 +152,7 @@ function buildTree(current: SpanTreeNode[], previous: SpanTreeNode[] | null): Tr// (e.g. <ROOT> → A → B → A when traces are crafted recursively). Without this,// `build` would recurse indefinitely and freeze the browser tab. We skip any// child key that already appears on the current build path.- function build(key: string, serviceName: string, name: string, ancestors: Set<string>): TreeNode {+ function build(key: string, serviceName: string, name: string, ancestors: Set<string>): FlameTreeNode {const entry = allNodes.get(key) ?? { currentRows: [], previousRows: [] }const nextAncestors = new Set(ancestors)nextAncestors.add(key)@@ -211,10 +214,10 @@ function DeltaPct({ current, previous, higherIsWorse }: DeltaPctProps): JSX.Elemfunction deltaColor(current: SpanTreeNode | null, previous: SpanTreeNode | null): string {if (!current || !previous) {// New or vanished — neutral neon.- return 'rgba(168, 168, 168, 0.6)'+ return NEUTRAL_BAR_COLOR}if (previous.p50_duration_nano === 0) {- return 'rgba(168, 168, 168, 0.6)'+ return NEUTRAL_BAR_COLOR}// Same low-sample noise guard as the compare table's classification, so a node the table// calls unchanged can't render as a deep-red regression here.@@ -234,8 +237,36 @@ function deltaColor(current: SpanTreeNode | null, previous: SpanTreeNode | null)return 'rgba(120, 150, 200, 0.45)'}+function compareBarStyle(node: FlameTreeNode): React.CSSProperties {+ const current = node.node+ const previous = node.previousNode+ return {+ backgroundColor: deltaColor(current, previous),+ // Marker for "new in current window" (no previous data).+ outline: !previous && current ? '1px dashed rgba(80, 180, 100, 0.85)' : undefined,+ // Marker for "vanished" (no current data).+ opacity: !current && previous ? 0.5 : 1,+ }+}++function singleTraceBarStyle(+ node: FlameTreeNode,+ serviceColorMap: Map<string, number> | undefined+): React.CSSProperties {+ if (!serviceColorMap) {+ return { backgroundColor: NEUTRAL_BAR_COLOR }+ }+ // The waterfall uses the same map and the same error color, so a service keeps its color when the view changes.+ const color =+ (node.node?.error_count ?? 0) > 0 ? 'var(--danger)' : getSeriesColor(serviceColorMap.get(node.serviceName) ?? 0)+ return {+ backgroundColor: `color-mix(in srgb, ${color} 35%, transparent)`,+ borderLeft: `1px solid ${color}`,+ }+}+interface FlameRowProps {- node: TreeNode+ node: FlameTreeNodedepth: number/** This node's width as a fraction (0..1) of the row it lives in. */fraction: number@@ -243,14 +274,20 @@ interface FlameRowProps {selfPath: string[]/** Sets the active focus to a given path. */onFocus: (path: string[]) => void+ display: FlameDisplay}-function FlameRow({ node, fraction, selfPath, onFocus }: FlameRowProps): JSX.Element {- const own = nodeSize(node)- const color = deltaColor(node.node, node.previousNode)+function FlameRow({ node, fraction, selfPath, onFocus, display }: FlameRowProps): JSX.Element {+ const own = nodeSize(node, display.sizeBy)const current = node.nodeconst previous = node.previousNode+ // In a single trace a bar stands for the spans of one operation under one path of callers, so the count says+ // how many spans that is.+ const label =+ !display.hasBaseline && current && current.count > 1+ ? `${node.name} ×${humanFriendlyNumber(current.count)}`+ : node.nameconst fmtCount = (n: SpanTreeNode | null): string => (n ? humanFriendlyNumber(n.count) : '—')const fmtDur = (v: number | undefined): string => (v === undefined ? '—' : formatDuration(v))@@ -305,29 +342,34 @@ function FlameRow({ node, fraction, selfPath, onFocus }: FlameRowProps): JSX.EleclassName="flex items-center px-2 overflow-hidden min-w-0 text-xs font-mono cursor-pointer border-r border-b border-bg-bg transition-[filter] hover:brightness-125"style={{height: ROW_HEIGHT_PX,- backgroundColor: color,- // Marker for "new in current window" (no previous data).- outline: !previous && current ? '1px dashed rgba(80, 180, 100, 0.85)' : undefined,- // Marker for "vanished" (no current data).- opacity: !current && previous ? 0.5 : 1,+ ...(display.hasBaseline+ ? compareBarStyle(node)+ : singleTraceBarStyle(node, display.serviceColorMap)),}}>- <span className="truncate">{node.name}</span>+ <span className="truncate">{label}</span></div></Tooltip>{node.children.length > 0 && (- <ChildrenRow parent={node} parentTotal={own} ancestorPath={selfPath} onFocus={onFocus} />+ <ChildrenRow+ parent={node}+ parentTotal={own}+ ancestorPath={selfPath}+ onFocus={onFocus}+ display={display}+ />)}</div>)}interface ChildrenRowProps {- parent: TreeNode+ parent: FlameTreeNodeparentTotal: number/** Path from the synthetic root to the parent of these children. */ancestorPath: string[]onFocus: (path: string[]) => void+ display: FlameDisplay}/**@@ -337,18 +379,18 @@ interface ChildrenRowProps {* (< 1% of the visible row) are bundled into a "+N more" cell that opens a popover* with that subset rendered at full width.*/-function ChildrenRow({ parent, parentTotal, ancestorPath, onFocus }: ChildrenRowProps): JSX.Element {+function ChildrenRow({ parent, parentTotal, ancestorPath, onFocus, display }: ChildrenRowProps): JSX.Element {const childrenWithSize = parent.children.map((child) => ({child,- size: nodeSize(child),+ size: nodeSize(child, display.sizeBy),}))const totalSize = childrenWithSize.reduce((acc, c) => acc + c.size, 0)// Normalization base: use the larger of parent's own total and the summed children// total — the latter wins under concurrency so siblings always fit inside 100%.const base = Math.max(parentTotal, totalSize, 1)- const visible: { child: TreeNode; fraction: number }[] = []- const grouped: { child: TreeNode; fraction: number }[] = []+ const visible: { child: FlameTreeNode; fraction: number }[] = []+ const grouped: { child: FlameTreeNode; fraction: number }[] = []for (const { child, size } of childrenWithSize) {const fraction = size / baseif (fraction >= MIN_VISIBLE_FRACTION) {@@ -377,6 +419,7 @@ function ChildrenRow({ parent, parentTotal, ancestorPath, onFocus }: ChildrenRowfraction={fraction}selfPath={[...ancestorPath, nodeKey(child.serviceName, child.name)]}onFocus={onFocus}+ display={display}/>))}{grouped.length > 0 && (@@ -385,6 +428,7 @@ function ChildrenRow({ parent, parentTotal, ancestorPath, onFocus }: ChildrenRowfraction={groupedSum + remainder}ancestorPath={ancestorPath}onFocus={onFocus}+ display={display}/>)}</div>@@ -392,20 +436,21 @@ function ChildrenRow({ parent, parentTotal, ancestorPath, onFocus }: ChildrenRow}interface GroupedCellProps {- items: { child: TreeNode; fraction: number }[]+ items: { child: FlameTreeNode; fraction: number }[]/** Combined width fraction of the bundle inside the row. */fraction: numberancestorPath: string[]onFocus: (path: string[]) => void+ display: FlameDisplay}-function GroupedCell({ items, fraction, ancestorPath, onFocus }: GroupedCellProps): JSX.Element {+function GroupedCell({ items, fraction, ancestorPath, onFocus, display }: GroupedCellProps): JSX.Element {const [open, setOpen] = useState(false)// Count every span in the bundled subtrees, not just the top-level grouped children —// a single grouped child can hide a deep subtree of small spans.- const totalSpanCount = items.reduce((acc, i) => acc + countDescendants(i.child), 0)+ const totalSpanCount = items.reduce((acc, i) => acc + countFlameNodes(i.child), 0)// Inside the popover, redistribute the bundle's items to fill the full width.- const bundleTotal = items.reduce((acc, i) => acc + nodeSize(i.child), 0) || 1+ const bundleTotal = items.reduce((acc, i) => acc + nodeSize(i.child, display.sizeBy), 0) || 1return (<LemonDropdownvisible={open}@@ -416,18 +461,19 @@ function GroupedCell({ items, fraction, ancestorPath, onFocus }: GroupedCellProp<div className="flex w-full" style={{ minWidth: 480 }}>{items.slice()- .sort((a, b) => nodeSize(b.child) - nodeSize(a.child))+ .sort((a, b) => nodeSize(b.child, display.sizeBy) - nodeSize(a.child, display.sizeBy)).map((item) => (<FlameRowkey={nodeKey(item.child.serviceName, item.child.name)}node={item.child}depth={0}- fraction={nodeSize(item.child) / bundleTotal}+ fraction={nodeSize(item.child, display.sizeBy) / bundleTotal}selfPath={[...ancestorPath, nodeKey(item.child.serviceName, item.child.name)]}onFocus={(p) => {setOpen(false)onFocus(p)}}+ display={display}/>))}</div>@@ -452,52 +498,93 @@ function GroupedCell({ items, fraction, ancestorPath, onFocus }: GroupedCellProp}interface TraceCompareFlameProps {+ /**+ * A tree to draw instead of the one built from `current` and `previous`. A single trace passes the tree of its+ * spans, which keeps an operation's calls apart under each caller. The name-merged rows cannot do that. A given tree+ * draws without the compare legend, colors bars by service and labels a bar with its span count.+ */+ tree?: FlameTreeNodecurrent: SpanTreeNode[]previous: SpanTreeNode[] | nullloading: boolean/** Span name the modal was opened for — the flame initially focuses on this node. */initialSpanName?: string | null-}--/** DFS the tree for the first node matching `name`. Returns the path of node keys to it (excluding root). */-function findPathByName(root: TreeNode, name: string): string[] | null {- for (const child of root.children) {- if (child.name === name) {- return [nodeKey(child.serviceName, child.name)]- }- const sub = findPathByName(child, name)- if (sub) {- return [nodeKey(child.serviceName, child.name), ...sub]- }- }- return null+ /**+ * The `compareRowKey` of the operation to focus first. It takes the place of `initialSpanName`, because two services+ * can each run an operation of the same name.+ */+ initialFocusKey?: string | null+ /** Which metric sets each bar's width. 'p50' is the per-call cost (the compare view); 'total' is wall-clock time summed over the calls (a single trace). */+ sizeBy?: FlameSizeBy+ /** Colors a single trace's bars by service. Pass the waterfall's map so both views use the same color for a service. */+ serviceColorMap?: Map<string, number>+ /** Where the flame sits, for analytics. */+ surface: TraceFlameSurface}export function TraceCompareFlame({+ tree: givenTree,current,previous,loading,initialSpanName,+ initialFocusKey,+ sizeBy = 'p50',+ serviceColorMap,+ surface,}: TraceCompareFlameProps): JSX.Element {- const tree = useMemo(() => buildTree(current, previous), [current, previous])+ const tree = useMemo(() => givenTree ?? buildTree(current, previous), [givenTree, current, previous])+ // A given tree is the call tree of one trace, which has no previous window, so the compare legend and the new and+ // vanished markers would describe a comparison that does not exist. The compare modal always builds its tree from+ // rows and keeps them, also when its `previous` is null: before the first tree fetch ends, or when the response has+ // no comparison rows.+ const hasBaseline = givenTree === undefined+ const display = useMemo(+ (): FlameDisplay => ({ sizeBy, hasBaseline, serviceColorMap }),+ [sizeBy, hasBaseline, serviceColorMap]+ )// Focus path: keys of nodes from the synthetic root down to the currently focused span.// Each click on a flame bar appends; breadcrumb clicks truncate.const [focusPath, setFocusPath] = useState<string[]>([])- // Whenever the tree (or the initial span we were opened with) changes, reset focus to- // that span. The user may then drill in/out via flame clicks and breadcrumb segments.+ // Whenever the tree (or the initial operation we were opened with) changes, reset focus to+ // that operation. The user may then drill in/out via flame clicks and breadcrumb segments.+ // An initial operation that clears on the same tree keeps the drill-down. The trace view clears its+ // highlight when the person switches to another inspector tab, which is no request to leave the bar.+ const lastResetRef = useRef<FlameTreeNode | null>(null)useEffect(() => {- if (!initialSpanName) {- setFocusPath([])+ const sameTree = lastResetRef.current === tree+ lastResetRef.current = tree+ if (sameTree && !initialFocusKey && !initialSpanName) {return}- const path = findPathByName(tree, initialSpanName)- setFocusPath(path ?? [])- }, [tree, initialSpanName])+ const matches = initialFocusKey+ ? (node: FlameTreeNode): boolean => nodeKey(node.serviceName, node.name) === initialFocusKey+ : initialSpanName+ ? (node: FlameTreeNode): boolean => node.name === initialSpanName+ : null+ const path = matches ? findFlamePath(tree, matches) : null+ setFocusPath(path ? path.map((node) => nodeKey(node.serviceName, node.name)) : [])+ }, [tree, initialSpanName, initialFocusKey])++ // Only bar and breadcrumb clicks come through here, so the event counts the person's own drill and not a focus+ // that the flame sets for itself, such as the initial operation or a reset when the tree changes. A click on the+ // bar that already has the focus changes nothing, so it sends nothing.+ const focusByClick = (path: string[]): void => {+ if (path.length !== focusPath.length) {+ // pinned: analytics event and property names, so renaming them breaks the insights built on them+ posthog.capture('tracing trace call tree focused', {+ direction: path.length > focusPath.length ? 'in' : 'out',+ depth: path.length,+ surface,+ })+ }+ setFocusPath(path)+ }// Walk the tree to resolve the focused node + its ancestor chain.- const breadcrumb: TreeNode[] = []- let focused: TreeNode = tree+ const breadcrumb: FlameTreeNode[] = []+ let focused: FlameTreeNode = treefor (const key of focusPath) {const next = focused.children.find((c) => nodeKey(c.serviceName, c.name) === key)if (!next) {@@ -521,69 +608,79 @@ export function TraceCompareFlame({return (<div className="flex flex-col gap-2">- <FocusBreadcrumb breadcrumb={breadcrumb} onSelect={(depth) => setFocusPath(focusPath.slice(0, depth))} />+ <FocusBreadcrumb breadcrumb={breadcrumb} onSelect={(depth) => focusByClick(focusPath.slice(0, depth))} /><div className="overflow-x-auto">{focusPath.length === 0 ? (// No focus → render top-level children directly (the synthetic root has no real bar).<ChildrenRowparent={focused}- parentTotal={focused.children.reduce((sum, c) => sum + nodeSize(c), 0)}+ parentTotal={focused.children.reduce((sum, c) => sum + nodeSize(c, sizeBy), 0)}ancestorPath={focusPath}- onFocus={setFocusPath}+ onFocus={focusByClick}+ display={display}/>) : (// Focused span sits at the top as a full-width bar; its children stack below.<div className="flex">- <FlameRow node={focused} depth={0} fraction={1} selfPath={focusPath} onFocus={setFocusPath} />+ <FlameRow+ node={focused}+ depth={0}+ fraction={1}+ selfPath={focusPath}+ onFocus={focusByClick}+ display={display}+ /></div>)}</div>- <div className="flex gap-3 text-xs text-muted">- <span className="flex items-center gap-1">- <span- className="inline-block w-3 h-3 rounded"- style={{ backgroundColor: 'rgba(220, 80, 80, 0.7)' }}- />- slower- </span>- <span className="flex items-center gap-1">- <span- className="inline-block w-3 h-3 rounded"- style={{ backgroundColor: 'rgba(80, 180, 100, 0.7)' }}- />- faster- </span>- <span className="flex items-center gap-1">- <span- className="inline-block w-3 h-3 rounded"- style={{ backgroundColor: 'rgba(120, 150, 200, 0.45)' }}- />- similar- </span>- <span className="flex items-center gap-1">- <span- className="inline-block w-3 h-3 rounded"- style={{- backgroundColor: 'rgba(168, 168, 168, 0.6)',- outline: '1px dashed rgba(80, 180, 100, 0.85)',- }}- />- new- </span>- <span className="flex items-center gap-1">- <span- className="inline-block w-3 h-3 rounded"- style={{ backgroundColor: 'rgba(168, 168, 168, 0.3)' }}- />- vanished- </span>- </div>+ {hasBaseline && (+ <div className="flex gap-3 text-xs text-muted">+ <span className="flex items-center gap-1">+ <span+ className="inline-block w-3 h-3 rounded"+ style={{ backgroundColor: 'rgba(220, 80, 80, 0.7)' }}+ />+ slower+ </span>+ <span className="flex items-center gap-1">+ <span+ className="inline-block w-3 h-3 rounded"+ style={{ backgroundColor: 'rgba(80, 180, 100, 0.7)' }}+ />+ faster+ </span>+ <span className="flex items-center gap-1">+ <span+ className="inline-block w-3 h-3 rounded"+ style={{ backgroundColor: 'rgba(120, 150, 200, 0.45)' }}+ />+ similar+ </span>+ <span className="flex items-center gap-1">+ <span+ className="inline-block w-3 h-3 rounded"+ style={{+ backgroundColor: NEUTRAL_BAR_COLOR,+ outline: '1px dashed rgba(80, 180, 100, 0.85)',+ }}+ />+ new+ </span>+ <span className="flex items-center gap-1">+ <span+ className="inline-block w-3 h-3 rounded"+ style={{ backgroundColor: 'rgba(168, 168, 168, 0.3)' }}+ />+ vanished+ </span>+ </div>+ )}</div>)}interface FocusBreadcrumbProps {- breadcrumb: TreeNode[]+ breadcrumb: FlameTreeNode[]/** Called with the new desired depth (0 = top). */onSelect: (depth: number) => void}diff --git a/products/tracing/frontend/TracingScene.tsx b/products/tracing/frontend/TracingScene.tsxindex 24a75cf79263..1dac737c1a16 100644--- a/products/tracing/frontend/TracingScene.tsx+++ b/products/tracing/frontend/TracingScene.tsx@@ -416,6 +416,7 @@ function TracingSceneContents(): JSX.Element {previous={spanTree.previous}loading={spanTreeLoading}initialSpanName={compareFlameSpanName}+ surface="compare_modal"/></LemonModal></SceneContent>diff --git a/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx b/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsxindex 032938529cff..d501b356df83 100644--- a/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx+++ b/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx@@ -2,14 +2,13 @@ import { useActions, useValues } from 'kea'import { CSSProperties, useCallback, useEffect, useMemo, useRef, useState } from 'react'import { List, useListRef } from 'react-window'-import { LemonButton, Spinner } from '@posthog/lemon-ui'-import { AutoSizer } from 'lib/components/AutoSizer'import { SizeProps } from 'lib/components/AutoSizer/AutoSizer'import { getTimelineTicks } from 'products/tracing/frontend/spanTree'import { formatDuration } from 'products/tracing/frontend/TraceWaterfallView'+import { TraceLoadMoreFooter } from '../TraceView/TraceLoadMoreFooter'import { traceViewLogic, type TraceViewTimeline } from '../TraceView/traceViewLogic'import type { TraceDialogSource } from './traceDialogLogic'import { TraceDialogMoreSpansRow } from './TraceDialogMoreSpansRow'@@ -337,25 +336,7 @@ export function TraceDialogSpanTree({)}/></div>- {hasMore && (- <div className="flex items-center justify-center gap-2 py-2 text-xs text-muted border-t">- {loadingMore ? (- <>- <Spinner />- <span>Loading more spans…</span>- </>- ) : (- <LemonButton- size="xsmall"- type="tertiary"- onClick={() => onLoadMore?.()}- data-attr="trace-view-load-more-spans"- >- Load more spans- </LemonButton>- )}- </div>- )}+ {hasMore && <TraceLoadMoreFooter loadingMore={loadingMore} onLoadMore={onLoadMore} />}</div>)}diff --git a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.tsindex ea3b2798b593..6ffc26856316 100644--- a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts+++ b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts@@ -486,6 +486,7 @@ describe('traceDialogLogic', () => {entry_point: 'log_row_action',has_span: true,has_log: false,+ view: 'waterfall',})expect(captureSpy).toHaveBeenCalledWith('tracing trace dialog closed',diff --git a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.tsindex 50da45584eb6..1fd92bcfd70b 100644--- a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts+++ b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts@@ -31,8 +31,9 @@ import {type TraceIdentity,} from 'products/tracing/frontend/traceIdentity'import { resolveTraceLookupRange } from 'products/tracing/frontend/traceLinks'+import { tracingConfigLogic } from 'products/tracing/frontend/tracingConfigLogic'import { tracingCorrelationConfigLogic } from 'products/tracing/frontend/tracingCorrelationConfigLogic'-import type { Span, SpanInspectorTab } from 'products/tracing/frontend/types'+import type { Span, SpanInspectorTab, TraceViewMode } from 'products/tracing/frontend/types'import { traceViewLogic } from '../TraceView/traceViewLogic'@@ -107,6 +108,7 @@ function captureClosed(session: TraceDialogSession, reason: TraceDialogCloseReas// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface traceDialogLogicValues {featureFlags: FeatureFlagsSet // featureFlagLogic+ traceViewMode: TraceViewMode // tracingConfigLogicconfiguredDistinctIdKeys: string[] | undefined // tracingCorrelationConfigLogicconfiguredSessionIdKeys: string[] | undefined // tracingCorrelationConfigLogiccorrelationLinksEnabled: boolean // tracingCorrelationConfigLogic@@ -319,6 +321,8 @@ export const traceDialogLogic = kea<traceDialogLogicType>([],featureFlagLogic,['featureFlags'],+ tracingConfigLogic,+ ['traceViewMode'],],})),actions({@@ -478,6 +482,9 @@ export const traceDialogLogic = kea<traceDialogLogicType>([entry_point: entryPoint,has_span: !!trace.spanId,has_log: !!openedFrom,+ // The view persists across traces, so the view change event alone cannot count the traces read+ // as a call tree.+ view: values.traceViewMode,})actions.showTrace(trace.spanId, null)actions.loadTraceSpans(trace)diff --git a/products/tracing/frontend/components/TraceDrawer/TraceDrawer.test.tsx b/products/tracing/frontend/components/TraceDrawer/TraceDrawer.test.tsxindex 65e78c00aef6..8501e452b85f 100644--- a/products/tracing/frontend/components/TraceDrawer/TraceDrawer.test.tsx+++ b/products/tracing/frontend/components/TraceDrawer/TraceDrawer.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { cleanup, render, screen, within } from '@testing-library/react'+import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react'import { Provider } from 'kea'import posthog from 'posthog-js'@@ -12,8 +12,10 @@ import { initKeaTests } from '~/test/init'import { tracingSpansTraceAiEventsRetrieve } from 'products/tracing/frontend/generated/api'import { makeSpan } from '../../__mocks__/span'+import { compareRowKey } from '../../compareUtils'import { EMPTY_TRACE_IDENTITY } from '../../traceIdentity'-import type { SpanInspectorTab } from '../../types'+import { tracingConfigLogic } from '../../tracingConfigLogic'+import type { Span, SpanInspectorTab } from '../../types'import { traceViewLogic } from '../TraceView/traceViewLogic'import { TraceDrawer, type TraceDrawerProps } from './TraceDrawer'@@ -29,6 +31,8 @@ describe('TraceDrawer', () => {let captureSpy: jest.SpyInstancebeforeEach(() => {+ // The view mode persists, so a mode that one test leaves would carry over into the next.+ localStorage.clear()initKeaTests()mockAiEventsRetrieve.mockReset()mockAiEventsRetrieve.mockResolvedValue({ results: [], has_more: false, limit: 500 })@@ -201,6 +205,117 @@ describe('TraceDrawer', () => {expect(tabEvents()).toEqual([['tracing trace inspector tab changed', { tab: 'raw', surface: 'drawer' }]])})+ describe('call tree', () => {+ const traceSpan = (span_id: string, parent_span_id: string, service_name: string, name: string): Span =>+ makeSpan({+ trace_id: 'abc123',+ uuid: span_id,+ span_id,+ parent_span_id,+ service_name,+ name,+ is_root_span: parent_span_id === '',+ })+ const setCallTreeFlag = (on: boolean): void => {+ featureFlagLogic.mount()+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_TRACE_CALL_TREE]: on })+ }++ beforeEach(() => {+ // jsdom lays nothing out, so the virtualized span tree needs a size to render rows into.+ jest.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(600)+ jest.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(1000)+ setCallTreeFlag(true)+ })+ afterEach(() => {+ jest.restoreAllMocks()+ })++ // The toggle swaps one view for the other, so a regression that renders both, or neither, fails here. The+ // call tree draws the two cache GET spans under POST /orders as one bar with its count. The third runs under+ // SELECT orders, so it gets a bar of its own: a tree that merges an operation across its callers labels both+ // bars cache GET ×3. A click on the selected option also calls onChange, and must not count as a change.+ it('switches the left pane to the call tree, and captures the change once', () => {+ renderDrawer({+ spans: [+ traceSpan('span-1', '', 'checkout-api', 'POST /orders'),+ traceSpan('span-2', 'span-1', 'postgres', 'SELECT orders'),+ traceSpan('span-3', 'span-1', 'checkout-api', 'cache GET'),+ traceSpan('span-4', 'span-1', 'checkout-api', 'cache GET'),+ traceSpan('span-5', 'span-2', 'checkout-api', 'cache GET'),+ ],+ })+ const spanRow = (): Element | null => document.querySelector('[data-attr="trace-dialog-span-row"]')+ const callTreeOption = (): HTMLElement =>+ document.querySelector<HTMLElement>('[data-attr="tracing-trace-view-calltree"]')!+ expect(spanRow()).not.toBeNull()+ expect(screen.queryByText('cache GET ×2')).toBeNull()++ fireEvent.click(callTreeOption())+ fireEvent.click(callTreeOption())++ expect(spanRow()).toBeNull()+ expect(screen.getByText('cache GET ×2')).toBeInTheDocument()+ expect(screen.getByText('cache GET')).toBeInTheDocument()+ expect(screen.queryByText('cache GET ×3')).toBeNull()+ expect(captureSpy.mock.calls.filter(([event]) => event === 'tracing trace view changed')).toEqual([+ ['tracing trace view changed', { view: 'calltree', surface: 'drawer' }],+ ])+ })++ // With the flag off nothing may offer the call tree, and a call tree that the person picked while the flag was+ // on must not hide the span tree with no toggle to get it back.+ it('with the flag off, shows no toggle and keeps the span tree for a stored call tree', () => {+ setCallTreeFlag(false)+ tracingConfigLogic.mount()+ tracingConfigLogic.actions.setTraceViewMode('calltree', 'drawer')+ renderDrawer({ spans: [traceSpan('span-1', '', 'checkout-api', 'POST /orders')] })++ expect(document.querySelector('[data-attr="tracing-trace-view-calltree"]')).toBeNull()+ expect(document.querySelector('[data-attr="trace-dialog-span-row"]')).not.toBeNull()+ expect(captureSpy.mock.calls.filter(([event]) => event === 'tracing trace view changed')).toEqual([])+ })++ // Two services each run GET /x. A focus that matches on the name alone opens the first one, under which only+ // web-only runs. A switch to another inspector tab clears the highlight, and must not undo the person's drill+ // back to the root, where web-only shows again. The focus event counts only the person's own drill, so the+ // highlight's focus and its clearing send nothing.+ it('focuses the highlighted operation of its own service, keeps a drill when the highlight clears, and captures only clicked focus changes', () => {+ tracingConfigLogic.mount()+ tracingConfigLogic.actions.setTraceViewMode('calltree', 'drawer')+ renderDrawer({+ spans: [+ traceSpan('span-1', '', 'checkout-api', 'POST /orders'),+ traceSpan('span-2', 'span-1', 'web', 'GET /x'),+ traceSpan('span-3', 'span-2', 'web', 'web-only'),+ traceSpan('span-4', 'span-1', 'api', 'GET /x'),+ traceSpan('span-5', 'span-4', 'api', 'api-only'),+ ],+ })++ act(() => {+ traceViewLogic({ id: 'trace-drawer-test' }).actions.highlightOperation(+ compareRowKey({ service_name: 'api', name: 'GET /x' })+ )+ })++ expect(screen.getByText('api-only')).toBeInTheDocument()+ expect(screen.queryByText('web-only')).toBeNull()++ fireEvent.click(screen.getByText('api-only'))+ act(() => {+ traceViewLogic({ id: 'trace-drawer-test' }).actions.setInspectorTab('logs')+ })+ expect(screen.queryByText('web-only')).toBeNull()+ fireEvent.click(screen.getByText('Root'))++ expect(captureSpy.mock.calls.filter(([event]) => event === 'tracing trace call tree focused')).toEqual([+ ['tracing trace call tree focused', { direction: 'in', depth: 3, surface: 'drawer' }],+ ['tracing trace call tree focused', { direction: 'out', depth: 0, surface: 'drawer' }],+ ])+ })+ })+it('shows a retryable error when the AI events fetch fails and there are no spans', async () => {mockAiEventsRetrieve.mockRejectedValueOnce(new Error('boom'))featureFlagLogic.mount()diff --git a/products/tracing/frontend/components/TraceView/TraceLoadMoreFooter.tsx b/products/tracing/frontend/components/TraceView/TraceLoadMoreFooter.tsxnew file mode 100644index 000000000000..12d1f1bac649--- /dev/null+++ b/products/tracing/frontend/components/TraceView/TraceLoadMoreFooter.tsx@@ -0,0 +1,31 @@+import { LemonButton, Spinner } from '@posthog/lemon-ui'++export interface TraceLoadMoreFooterProps {+ /** A next page of spans is being fetched. */+ loadingMore: boolean+ onLoadMore?: () => void+}++/** Render it only while the open trace has more spans than the loaded pages. */+export function TraceLoadMoreFooter({ loadingMore, onLoadMore }: TraceLoadMoreFooterProps): JSX.Element {+ return (+ <div className="flex items-center justify-center gap-2 py-2 text-xs text-muted border-t">+ {loadingMore ? (+ <>+ <Spinner />+ <span>Loading more spans…</span>+ </>+ ) : (+ <LemonButton+ size="xsmall"+ type="tertiary"+ onClick={() => onLoadMore?.()}+ // pinned: autocapture selects on this value, so renaming it breaks saved actions+ data-attr="trace-view-load-more-spans"+ >+ Load more spans+ </LemonButton>+ )}+ </div>+ )+}diff --git a/products/tracing/frontend/components/TraceView/TraceView.tsx b/products/tracing/frontend/components/TraceView/TraceView.tsxindex da9ac70a5a0b..9b50ac2b8d5f 100644--- a/products/tracing/frontend/components/TraceView/TraceView.tsx+++ b/products/tracing/frontend/components/TraceView/TraceView.tsx@@ -1,21 +1,87 @@import { useActions, useValues } from 'kea'-import { type CSSProperties, useCallback, useMemo, useRef } from 'react'+import { type CSSProperties, memo, useCallback, useMemo, useRef } from 'react'++import { LemonSegmentedButton, type LemonSegmentedButtonOption } from '@posthog/lemon-ui'import { Resizer } from 'lib/components/Resizer/Resizer'import { type ResizerLogicProps, resizerLogic } from 'lib/components/Resizer/resizerLogic'+import type { SpanTreeNode } from '~/queries/schema/schema-general'+import { SpanInspector } from 'products/tracing/frontend/components/SpanInspector/SpanInspector'import { MissingParentSpanNotice } from 'products/tracing/frontend/components/TraceDrawer/MissingParentSpanNotice'import type { ErrorScope } from 'products/tracing/frontend/errorCorrelation'-import type { SpanInspectorTab } from 'products/tracing/frontend/types'+import { TraceCompareFlame } from 'products/tracing/frontend/TraceCompareFlame'+import type { SpanInspectorTab, TraceViewMode } from 'products/tracing/frontend/types'import type { TraceDialogSource } from '../TraceDialog/traceDialogLogic'import { TraceDialogSpanTree } from '../TraceDialog/TraceDialogSpanTree'+import { TraceLoadMoreFooter } from './TraceLoadMoreFooter'import { type TraceOperationsScope, traceViewLogic, type TraceViewSurface } from './traceViewLogic'// Below this the attribute tables stop being readable, so a drag can't crush the inspector.const MIN_INSPECTOR_WIDTH = 320+const VIEW_MODE_OPTIONS: LemonSegmentedButtonOption<TraceViewMode>[] = [+ // pinned: autocapture selects on these values, so renaming them breaks saved actions.+ // LemonSegmentedButton puts no data-attr on its root, so each option carries one.+ { value: 'waterfall', label: 'Waterfall', 'data-attr': 'tracing-trace-view-waterfall' },+ { value: 'calltree', label: 'Flame graph', 'data-attr': 'tracing-trace-view-calltree' },+]++// The call tree draws a tree of its own, so the flame gets no rows to build one from.+const NO_FLAME_ROWS: SpanTreeNode[] = []++interface TraceViewCallTreeProps {+ surface: TraceViewSurface+ /** Spans are still loading, so an empty tree is not yet a verdict. */+ resolving: boolean+ hasMoreSpans: boolean+ loadingMoreSpans: boolean+ onLoadMoreSpans?: () => void+}++/**+ * The left pane in call tree mode. Only this pane reads the call tree, so the logic builds it only in this mode.+ * Memoized because the view re-renders on every mousemove of an inspector resize drag, and on changes the pane does+ * not show, such as another inspector tab. The props stay the same then, so the flame, with a tooltip on every bar,+ * does not render again. The pane's own `useValues` renders it when the tree, the colors or the highlight change.+ */+const TraceViewCallTree = memo(function TraceViewCallTree({+ surface,+ resolving,+ hasMoreSpans,+ loadingMoreSpans,+ onLoadMoreSpans,+}: TraceViewCallTreeProps): JSX.Element {+ const { callTree, serviceColorMap, highlightedOperationKey } = useValues(traceViewLogic)+ return (+ <>+ <div className="flex-1 min-h-0 overflow-y-auto">+ {callTree.children.length === 0 ? (+ // The surface shows its own spinner while spans load.+ resolving ? null : (+ <div className="text-muted text-center py-8">No spans to show in the flame graph.</div>+ )+ ) : (+ <TraceCompareFlame+ tree={callTree}+ current={NO_FLAME_ROWS}+ previous={null}+ loading={false}+ sizeBy="total"+ initialFocusKey={highlightedOperationKey}+ serviceColorMap={serviceColorMap}+ surface={surface}+ />+ )}+ </div>+ {/* The call tree has no scroll trigger, so this button is the only way to load the rest of a large trace. */}+ {hasMoreSpans && <TraceLoadMoreFooter loadingMore={loadingMoreSpans} onLoadMore={onLoadMoreSpans} />}+ </>+ )+})+export interface TraceViewProps {traceId: string/** Where the view sits, for analytics. */@@ -37,7 +103,10 @@ export interface TraceViewProps {onLoadMoreSpans?: () => void}-/** The span tree beside the inspector for the selected span. Mount it inside a `BindLogic` for `traceViewLogic`. */+/**+ * The span tree, or the call tree of its operations, beside the inspector for the selected span. Mount it inside a+ * `BindLogic` for `traceViewLogic`.+ */export function TraceView({traceId,surface,@@ -72,8 +141,10 @@ export function TraceView({operationSpans,highlightedOperationKey,operationsTabEnabled,+ traceViewMode,+ callTreeEnabled,} = useValues(traceViewLogic)- const { setInspectorTab, highlightOperation } = useActions(traceViewLogic)+ const { setInspectorTab, highlightOperation, setTraceViewMode } = useActions(traceViewLogic)// A click on the tab already shown still records the person's pick. Nothing on screen changes, so it passes// no surface and sends no event.const onSelectTab = useCallback(@@ -112,15 +183,37 @@ export function TraceView({return (<div className="@container/trace-dialog flex flex-1 min-h-0"><div className="flex flex-1 min-w-0 min-h-0 flex-col @min-[56rem]/trace-dialog:flex-row">- <div className="flex-1 min-w-0 min-h-0">- <TraceDialogSpanTree- openedFrom={openedFrom}- openedAtUs={openedAtUs}- hasMore={hasMoreSpans}- loadingMore={loadingMoreSpans}- onLoadMore={onLoadMoreSpans}- showOperationHighlight={inspectorOpen}- />+ <div className="flex flex-col gap-2 flex-1 min-w-0 min-h-0">+ {callTreeEnabled && (+ <div>+ <LemonSegmentedButton+ size="xsmall"+ value={traceViewMode}+ onChange={(mode) => setTraceViewMode(mode, surface)}+ options={VIEW_MODE_OPTIONS}+ />+ </div>+ )}+ <div className="flex flex-col flex-1 min-h-0">+ {traceViewMode === 'calltree' ? (+ <TraceViewCallTree+ surface={surface}+ resolving={resolving}+ hasMoreSpans={!!hasMoreSpans}+ loadingMoreSpans={!!loadingMoreSpans}+ onLoadMoreSpans={onLoadMoreSpans}+ />+ ) : (+ <TraceDialogSpanTree+ openedFrom={openedFrom}+ openedAtUs={openedAtUs}+ hasMore={hasMoreSpans}+ loadingMore={loadingMoreSpans}+ onLoadMore={onLoadMoreSpans}+ showOperationHighlight={inspectorOpen}+ />+ )}+ </div></div>{inspectorOpen && (missingParentSpanId || selectedSpan) && (<divdiff --git a/products/tracing/frontend/components/TraceView/TraceViewShortcuts.tsx b/products/tracing/frontend/components/TraceView/TraceViewShortcuts.tsxindex 551fe4af280e..41829afd60ac 100644--- a/products/tracing/frontend/components/TraceView/TraceViewShortcuts.tsx+++ b/products/tracing/frontend/components/TraceView/TraceViewShortcuts.tsx@@ -1,5 +1,9 @@+import { useValues } from 'kea'+import { KeyboardShortcut } from 'lib/components/KeyboardShortcut/KeyboardShortcut'+import { tracingConfigLogic } from 'products/tracing/frontend/tracingConfigLogic'+export interface TraceViewShortcutsProps {/** What Escape does here, such as "Close" or "Back to log". */closeLabel: string@@ -7,22 +11,28 @@ export interface TraceViewShortcutsProps {}export function TraceViewShortcuts({ closeLabel, children }: TraceViewShortcutsProps): JSX.Element {+ // The span keys are off while the call tree hides the span tree, so their hints go with them.+ const { traceViewMode } = useValues(tracingConfigLogic)return (<div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted"><span className="flex items-center gap-1"><KeyboardShortcut escape /><span>{closeLabel}</span></span>- <span className="flex items-center gap-1">- <KeyboardShortcut j />- <KeyboardShortcut k />- <span>Next or previous span</span>- </span>- <span className="flex items-center gap-1">- <KeyboardShortcut arrowleft />- <KeyboardShortcut arrowright />- <span>Collapse or expand</span>- </span>+ {traceViewMode !== 'calltree' && (+ <>+ <span className="flex items-center gap-1">+ <KeyboardShortcut j />+ <KeyboardShortcut k />+ <span>Next or previous span</span>+ </span>+ <span className="flex items-center gap-1">+ <KeyboardShortcut arrowleft />+ <KeyboardShortcut arrowright />+ <span>Collapse or expand</span>+ </span>+ </>+ )}{children}</div>)diff --git a/products/tracing/frontend/components/TraceView/traceViewLogic.test.ts b/products/tracing/frontend/components/TraceView/traceViewLogic.test.tsindex a89f1dafccd9..cba7ab92e326 100644--- a/products/tracing/frontend/components/TraceView/traceViewLogic.test.ts+++ b/products/tracing/frontend/components/TraceView/traceViewLogic.test.ts@@ -1,5 +1,8 @@import posthog from 'posthog-js'+import { FEATURE_FLAGS } from 'lib/constants'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'+import { initKeaTests } from '~/test/init'import { makeSpan } from '../../__mocks__/span'@@ -41,6 +44,8 @@ describe('traceViewLogic', () => {// A stale key dims the next trace's tree. Another inspector tab, or the missing-parent notice that// replaces the inspector, would leave dimmed rows with nothing on screen to explain them. A switch to// the Operations tab itself must keep the pick, or a caller that selects the tab after a highlight drops it.+ // The Operations tab stays on screen in the call tree, which opens on the same operation, so a switch of+ // view must keep the pick too.// The tab event must count only the person's own switches: a trace that opens on a tab, or a click on the// tab already shown, would inflate it.it.each([@@ -69,6 +74,15 @@ describe('traceViewLogic', () => {keeps: true,tabEvent: null,},+ {+ action: 'switching to the call tree',+ act: () => {+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_TRACE_CALL_TREE]: true })+ logic.actions.setTraceViewMode('calltree', 'drawer')+ },+ keeps: true,+ tabEvent: null,+ },])('after $action, keeps the highlighted operation: $keeps, and sends a tab event only for a tab change',({ act, keeps, tabEvent }) => {@@ -84,6 +98,46 @@ describe('traceViewLogic', () => {})+ // The call tree hides the span tree, so J there would move a selection that nobody can see. Back in the span+ // tree the keys must work again, and with the flag off a stored call tree must not turn them off. In every+ // view the key must stop at the view, or the page under it acts on the key too.+ it.each([+ { view: 'calltree' as const, flagOn: true, selected: 'a' },+ { view: 'waterfall' as const, flagOn: true, selected: 'b' },+ { view: 'calltree' as const, flagOn: false, selected: 'b' },+ ])(+ 'in the $view view with the flag on: $flagOn, J leaves the selection on $selected',+ ({ view, flagOn, selected }) => {+ featureFlagLogic.mount()+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_TRACE_CALL_TREE]: flagOn })+ logic.actions.setSpans([root, child('a', 'root', 'SELECT orders', 1), child('b', 'root', 'cache GET', 2)])+ logic.actions.selectSpan('a')+ logic.actions.enableSpanKeys('[data-attr="trace-view-test"]')+ logic.actions.setTraceViewMode('calltree', 'drawer')+ logic.actions.setTraceViewMode(view, 'drawer')+ const pageKeyDown = jest.fn()+ document.body.addEventListener('keydown', pageKeyDown)++ const event = new KeyboardEvent('keydown', { key: 'j', bubbles: true, cancelable: true })+ document.body.dispatchEvent(event)+ document.body.removeEventListener('keydown', pageKeyDown)++ expect(logic.values.selectedSpanId).toBe(selected)+ expect([event.defaultPrevented, pageKeyDown.mock.calls.length]).toEqual([true, 0])+ }+ )++ // AI events load after the spans. A call tree rebuilt for them resets the drill-down in the flame.+ it('keeps the call tree when only AI event rows arrive', () => {+ const spans = [root, child('cache', 'root', 'cache GET', 1)]+ logic.actions.setSpans(spans)+ const callTree = logic.values.callTree++ logic.actions.setSpans([...spans, child('ai:event-1', 'root', 'llm call', 2)])++ expect(logic.values.callTree).toBe(callTree)+ })+// Fails when the highlight stops opening the child list that hides the operation's first span, which// leaves the tree nothing to scroll to, or when it moves the selection off the span the person inspects.it('opens the child list that hides the first span of the highlighted operation, and keeps the selection', () => {diff --git a/products/tracing/frontend/components/TraceView/traceViewLogic.ts b/products/tracing/frontend/components/TraceView/traceViewLogic.tsindex fa69ffb026c7..fc6f78ea1734 100644--- a/products/tracing/frontend/components/TraceView/traceViewLogic.ts+++ b/products/tracing/frontend/components/TraceView/traceViewLogic.ts@@ -17,10 +17,13 @@ import { FEATURE_FLAGS } from 'lib/constants'import { type FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'import { isAiEventSpan } from 'products/tracing/frontend/aiEventSpans'+import type { FlameTreeNode } from 'products/tracing/frontend/flameTree'import { buildSpanTree, parseTimestampUs, type SpanNode } from 'products/tracing/frontend/spanTree'+import { buildTraceCallTree } from 'products/tracing/frontend/traceCallTree'import { buildServiceColorMap } from 'products/tracing/frontend/TraceWaterfallView'+import { tracingConfigLogic } from 'products/tracing/frontend/tracingConfigLogic'import { tracingCorrelationConfigLogic } from 'products/tracing/frontend/tracingCorrelationConfigLogic'-import type { Span, SpanInspectorTab } from 'products/tracing/frontend/types'+import type { Span, SpanInspectorTab, TraceViewMode } from 'products/tracing/frontend/types'import {buildTraceDialogRows,@@ -38,6 +41,11 @@ import {// only the last one acts on a key.const spanKeyOwners: symbol[] = []+// Shallow, because the spans of a trace keep their identity when the list is rebuilt around them.+function sameSpans(a: Span[], b: Span[]): boolean {+ return a.length === b.length && a.every((span, index) => span === b[index])+}+export interface TraceViewLogicProps {id: string}@@ -72,8 +80,11 @@ export interface TraceViewSummary {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface traceViewLogicValues {featureFlags: FeatureFlagsSet // featureFlagLogic+ callTreeEnabled: boolean // tracingConfigLogic+ traceViewMode: TraceViewMode // tracingConfigLogicsessionErrorBadgesEnabled: boolean // tracingCorrelationConfigLogicactiveInspectorTab: SpanInspectorTab+ callTree: FlameTreeNodechainStartIds: {folded: Set<string>opened: Set<string>@@ -104,6 +115,13 @@ export interface traceViewLogicValues {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface traceViewLogicActions {+ setTraceViewMode: (+ traceViewMode: 'calltree' | 'waterfall',+ surface: TraceViewSurface+ ) => {+ surface: TraceViewSurface+ traceViewMode: 'calltree' | 'waterfall'+ } // tracingConfigLogiccollapseSelectedSpan: () => {value: true}@@ -210,6 +228,7 @@ export interface traceViewLogicMeta {) => boolean[] | nullfirstOperationRowIndex: (operationRowMatches: boolean[] | null) => numberoperationSpans: (spans: Span[]) => Span[]+ callTree: (operationSpans: Span[]) => FlameTreeNodeserviceColorMap: (spans: Span[]) => Map<string, number>timeline: (spans: Span[]) => TraceViewTimelinesummary: (spans: Span[]) => TraceViewSummary@@ -228,7 +247,15 @@ export const traceViewLogic: LogicWrapper<traceViewLogicType> = kea<traceViewLogkey((props) => props.id),path((key) => ['products', 'tracing', 'frontend', 'components', 'TraceView', 'traceViewLogic', key]),connect(() => ({- values: [tracingCorrelationConfigLogic, ['sessionErrorBadgesEnabled'], featureFlagLogic, ['featureFlags']],+ values: [+ tracingCorrelationConfigLogic,+ ['sessionErrorBadgesEnabled'],+ featureFlagLogic,+ ['featureFlags'],+ tracingConfigLogic,+ ['traceViewMode', 'callTreeEnabled'],+ ],+ actions: [tracingConfigLogic, ['setTraceViewMode']],})),actions({showTrace: (pinnedSpanId: string | null, tab: SpanInspectorTab | null) => ({ pinnedSpanId, tab }),@@ -451,8 +478,19 @@ export const traceViewLogic: LogicWrapper<traceViewLogicType> = kea<traceViewLog(s) => [s.operationRowMatches],(matches: boolean[] | null): number => matches?.indexOf(true) ?? -1,],- // AI event rows are synthetic and have no operation of their own.- operationSpans: [(s) => [s.spans], (spans: Span[]): Span[] => spans.filter((span) => !isAiEventSpan(span))],+ // AI event rows are synthetic and have no operation of their own. AI events load after the spans, and a new+ // list of the same spans must keep this one, or the call tree rebuilds and drops the person's drill-down.+ operationSpans: [+ (s) => [s.spans],+ (spans: Span[]): Span[] => spans.filter((span) => !isAiEventSpan(span)),+ { resultEqualityCheck: sameSpans },+ ],+ // Kea computes a selector only when something reads it. Only the call tree pane reads this one, so the+ // waterfall never builds it, and a new selection or a resize does not rebuild it.+ callTree: [+ (s) => [s.operationSpans],+ (operationSpans: Span[]): FlameTreeNode => buildTraceCallTree(operationSpans),+ ],serviceColorMap: [(s) => [s.spans], (spans: Span[]): Map<string, number> => buildServiceColorMap(spans)],timeline: [(s) => [s.spans],@@ -508,6 +546,10 @@ export const traceViewLogic: LogicWrapper<traceViewLogicType> = kea<traceViewLogif (step) {event.preventDefault()event.stopPropagation()+ // The call tree hides the span tree, so a step would move a selection that nobody can see.+ if (values.traceViewMode === 'calltree') {+ return+ }step()}}diff --git a/products/tracing/frontend/flameTree.test.ts b/products/tracing/frontend/flameTree.test.tsnew file mode 100644index 000000000000..a34cf66700ce--- /dev/null+++ b/products/tracing/frontend/flameTree.test.ts@@ -0,0 +1,40 @@+import { countFlameNodes, findFlamePath, type FlameTreeNode } from './flameTree'++const flameNode = (name: string, children: FlameTreeNode[] = []): FlameTreeNode => ({+ serviceName: 'web',+ name,+ node: null,+ previousNode: null,+ children,+})++describe('flameTree', () => {+ // The compare modal focuses the first match in pre-order, earlier siblings first. A walk that takes the last+ // sibling first, or the shallowest match first, focuses the second `cache GET` here.+ it('finds the first match in depth-first order, earlier siblings first', () => {+ const root = flameNode('<ROOT>', [flameNode('GET /x', [flameNode('cache GET')]), flameNode('cache GET')])++ const path = findFlamePath(root, (node) => node.name === 'cache GET')++ expect(path?.map((node) => node.name)).toEqual(['GET /x', 'cache GET'])+ })++ // A recursive walk overflows the call stack on a chain this deep, so a highlight that focuses the leaf throws, and+ // so does a "+N more" bundle that counts the chain.+ it('finds the leaf of a chain 20,000 levels deep, and counts the chain', () => {+ const root = flameNode('<ROOT>')+ let parent = root+ for (let depth = 1; depth <= 20_000; depth++) {+ const child = flameNode(`level ${depth}`)+ parent.children.push(child)+ parent = child+ }++ const path = findFlamePath(root, (node) => node.name === 'level 20000')++ expect(path?.length).toBe(20_000)+ expect(path?.[0].name).toBe('level 1')+ expect(path?.[path.length - 1]).toBe(parent)+ expect(countFlameNodes(root)).toBe(20_001)+ })+})diff --git a/products/tracing/frontend/flameTree.ts b/products/tracing/frontend/flameTree.tsnew file mode 100644index 000000000000..cc206c2b457f--- /dev/null+++ b/products/tracing/frontend/flameTree.ts@@ -0,0 +1,56 @@+import type { SpanTreeNode } from '~/queries/schema/schema-general'++/** One bar of TraceCompareFlame: the calls of one operation, with the operations they call as children. */+export interface FlameTreeNode {+ serviceName: string+ name: string+ node: SpanTreeNode | null+ previousNode: SpanTreeNode | null+ children: FlameTreeNode[]+}++interface PathEntry {+ node: FlameTreeNode+ parent: PathEntry | null+}++/**+ * The nodes from a child of `root` down to the first node that `matches`, in depth-first order with the earlier+ * sibling first. Null when no node matches. Iterative, so that a tree thousands of levels deep cannot overflow the+ * call stack. Each stack entry links to the entry of its parent, so only the match pays for its path.+ */+export function findFlamePath(root: FlameTreeNode, matches: (node: FlameTreeNode) => boolean): FlameTreeNode[] | null {+ const stack: PathEntry[] = []+ const pushChildren = (node: FlameTreeNode, parent: PathEntry | null): void => {+ // Reversed, so that the first child comes off the stack first.+ for (let i = node.children.length - 1; i >= 0; i--) {+ stack.push({ node: node.children[i], parent })+ }+ }+ pushChildren(root, null)+ while (stack.length > 0) {+ const entry = stack.pop()!+ if (matches(entry.node)) {+ const path: FlameTreeNode[] = []+ for (let step: PathEntry | null = entry; step; step = step.parent) {+ path.push(step.node)+ }+ return path.reverse()+ }+ pushChildren(entry.node, entry)+ }+ return null+}++/** The number of nodes in the subtree of `node`, itself included. Iterative for the same reason as `findFlamePath`. */+export function countFlameNodes(node: FlameTreeNode): number {+ let count = 0+ const stack = [node]+ while (stack.length > 0) {+ count += 1+ for (const child of stack.pop()!.children) {+ stack.push(child)+ }+ }+ return count+}diff --git a/products/tracing/frontend/traceCallTree.test.ts b/products/tracing/frontend/traceCallTree.test.tsnew file mode 100644index 000000000000..0d4c259d14d3--- /dev/null+++ b/products/tracing/frontend/traceCallTree.test.ts@@ -0,0 +1,100 @@+import { makeSpan } from './__mocks__/span'+import { buildTraceCallTree } from './traceCallTree'+import type { Span } from './types'++const TRACE_START_MS = Date.parse('2026-09-03T10:00:00.000Z')++function treeSpan(+ id: string,+ parentId: string,+ service_name: string,+ name: string,+ offsetMs: number,+ durationMs: number,+ overrides: Partial<Span> = {}+): Span {+ return makeSpan({+ uuid: id,+ span_id: id,+ parent_span_id: parentId,+ is_root_span: parentId === '',+ service_name,+ name,+ timestamp: new Date(TRACE_START_MS + offsetMs).toISOString(),+ duration_nano: durationMs * 1_000_000,+ ...overrides,+ })+}++describe('buildTraceCallTree', () => {+ it('merges repeated siblings under one parent and keeps the same operation apart under different parents', () => {+ // A tree that merges an operation across its callers gives both "cache GET" nodes a count of 4.+ // The input order differs from the start order, so the test fails if the children sort is dropped.+ const tree = buildTraceCallTree([+ treeSpan('cache-3', 'root', 'web', 'cache GET', 40, 60),+ treeSpan('nested-cache', 'db', 'web', 'cache GET', 3, 5),+ treeSpan('db', 'root', 'db', 'SELECT', 2, 50, { status_code: 2 }),+ treeSpan('root', '', 'web', 'GET /x', 0, 100),+ treeSpan('cache-1', 'root', 'web', 'cache GET', 1, 10),+ treeSpan('cache-2', 'root', 'web', 'cache GET', 15, 20),+ ])++ expect(tree.children.map((child) => child.name)).toEqual(['GET /x'])+ const [getX] = tree.children+ expect(getX.node).toMatchObject({ parent_name: '<ROOT>', count: 1, calls_per_parent_invocation: null })+ expect(getX.children.map((child) => [child.serviceName, child.name])).toEqual([+ ['db', 'SELECT'],+ ['web', 'cache GET'],+ ])+ const [select, cacheGet] = getX.children+ expect(cacheGet.node).toMatchObject({+ parent_service: 'web',+ parent_name: 'GET /x',+ count: 3,+ total_duration_nano: 90_000_000,+ p50_duration_nano: 20_000_000,+ p95_duration_nano: 60_000_000,+ error_count: 0,+ calls_per_parent_invocation: 3,+ })+ expect(select.node).toMatchObject({ error_count: 1 })+ expect(select.children.map((child) => child.node)).toEqual([+ expect.objectContaining({+ parent_name: 'SELECT',+ name: 'cache GET',+ count: 1,+ total_duration_nano: 5_000_000,+ }),+ ])+ })++ // A service that does not export its spans leaves its callees with a parent id that is not in the trace, and a+ // malformed span can name itself as its parent. Both must still show, and the second must not loop the walk.+ it.each([+ { parent: 'a parent that is not in the trace', parentId: 'missing' },+ { parent: 'itself as its parent', parentId: 'orphan' },+ ])('treats a span with $parent as a root', ({ parentId }) => {+ const tree = buildTraceCallTree([treeSpan('orphan', parentId, 'web', 'cache GET', 0, 10)])++ expect(tree.children.map((child) => child.node)).toEqual([+ expect.objectContaining({ parent_name: '<ROOT>', name: 'cache GET', count: 1 }),+ ])+ })++ it('counts calls per parent invocation across several parent spans', () => {+ // Dividing by the one merged "GET /x" node instead of its two spans gives 3, not 1.5.+ const tree = buildTraceCallTree([+ treeSpan('root-1', '', 'web', 'GET /x', 0, 100),+ treeSpan('root-2', '', 'web', 'GET /x', 200, 100),+ treeSpan('cache-1', 'root-1', 'web', 'cache GET', 1, 10),+ treeSpan('cache-2', 'root-1', 'web', 'cache GET', 20, 10),+ treeSpan('cache-3', 'root-2', 'web', 'cache GET', 201, 10),+ ])++ const [getX] = tree.children+ expect(getX.node?.count).toBe(2)+ expect(getX.children.map((child) => child.node)).toEqual([+ expect.objectContaining({ count: 3, calls_per_parent_invocation: 1.5 }),+ ])+ })+})diff --git a/products/tracing/frontend/traceCallTree.ts b/products/tracing/frontend/traceCallTree.tsnew file mode 100644index 000000000000..a94a1fac1f2e--- /dev/null+++ b/products/tracing/frontend/traceCallTree.ts@@ -0,0 +1,132 @@+import type { SpanTreeNode } from '~/queries/schema/schema-general'++import { compareRowKey } from './compareUtils'+import type { FlameTreeNode } from './flameTree'+import { parseTimestampUs } from './spanTree'+import type { Span } from './types'++const ROOT_NAME = '<ROOT>'++function median(sortedValues: number[]): number {+ const mid = Math.floor(sortedValues.length / 2)+ return sortedValues.length % 2 === 1 ? sortedValues[mid] : (sortedValues[mid - 1] + sortedValues[mid]) / 2+}++function nearestRank(sortedValues: number[], quantile: number): number {+ return sortedValues[Math.min(sortedValues.length - 1, Math.ceil(quantile * sortedValues.length) - 1)]+}++// A root span has the all-zero OTel parent id, which the API returns as hex ("0000000000000000"), not "".+// A span that names itself as its parent is a root too, because as its own child no path from a root reaches it.+function parentSpanOf(span: Span, spansById: Map<string, Span>): Span | null {+ if (+ span.is_root_span ||+ !span.parent_span_id ||+ /^0+$/.test(span.parent_span_id) ||+ span.parent_span_id === span.span_id+ ) {+ return null+ }+ return spansById.get(span.parent_span_id) ?? null+}++/** The stats of the spans of one operation that `caller` ran. */+function summarizeCalls(spans: Span[], caller: FlameTreeNode, spansById: Map<string, Span>): SpanTreeNode {+ const sortedDurations = spans.map((span) => span.duration_nano).sort((a, b) => a - b)+ let total = 0+ let errorCount = 0+ let startOffsetTotalNano = 0+ for (const span of spans) {+ total += span.duration_nano+ if (span.status_code === 2) {+ errorCount += 1+ }+ const parent = parentSpanOf(span, spansById)+ if (parent) {+ startOffsetTotalNano += (parseTimestampUs(span.timestamp) - parseTimestampUs(parent.timestamp)) * 1_000+ }+ }+ const count = spans.length+ return {+ parent_service: caller.serviceName,+ parent_name: caller.name,+ service_name: spans[0].service_name,+ name: spans[0].name,+ count,+ total_duration_nano: total,+ avg_duration_nano: total / count,+ // The median, not the nearest rank, so that the flame and the Operations tab show the same p50.+ p50_duration_nano: median(sortedDurations),+ p95_duration_nano: nearestRank(sortedDurations, 0.95),+ p99_duration_nano: nearestRank(sortedDurations, 0.99),+ p999_duration_nano: nearestRank(sortedDurations, 0.999),+ error_count: errorCount,+ avg_start_offset_nano: startOffsetTotalNano / count,+ // A caller node counts its spans, so this divides by the caller spans, not by the one merged caller node.+ calls_per_parent_invocation: caller.node ? count / caller.node.count : null,+ }+}++/**+ * One trace's spans as the tree TraceCompareFlame draws. Like a profiler flame graph, spans merge by their path of+ * operations from the root: an operation that two callers run gets one node under each caller, and each node counts+ * only the calls of that caller.+ */+export function buildTraceCallTree(spans: Span[]): FlameTreeNode {+ const spansById = new Map<string, Span>()+ for (const span of spans) {+ spansById.set(span.span_id, span)+ }++ // Iterate the span_id index, not the input, so that a duplicate span row counts once, like in buildSpanTree.+ const rootSpans: Span[] = []+ const childSpansByParentId = new Map<string, Span[]>()+ for (const span of spansById.values()) {+ const parent = parentSpanOf(span, spansById)+ if (!parent) {+ rootSpans.push(span)+ continue+ }+ const siblings = childSpansByParentId.get(parent.span_id)+ if (siblings) {+ siblings.push(span)+ } else {+ childSpansByParentId.set(parent.span_id, [span])+ }+ }++ const root: FlameTreeNode = { serviceName: '', name: ROOT_NAME, node: null, previousNode: null, children: [] }+ // Iterative so that a deep parent/child chain cannot overflow the call stack. The walk needs no visited set:+ // each span is in one child list only, and no path from a root reaches a span in a parent cycle.+ const pending: { node: FlameTreeNode; childSpans: Span[] }[] = [{ node: root, childSpans: rootSpans }]+ while (pending.length > 0) {+ const { node, childSpans } = pending.pop()!+ const spansByOperation = new Map<string, Span[]>()+ for (const span of childSpans) {+ const key = compareRowKey(span)+ const operationSpans = spansByOperation.get(key)+ if (operationSpans) {+ operationSpans.push(span)+ } else {+ spansByOperation.set(key, [span])+ }+ }+ for (const operationSpans of spansByOperation.values()) {+ const child: FlameTreeNode = {+ serviceName: operationSpans[0].service_name,+ name: operationSpans[0].name,+ node: summarizeCalls(operationSpans, node, spansById),+ previousNode: null,+ children: [],+ }+ node.children.push(child)+ pending.push({+ node: child,+ childSpans: operationSpans.flatMap((span) => childSpansByParentId.get(span.span_id) ?? []),+ })+ }+ // Earlier operations go to the left, like in the compare flame.+ node.children.sort((a, b) => (a.node?.avg_start_offset_nano ?? 0) - (b.node?.avg_start_offset_nano ?? 0))+ }+ return root+}diff --git a/products/tracing/frontend/tracingConfigLogic.ts b/products/tracing/frontend/tracingConfigLogic.tsindex 7eaf4a3f31d3..5be57ba3cbf5 100644--- a/products/tracing/frontend/tracingConfigLogic.ts+++ b/products/tracing/frontend/tracingConfigLogic.ts@@ -1,20 +1,30 @@-import { MakeLogicType, actions, kea, path, reducers, selectors } from 'kea'+import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'+import posthog from 'posthog-js'+import { FEATURE_FLAGS } from 'lib/constants'+import { type FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'++import type { TraceViewSurface } from './components/TraceView/traceViewLogic'import {DEFAULT_SPAN_COLUMNS,normalizeSpanColumns,SpanColumnConfig,} from './components/VirtualizedSpanList/spanColumns'+import { TRACE_VIEW_MODES, type TraceViewMode } from './types'/** Column widths a person has dragged, keyed by table then column. */export type TracingColumnWidths = Record<string, Record<string, number>>// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface tracingConfigLogicValues {+ featureFlags: FeatureFlagsSet // featureFlagLogic+ callTreeEnabled: booleancolumnWidths: TracingColumnWidthsfacetRailCollapsed: booleanspanColumns: SpanColumnConfig[]storedSpanColumns: SpanColumnConfig[]+ storedTraceViewMode: string+ traceViewMode: TraceViewMode}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.@@ -41,12 +51,21 @@ export interface tracingConfigLogicActions {setSpanColumns: (columns: SpanColumnConfig[]) => {columns: SpanColumnConfig[]}+ setTraceViewMode: (+ traceViewMode: TraceViewMode,+ surface: TraceViewSurface+ ) => {+ surface: TraceViewSurface+ traceViewMode: 'calltree' | 'waterfall'+ }}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface tracingConfigLogicMeta {__keaTypeGenInternalSelectorTypes: {spanColumns: (storedSpanColumns: SpanColumnConfig[]) => SpanColumnConfig[]+ callTreeEnabled: (featureFlags: FeatureFlagsSet) => boolean+ traceViewMode: (storedTraceViewMode: string, callTreeEnabled: boolean) => TraceViewMode}}@@ -57,15 +76,23 @@ export type tracingConfigLogicType = MakeLogicType<tracingConfigLogicMeta>-/** Display-only preferences for the tracing scene — never affects the query. */+/**+ * Display-only preferences for tracing, never part of a query. The trace views read them too, also in the trace dialog+ * outside the tracing scene, so a preference holds for every trace a person opens.+ */export const tracingConfigLogic = kea<tracingConfigLogicType>([path(['products', 'tracing', 'frontend', 'tracingConfigLogic']),+ connect(() => ({+ values: [featureFlagLogic, ['featureFlags']],+ })),+actions({setFacetRailCollapsed: (facetRailCollapsed: boolean) => ({ facetRailCollapsed }),setColumnWidth: (tableKey: string, columnKey: string, width: number) => ({ tableKey, columnKey, width }),resetColumnWidth: (tableKey: string, columnKey: string) => ({ tableKey, columnKey }),setSpanColumns: (columns: SpanColumnConfig[]) => ({ columns }),+ setTraceViewMode: (traceViewMode: TraceViewMode, surface: TraceViewSurface) => ({ traceViewMode, surface }),}),reducers({@@ -97,6 +124,13 @@ export const tracingConfigLogic = kea<tracingConfigLogicType>([},},],+ storedTraceViewMode: [+ 'waterfall' as string,+ { persist: true },+ {+ setTraceViewMode: (_, { traceViewMode }) => traceViewMode,+ },+ ],}),selectors({@@ -107,5 +141,31 @@ export const tracingConfigLogic = kea<tracingConfigLogicType>([// from a bare function reference and drops the selector from the values interface.(storedSpanColumns: SpanColumnConfig[]): SpanColumnConfig[] => normalizeSpanColumns(storedSpanColumns),],+ callTreeEnabled: [+ (s) => [s.featureFlags],+ (featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.TRACING_TRACE_CALL_TREE],+ ],+ // The mode on screen, which the view, the span keys and the events all read. Checked on the way out for the+ // same reason, so an unknown value never reaches them. With the flag off it is the waterfall, also for a+ // person who picked the call tree while the flag was on.+ traceViewMode: [+ (s) => [s.storedTraceViewMode, s.callTreeEnabled],+ (storedTraceViewMode: string, callTreeEnabled: boolean): TraceViewMode =>+ callTreeEnabled+ ? (TRACE_VIEW_MODES.find((mode) => mode === storedTraceViewMode) ?? 'waterfall')+ : 'waterfall',+ ],}),++ listeners(({ values, selectors }) => ({+ setTraceViewMode: ({ surface }, _, __, previousState) => {+ // A click on the option that is already selected calls onChange too, and it changes nothing. With the+ // flag off the mode on screen stays the waterfall, so nothing changes either.+ if (values.traceViewMode === selectors.traceViewMode(previousState)) {+ return+ }+ // pinned: analytics event and property names, so renaming them breaks the insights built on them+ posthog.capture('tracing trace view changed', { view: values.traceViewMode, surface })+ },+ })),])diff --git a/products/tracing/frontend/tracingViewerLogic.test.ts b/products/tracing/frontend/tracingViewerLogic.test.tsindex 9eb76a38e422..f16b8cda0c9c 100644--- a/products/tracing/frontend/tracingViewerLogic.test.ts+++ b/products/tracing/frontend/tracingViewerLogic.test.ts@@ -1,4 +1,5 @@import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'import api from 'lib/api'import { FEATURE_FLAGS } from 'lib/constants'@@ -326,6 +327,22 @@ describe('tracingViewerLogic', () => {overlay.unmount()})+ // The open is the entry of the compare modal's funnel, so each open sends exactly one event.+ it('sends one compare flame event per open', () => {+ const treeSpy = jest.spyOn(api.tracing, 'tree').mockResolvedValue({ results: [], compare: null })+ const captureSpy = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ try {+ logic.actions.openCompareFlame('SELECT orders', 'postgres')++ expect(captureSpy.mock.calls.filter(([event]) => event === 'tracing compare flame opened')).toEqual([+ ['tracing compare flame opened'],+ ])+ } finally {+ captureSpy.mockRestore()+ treeSpy.mockRestore()+ }+ })+describe('closeTrace', () => {// The attribute buttons in the drawer queue their query for when the drawer closes.// Without this, closing the drawer would silently drop the filter the user just added.diff --git a/products/tracing/frontend/tracingViewerLogic.ts b/products/tracing/frontend/tracingViewerLogic.tsindex 1cec1cc909d4..15ea4d6359e0 100644--- a/products/tracing/frontend/tracingViewerLogic.ts+++ b/products/tracing/frontend/tracingViewerLogic.ts@@ -15,11 +15,12 @@ import {resolveTraceSessionId,type TraceIdentity,} from './traceIdentity'+import { tracingConfigLogic } from './tracingConfigLogic'import { tracingCorrelationConfigLogic } from './tracingCorrelationConfigLogic'import { PREFETCH_SPANS, tracingDataLogic } from './tracingDataLogic'import { TRACING_SCENE_VIEWER_ID, tracingFiltersLogic } from './tracingFiltersLogic'import type { OverlayWindow } from './tracingFiltersLogic'-import type { Span, SpanInspectorTab } from './types'+import type { Span, SpanInspectorTab, TraceViewMode } from './types'export interface TracingViewerLogicProps {id: string@@ -43,6 +44,7 @@ function isCompletePrefetch(spans: Span[], traceId: string): boolean {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface tracingViewerLogicValues {+ traceViewMode: TraceViewMode // tracingConfigLogicconfiguredDistinctIdKeys: string[] | undefined // tracingCorrelationConfigLogicconfiguredSessionIdKeys: string[] | undefined // tracingCorrelationConfigLogiccorrelationLinksEnabled: boolean // tracingCorrelationConfigLogic@@ -280,6 +282,8 @@ export const tracingViewerLogic = kea<tracingViewerLogicType>(['correlationLinksEnabled','sessionErrorBadgesEnabled',],+ tracingConfigLogic,+ ['traceViewMode'],],actions: [tracingDataLogic({ id }),@@ -534,7 +538,9 @@ export const tracingViewerLogic = kea<tracingViewerLogicType>([openTrace: ({ traceId, ts, spanId }) => {actions.showTrace(spanId, values.inspectorTab)actions.enableSpanKeys(`[data-attr="${TRACE_DRAWER_DATA_ATTR}"]`)- posthog.capture('tracing trace opened')+ // pinned: analytics event and property names. The view persists across traces, so the view change event+ // alone cannot count how many traces people read as a call tree.+ posthog.capture('tracing trace opened', { view: values.traceViewMode })// Fetch the trace by id when the prefetch can be incomplete. A batch with no root span is a// cold link (no spans loaded) or an orphan trace, whose prefetch holds only the spans that// matched the list. A full prefetch batch (>= PREFETCH_SPANS) may be truncated.@@ -574,6 +580,9 @@ export const tracingViewerLogic = kea<tracingViewerLogicType>([actions.setInspectorTab(tab)},openCompareFlame: ({ spanName, serviceName }) => {+ // The span and service names are the customer's own telemetry, so the event leaves them out.+ // pinned: analytics event name, so renaming it breaks the insights built on it+ posthog.capture('tracing compare flame opened')actions.fetchSpanTree({ spanName, serviceName })},updateComparisonWindows: () => {diff --git a/products/tracing/frontend/types.ts b/products/tracing/frontend/types.tsindex 565719534638..fde94d3dfbb1 100644--- a/products/tracing/frontend/types.ts+++ b/products/tracing/frontend/types.ts@@ -44,3 +44,10 @@ export const STATUS_CODE_LABELS: Record<number, { label: string; type: 'success'// active tab can point at a tab the drawer is not currently rendering. Pinned: analytics events send// these values as the `tab` property, so renaming one breaks the insights built on it.export type SpanInspectorTab = 'attributes' | 'query' | 'logs' | 'errors' | 'operations' | 'metrics' | 'raw'++/**+ * The views the left pane of a trace view can show. Pinned: the values are persisted, and analytics events send them as+ * the `view` property.+ */+export const TRACE_VIEW_MODES = ['waterfall', 'calltree'] as const+export type TraceViewMode = (typeof TRACE_VIEW_MODES)[number]