: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): prune flame graph to selected span's lineage (#58574)
Frank Hamanddf36a9161f01
diff --git a/products/tracing/frontend/TraceCompareFlame.test.ts b/products/tracing/frontend/TraceCompareFlame.test.tsnew file mode 100644index 000000000000..6f9a35b174cf--- /dev/null+++ b/products/tracing/frontend/TraceCompareFlame.test.ts@@ -0,0 +1,100 @@+import { TreeNode, pruneToLineage } from './TraceCompareFlame'++function node(serviceName: string, name: string, children: TreeNode[] = []): TreeNode {+ return { serviceName, name, node: null, previousNode: null, children }+}++function names(n: TreeNode): { name: string; children: ReturnType<typeof names>[] } {+ return { name: n.name, children: n.children.map(names) }+}++describe('pruneToLineage', () => {+ const targetDirectChild = node('', '<ROOT>', [+ node('svc', 'target', [node('svc', 'leaf-a'), node('svc', 'leaf-b')]),+ node('svc', 'sibling', [node('svc', 'sibling-leaf')]),+ ])++ const targetDeep = node('', '<ROOT>', [+ node('svc', 'top', [+ node('svc', 'mid', [+ node('svc', 'target', [node('svc', 'descendant-a'), node('svc', 'descendant-b')]),+ node('svc', 'mid-sibling'),+ ]),+ node('svc', 'top-cousin'),+ ]),+ node('svc', 'other-top'),+ ])++ // Mirrors a cycle that buildTree already broke (svc/loop appears once at depth 1 and is+ // not re-added under itself). pruneToLineage shouldn't care — it walks the already-acyclic+ // tree just like any other.+ const cycleBroken = node('', '<ROOT>', [node('svc', 'loop', [node('svc', 'target', [node('svc', 'tail')])])])++ it.each<[string, TreeNode, string, ReturnType<typeof names>]>([+ ['target not found → original tree', targetDirectChild, 'missing', names(targetDirectChild)],+ [+ 'target is direct child of root → drops siblings, keeps target subtree',+ targetDirectChild,+ 'target',+ {+ name: '<ROOT>',+ children: [+ {+ name: 'target',+ children: [+ { name: 'leaf-a', children: [] },+ { name: 'leaf-b', children: [] },+ ],+ },+ ],+ },+ ],+ [+ 'target deep in tree → keeps ancestor chain and full descendant subtree',+ targetDeep,+ 'target',+ {+ name: '<ROOT>',+ children: [+ {+ name: 'top',+ children: [+ {+ name: 'mid',+ children: [+ {+ name: 'target',+ children: [+ { name: 'descendant-a', children: [] },+ { name: 'descendant-b', children: [] },+ ],+ },+ ],+ },+ ],+ },+ ],+ },+ ],+ [+ 'cycle-broken node on lineage path → pruned like any other path',+ cycleBroken,+ 'target',+ {+ name: '<ROOT>',+ children: [+ {+ name: 'loop',+ children: [{ name: 'target', children: [{ name: 'tail', children: [] }] }],+ },+ ],+ },+ ],+ ])('%s', (_label, tree, target, expected) => {+ expect(names(pruneToLineage(tree, target))).toEqual(expected)+ })++ it('returns the original tree by reference when target is missing', () => {+ expect(pruneToLineage(targetDirectChild, 'missing')).toBe(targetDirectChild)+ })+})diff --git a/products/tracing/frontend/TraceCompareFlame.tsx b/products/tracing/frontend/TraceCompareFlame.tsxindex 71d4dcb0f00b..c89b35e6dd9c 100644--- a/products/tracing/frontend/TraceCompareFlame.tsx+++ b/products/tracing/frontend/TraceCompareFlame.tsx@@ -11,7 +11,7 @@ import { SpanTreeNode } from '~/queries/schema/schema-general'import { formatDuration } from './TraceFlameChart'-interface TreeNode {+export interface TreeNode {serviceName: stringname: stringnode: SpanTreeNode | null@@ -441,13 +441,45 @@ function findPathByName(root: TreeNode, name: string): string[] | null {return null}+/**+ * Prune the tree to keep only spans that can reach the target via parent or child:+ * the unique path from the synthetic root down to the target, plus the entire subtree+ * rooted at the target. Sibling/cousin subtrees of ancestors are dropped.+ *+ * Returns the original tree if the target name isn't found, so callers don't accidentally+ * blank the flame when the row's span name no longer matches anything in the result set.+ */+export function pruneToLineage(root: TreeNode, targetName: string): TreeNode {+ const path = findPathByName(root, targetName)+ if (!path) {+ return root+ }+ function walk(node: TreeNode, depth: number, lineage: string[]): TreeNode {+ if (depth >= lineage.length) {+ return node+ }+ const nextKey = lineage[depth]+ return {+ ...node,+ children: node.children+ .filter((c) => nodeKey(c.serviceName, c.name) === nextKey)+ .map((c) => walk(c, depth + 1, lineage)),+ }+ }+ return walk(root, 0, path)+}+export function TraceCompareFlame({current,previous,loading,initialSpanName,}: TraceCompareFlameProps): JSX.Element {- const tree = useMemo(() => buildTree(current, previous), [current, previous])+ const rawTree = useMemo(() => buildTree(current, previous), [current, previous])+ const tree = useMemo(+ () => (initialSpanName ? pruneToLineage(rawTree, initialSpanName) : rawTree),+ [rawTree, initialSpanName]+ )// 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[]>([])