Skip to content

Commit 300c9e4

Browse files
niieanibbrzoska
authored andcommitted
fix: correct span tracking
1 parent 71d8700 commit 300c9e4

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/v3/TraceManagerDebugger.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,13 +1645,22 @@ export default function TraceManagerDebugger<
16451645
useEffect(() => {
16461646
// schedule updates asynchronously so we never call setState mid‐render
16471647
const schedule = (fn: () => void) => void setTimeout(fn, 0)
1648+
// Create a map to store spans for each trace by ID
1649+
const traceEntriesMap = new Map<
1650+
string,
1651+
SpanAndAnnotation<RelationSchemasT>[]
1652+
>()
16481653

16491654
// Subscribe to trace-start events
16501655
const startSub = traceManager.when('trace-start').subscribe((event) => {
16511656
const trace = event.traceContext as AllPossibleTraces<RelationSchemasT>
1657+
const traceId = trace.input.id
1658+
1659+
// Create a new entry array for this trace (or clear existing)
1660+
traceEntriesMap.set(traceId, [])
16521661

16531662
const traceInfo: TraceInfo<RelationSchemasT> = {
1654-
traceId: trace.input.id,
1663+
traceId,
16551664
traceName: trace.definition.name,
16561665
variant: trace.input.variant as string,
16571666
state: trace.stateMachine.currentState,
@@ -1805,23 +1814,29 @@ export default function TraceManagerDebugger<
18051814
}),
18061815
)
18071816
})
1808-
1809-
const entries: SpanAndAnnotation<RelationSchemasT>[] = []
1810-
18111817
// Subscribe to add-span-to-recording for live info
18121818
const addSpanSub = traceManager
18131819
.when('add-span-to-recording')
18141820
.subscribe((event) => {
1821+
// Add the span to the trace entries map regardless of current UI state
1822+
const trace = event.traceContext
1823+
const traceId = trace.input.id
1824+
1825+
// Get or initialize entries array for this specific trace
1826+
if (!traceEntriesMap.has(traceId)) {
1827+
traceEntriesMap.set(traceId, [])
1828+
}
1829+
const entries = traceEntriesMap.get(traceId)!
1830+
entries.push(event.spanAndAnnotation)
1831+
1832+
// Now update the UI if this is the currently displayed trace
18151833
schedule(
18161834
() =>
18171835
void setCurrentTrace((prevTrace) => {
18181836
if (!prevTrace) return prevTrace
1819-
if (event.traceContext.input.id !== prevTrace.traceId) {
1837+
if (traceId !== prevTrace.traceId) {
18201838
return prevTrace
18211839
}
1822-
// Calculate live info from traceContext
1823-
const trace = event.traceContext
1824-
entries.push(event.spanAndAnnotation)
18251840

18261841
const liveDuration =
18271842
entries.length > 0

0 commit comments

Comments
 (0)