-
Notifications
You must be signed in to change notification settings - Fork 99
[WIP] Optimize data visualization on the tracing page #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jzstkarol
wants to merge
7
commits into
agentscope-ai:main
Choose a base branch
from
jzstkarol:jzst/dev_0205
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a1a965e
Unflatten attributes of spans
Cirilla-zmh 16661a9
format
Cirilla-zmh 56b0435
1. 瀑布图优化
12c1024
fix
jzstkarol f9ac9c3
Add migration script to flatten existing spans
Cirilla-zmh 94753d3
format
Cirilla-zmh 001e533
json-view 组件替换
jzstkarol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,12 +31,36 @@ import { | |
| } from '@/utils/common'; | ||
| import { SpanData } from '@shared/types/trace'; | ||
| import { getNestedValue } from '@shared/utils/objectUtils'; | ||
| import ReactJson from 'react-json-view'; | ||
|
|
||
| interface SpanTreeNode { | ||
| span: SpanData; | ||
| children?: SpanTreeNode[]; | ||
| } | ||
|
|
||
| const GEN_AI_OPERATION_COLORS: Record< | ||
| string, | ||
| string | ||
| > = { | ||
| invoke_agent: 'hsl(217 91% 60% / 0.8)', | ||
| chat: 'hsl(142 71% 45% / 0.8)', | ||
| create_agent: 'hsl(271 91% 65% / 0.8)', | ||
| embeddings: 'hsl(25 95% 53% / 0.8)', | ||
| execute_tool: 'hsl(174 72% 45% / 0.8)', | ||
| generate_content: 'hsl(239 84% 67% / 0.8)', | ||
| retrieval: 'hsl(43 96% 56% / 0.8)', | ||
| text_completion: 'hsl(350 89% 60% / 0.8)', | ||
| }; | ||
|
|
||
| const getFlameBarColor = (span: SpanData): string => { | ||
| const attrs = span.attributes || {}; | ||
| const op = (getNestedValue(attrs, 'gen_ai.operation.name') ?? | ||
| attrs['gen_ai.operation.name']) as | ||
| | string | ||
| | undefined; | ||
| return (op && GEN_AI_OPERATION_COLORS[op]) ?? 'hsl(0 0% 60% / 0.6)'; | ||
| }; | ||
|
|
||
| const getStatusIcon = (statusCode: number) => { | ||
| if (statusCode === 2) { | ||
| return <XCircleIcon className="size-4 text-destructive" />; | ||
|
|
@@ -135,6 +159,19 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| return Number(latestEnd - earliestStart) / 1e9; | ||
| }, [filteredSpans]); | ||
|
|
||
| // Time range for flame graph: min = earliest start, max = latest end | ||
| const flameGraphTimeRange = useMemo(() => { | ||
| if (!filteredSpans.length) return null; | ||
| const startTimes = filteredSpans.map((s) => | ||
| BigInt(s.startTimeUnixNano), | ||
| ); | ||
| const endTimes = filteredSpans.map((s) => BigInt(s.endTimeUnixNano)); | ||
| const min = startTimes.reduce((a, b) => (a < b ? a : b)); | ||
| const max = endTimes.reduce((a, b) => (a > b ? a : b)); | ||
| const range = max - min; | ||
| return { min, max, range: range > 0n ? range : 1n }; | ||
| }, [filteredSpans]); | ||
|
|
||
| // Display span (selected or root) | ||
| const displaySpan = selectedSpan || rootSpan; | ||
|
|
||
|
|
@@ -217,21 +254,36 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| return undefined; | ||
| }; | ||
|
|
||
| const getFlameBarStyle = (span: SpanData) => { | ||
| if (!flameGraphTimeRange) return { left: 0, width: 0 }; | ||
| const start = BigInt(span.startTimeUnixNano); | ||
| const end = BigInt(span.endTimeUnixNano); | ||
| const { min, range } = flameGraphTimeRange; | ||
| const leftPercent = | ||
| Number((start - min) * 10000n / range) / 100; | ||
| const widthPercent = | ||
| Number((end - start) * 10000n / range) / 100; | ||
| return { left: leftPercent, width: Math.max(widthPercent, 1) }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }; | ||
|
|
||
| // 火焰图条左边缘与行内容区对齐,不随层级缩进,便于时间轴对比 | ||
| const FLAME_BAR_LEFT_OFFSET_PX = 8; | ||
|
|
||
| const renderTreeNode = (node: SpanTreeNode, level = 0): React.ReactNode => { | ||
| const duration = | ||
| Number( | ||
| BigInt(node.span.endTimeUnixNano) - | ||
| BigInt(node.span.startTimeUnixNano), | ||
| BigInt(node.span.startTimeUnixNano), | ||
| ) / 1e9; | ||
| const isSelected = selectedSpanId === node.span.spanId; | ||
| const hasChildren = node.children && node.children.length > 0; | ||
| const flameStyle = getFlameBarStyle(node.span); | ||
|
|
||
| return ( | ||
| <div key={node.span.spanId} className="w-full"> | ||
| <div | ||
| className={`flex items-center gap-2 p-2 rounded-md cursor-pointer hover:bg-muted overflow-hidden ${ | ||
| isSelected ? 'bg-muted' : '' | ||
| }`} | ||
| className={`relative flex items-center gap-2 p-2 rounded-md cursor-pointer hover:bg-muted overflow-hidden ${isSelected ? 'bg-muted' : '' | ||
| }`} | ||
| style={{ paddingLeft: `${level * 16 + 8}px` }} | ||
| onClick={() => setSelectedSpanId(node.span.spanId)} | ||
| > | ||
|
|
@@ -267,12 +319,39 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| <span className="text-xs break-all max-w-[400px]"> | ||
| {node.span.name} | ||
| </span> | ||
| {flameGraphTimeRange && ( | ||
| <> | ||
| <br /> | ||
| <span className="text-xs text-muted-foreground"> | ||
| {formatDurationWithUnit(duration)} | ||
| </span> | ||
| </> | ||
| )} | ||
| </TooltipContent> | ||
| </Tooltip> | ||
| <span className="text-xs text-muted-foreground shrink-0"> | ||
| {formatDurationWithUnit(duration)} | ||
| </span> | ||
| {getStatusIcon(node.span.status?.code || 0)} | ||
| {/* 火焰图条:绝对定位,左边缘固定不随层级缩进 */} | ||
| {flameGraphTimeRange && ( | ||
| <div | ||
| className="absolute bottom-0 h-1 bg-muted overflow-hidden pointer-events-none" | ||
| style={{ | ||
| left: `${FLAME_BAR_LEFT_OFFSET_PX}px`, | ||
| right: 8, | ||
| }} | ||
| > | ||
| <div | ||
| className="absolute top-0 bottom-0 rounded-sm transition-colors" | ||
| style={{ | ||
| left: `${flameStyle.left}%`, | ||
| width: `${flameStyle.width}%`, | ||
| backgroundColor: getFlameBarColor(node.span), | ||
| }} | ||
| /> | ||
| </div> | ||
| )} | ||
| </div> | ||
| {hasChildren && | ||
| expandedNodes.has(node.span.spanId) && | ||
|
|
@@ -410,9 +489,9 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| BigInt( | ||
| displaySpan.endTimeUnixNano, | ||
| ) - | ||
| BigInt( | ||
| displaySpan.startTimeUnixNano, | ||
| ), | ||
| BigInt( | ||
| displaySpan.startTimeUnixNano, | ||
| ), | ||
| ) / 1e9, | ||
| )} | ||
| </div> | ||
|
|
@@ -465,13 +544,9 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| <CopyIcon className="size-3" /> | ||
| </Button> | ||
| </div> | ||
| <pre className="bg-muted p-3 rounded-md overflow-auto max-h-[300px] text-xs"> | ||
| {JSON.stringify( | ||
| extractInput(displaySpan), | ||
| null, | ||
| 2, | ||
| )} | ||
| </pre> | ||
| <div className="bg-muted p-3 rounded-md overflow-auto max-h-[300px] text-xs"> | ||
| <ReactJson src={extractInput(displaySpan) as object} name={false} displayDataTypes={false} /> | ||
| </div> | ||
| </div> | ||
| <Separator /> | ||
| <div> | ||
|
|
@@ -498,13 +573,9 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| <CopyIcon className="size-3" /> | ||
| </Button> | ||
| </div> | ||
| <pre className="bg-muted p-3 rounded-md overflow-auto max-h-[300px] text-xs"> | ||
| {JSON.stringify( | ||
| extractOutput(displaySpan), | ||
| null, | ||
| 2, | ||
| )} | ||
| </pre> | ||
| <div className="bg-muted p-3 rounded-md overflow-auto max-h-[300px] text-xs"> | ||
| <ReactJson src={extractInput(displaySpan) as object} name={false} displayDataTypes={false} /> | ||
jzstkarol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </div> | ||
| </div> | ||
| </div> | ||
| </AccordionContent> | ||
|
|
@@ -535,13 +606,9 @@ const TraceDetailPage = ({ traceId }: TraceDetailPageProps) => { | |
| <CopyIcon className="size-3" /> | ||
| </Button> | ||
| </div> | ||
| <pre className="bg-muted p-3 rounded-md overflow-auto text-xs"> | ||
| {JSON.stringify( | ||
| displaySpan.attributes, | ||
| null, | ||
| 2, | ||
| )} | ||
| </pre> | ||
| <div className="bg-muted p-3 rounded-md overflow-auto text-xs"> | ||
| <ReactJson src={displaySpan.attributes as object} name={false} displayDataTypes={false} /> | ||
| </div> | ||
| </AccordionContent> | ||
| </AccordionItem> | ||
| </Accordion> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getNestedValue工具函数已经被更新,可以同时处理扁平(flat)和嵌套(nested)的对象结构。因此,这里的?? attrs['gen_ai.operation.name']回退逻辑是多余的,可以移除以使代码更简洁。