Skip to content

Commit d83a5bb

Browse files
committed
fix: prevent logs from disappearing
1 parent cafbadd commit d83a5bb

6 files changed

Lines changed: 155 additions & 21 deletions

File tree

‎packages/app/src/components/DBTraceWaterfallChart.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ export function DBTraceWaterfallChartContainer({
793793
meta: logRowsMeta,
794794
filterError: logFilterError,
795795
fatalError: logFatalError,
796+
isTruncated: logIsTruncated,
796797
} = useFilteredEventsAroundFocus({
797798
// search data if logTableModel exist
798799
// search invalid date range if no logTableModel(react hook need execute no matter what)
@@ -834,8 +835,10 @@ export function DBTraceWaterfallChartContainer({
834835
traceId,
835836
highlightedRowWhere,
836837
initialRowHighlightHint,
837-
// Only while the dots show. A log filter asks for the rows it matches.
838-
foldTimingRows: showSpans && showSpanEvents && !logWhere,
838+
// Only while the dots show. A log filter asks for the rows it matches. A
839+
// capped list can keep a log that shares a timing log's key and drop the
840+
// timing log, and the fold would then hide that other log.
841+
foldTimingRows: showSpans && showSpanEvents && !logWhere && !logIsTruncated,
839842
enabled: !traceIsFetching,
840843
});
841844

‎packages/app/src/components/__tests__/DBTraceWaterfallChart.test.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,49 @@ describe('DBTraceWaterfallChartContainer', () => {
12631263
expect(lastFold()).toBe(false);
12641264
});
12651265

1266+
it('keeps the timing rows when the log list is capped', async () => {
1267+
setupQueryMocks({
1268+
traceData: mockTraceData,
1269+
logData: {
1270+
data: Array.from(
1271+
{ length: TRACE_WATERFALL_ROW_LIMIT + 1 },
1272+
() => mockLogData.data[0],
1273+
),
1274+
meta: [{ totalCount: TRACE_WATERFALL_ROW_LIMIT + 1 }],
1275+
},
1276+
});
1277+
renderComponent();
1278+
await waitForLoading();
1279+
1280+
expect(lastFold()).toBe(false);
1281+
});
1282+
1283+
it('sends the same log queries whether or not the source has browser event fields', () => {
1284+
const logQueries = (logSource: TLogSource) => {
1285+
mockUseOffsetPaginatedQuery.mockClear();
1286+
renderComponent(logSource).unmount();
1287+
return mockUseOffsetPaginatedQuery.mock.calls
1288+
.filter(([query]) => query?.from?.tableName === 'log_table')
1289+
.map(([query, opts]) => [query, opts?.enabled]);
1290+
};
1291+
setupQueryMocks({ traceData: mockTraceData, logData: mockLogData });
1292+
const correlatedSource = {
1293+
...mockLogTableSource,
1294+
traceIdExpression: 'TraceId',
1295+
spanIdExpression: 'SpanId',
1296+
};
1297+
1298+
const withEventFields = logQueries({
1299+
...correlatedSource,
1300+
eventNameExpression: 'EventName',
1301+
eventAttributesExpression: 'LogAttributes',
1302+
});
1303+
1304+
expect(withEventFields.some(([, enabled]) => enabled)).toBe(true);
1305+
expect(withEventFields).toEqual(logQueries(correlatedSource));
1306+
expect(JSON.stringify(withEventFields)).not.toContain('EventName');
1307+
});
1308+
12661309
describe('triggered-by row', () => {
12671310
const triggeredBy = 'Triggered by: Clicked testid=checkout · 3ms before';
12681311
const triggerRow: TraceTriggerRow = {

‎packages/app/src/hooks/__tests__/useBrowserTraceEvents.test.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ describe('useBrowserTraceEvents', () => {
213213
expect(result.current.logRows).toEqual([consoleLogRow]);
214214
});
215215

216+
it("keeps a log in the timing log's millisecond on its span, and the timing row", () => {
217+
const errorRow = {
218+
...consoleLogRow,
219+
id: 'error-row',
220+
Timestamp: TIMING_TIMESTAMP,
221+
Body: '',
222+
};
223+
const { result } = setup({ logRows: [...logRows, errorRow] });
224+
225+
expect(result.current.resourceTimingBySpanId.has('span-1')).toBe(true);
226+
expect(result.current.logRows).toEqual([...logRows, errorRow]);
227+
});
228+
216229
it('keeps the timing row while its dots are off screen', () => {
217230
const { result, rerender, initialProps } = setup({
218231
foldTimingRows: false,

‎packages/app/src/hooks/useBrowserTraceEvents.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '@/utils/browserTraceContext';
1919
import {
2020
buildResourceTimingQuery,
21+
getFoldedRowKeys,
2122
getResourceTimingMarkers,
2223
ResourceTimingMarker,
2324
resourceTimingRowKey,
@@ -52,7 +53,7 @@ export type TraceTriggerRow = {
5253
// Stable, so the waterfall's row memos don't rerun while there is nothing.
5354
const NO_RESOURCE_TIMING: ReturnType<typeof getResourceTimingMarkers> = {
5455
markersBySpanId: new Map(),
55-
drawnRowKeys: new Set(),
56+
drawnRowCounts: new Map(),
5657
};
5758

5859
/**
@@ -106,7 +107,7 @@ export function useBrowserTraceEvents<T extends LogRow>({
106107
});
107108
const timingResult =
108109
timingQuery != null && !timing.isError ? timing.data : undefined;
109-
const { markersBySpanId, drawnRowKeys } = useMemo(
110+
const { markersBySpanId, drawnRowCounts } = useMemo(
110111
() =>
111112
timingResult?.data.length
112113
? getResourceTimingMarkers(timingResult, traceRows)
@@ -118,19 +119,23 @@ export function useBrowserTraceEvents<T extends LogRow>({
118119
// would leave the detail panel showing a row the waterfall lacks, and the
119120
// hint would find nothing to select.
120121
const keptLogRows = useMemo(() => {
121-
if (!foldTimingRows || drawnRowKeys.size === 0) {
122+
if (!foldTimingRows || drawnRowCounts.size === 0) {
123+
return logRows;
124+
}
125+
const foldedRowKeys = getFoldedRowKeys(logRows, drawnRowCounts);
126+
if (foldedRowKeys.size === 0) {
122127
return logRows;
123128
}
124129
return logRows.filter(
125130
row =>
126-
!drawnRowKeys.has(resourceTimingRowKey(row.SpanId, row.Timestamp)) ||
131+
!foldedRowKeys.has(resourceTimingRowKey(row.SpanId, row.Timestamp)) ||
127132
row.id === highlightedRowWhere ||
128133
matchesRowHighlightHint(row, initialRowHighlightHint),
129134
);
130135
}, [
131136
logRows,
132137
foldTimingRows,
133-
drawnRowKeys,
138+
drawnRowCounts,
134139
highlightedRowWhere,
135140
initialRowHighlightHint,
136141
]);

‎packages/app/src/utils/__tests__/resourceTimingMarkers.test.ts‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@/hooks/__fixtures__/browserTraceEvents';
77
import {
88
buildResourceTimingQuery,
9+
getFoldedRowKeys,
910
getResourceTimingMarkers,
1011
resourceTimingRowKey,
1112
} from '@/utils/resourceTimingMarkers';
@@ -55,7 +56,7 @@ const draw = (
5556

5657
describe('getResourceTimingMarkers', () => {
5758
it('places dots in sdk-trace-web order relative to the span start', () => {
58-
const { markersBySpanId, drawnRowKeys } = draw(timingRow(sameOrigin));
59+
const { markersBySpanId, drawnRowCounts } = draw(timingRow(sameOrigin));
5960

6061
expect(markersBySpanId.get('span-1')).toEqual([
6162
{ name: 'fetchStart', epochMs: SPAN_START_MS, attributes: {} },
@@ -85,8 +86,8 @@ describe('getResourceTimingMarkers', () => {
8586
},
8687
},
8788
]);
88-
expect([...drawnRowKeys]).toEqual([
89-
resourceTimingRowKey('span-1', TIMING_TIMESTAMP),
89+
expect([...drawnRowCounts]).toEqual([
90+
[resourceTimingRowKey('span-1', TIMING_TIMESTAMP), 1],
9091
]);
9192
});
9293

@@ -164,9 +165,9 @@ describe('getResourceTimingMarkers', () => {
164165
['without a span id', timingRow(sameOrigin, { span_id: '' })],
165166
['with attributes that are not a map', timingRow('not a map')],
166167
])('draws nothing and keeps the row for a log %s', (_name, row) => {
167-
const { markersBySpanId, drawnRowKeys } = draw(row);
168+
const { markersBySpanId, drawnRowCounts } = draw(row);
168169
expect(markersBySpanId.size).toBe(0);
169-
expect(drawnRowKeys.size).toBe(0);
170+
expect(drawnRowCounts.size).toBe(0);
170171
});
171172

172173
it.each([
@@ -181,13 +182,60 @@ describe('getResourceTimingMarkers', () => {
181182
[{ name: 'timestamp', type: 'DateTime' }],
182183
],
183184
])('draws a log but keeps its row when %s', (_name, spans, meta) => {
184-
const { markersBySpanId, drawnRowKeys } = draw(
185+
const { markersBySpanId, drawnRowCounts } = draw(
185186
timingRow(sameOrigin),
186187
spans,
187188
meta,
188189
);
189190
expect(markersBySpanId.get('span-1')).toHaveLength(8);
190-
expect(drawnRowKeys.size).toBe(0);
191+
expect(drawnRowCounts.size).toBe(0);
192+
});
193+
194+
it('counts a timing log ingested twice once per copy', () => {
195+
const row = timingRow(sameOrigin);
196+
const { drawnRowCounts } = getResourceTimingMarkers(
197+
{ data: [row, row], meta: TIMESTAMP_META },
198+
traceRows,
199+
);
200+
expect([...drawnRowCounts]).toEqual([
201+
[resourceTimingRowKey('span-1', TIMING_TIMESTAMP), 2],
202+
]);
203+
});
204+
});
205+
206+
describe('getFoldedRowKeys', () => {
207+
const key = resourceTimingRowKey('span-1', TIMING_TIMESTAMP);
208+
const timingLogRow = { SpanId: 'span-1', Timestamp: TIMING_TIMESTAMP };
209+
const otherLogRow = {
210+
SpanId: 'span-1',
211+
Timestamp: '2026-10-22T04:52:37.825000000Z',
212+
};
213+
214+
it.each([
215+
['folds the timing row alone', [timingLogRow, otherLogRow], 1, [key]],
216+
[
217+
'keeps an exception in the same millisecond and the timing row',
218+
[timingLogRow, { ...timingLogRow, Body: '', SeverityNumber: 17 }],
219+
1,
220+
[],
221+
],
222+
[
223+
'keeps a console error in the same millisecond and the timing row',
224+
[timingLogRow, { ...timingLogRow, Body: 'boom', SeverityText: 'error' }],
225+
1,
226+
[],
227+
],
228+
[
229+
'folds two copies of a timing log drawn twice',
230+
[timingLogRow, timingLogRow],
231+
2,
232+
[key],
233+
],
234+
['ignores rows on other keys', [otherLogRow], 1, []],
235+
])('%s', (_name, logRows, drawn, expected) => {
236+
expect([...getFoldedRowKeys(logRows, new Map([[key, drawn]]))]).toEqual(
237+
expected,
238+
);
191239
});
192240
});
193241

‎packages/app/src/utils/resourceTimingMarkers.ts‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get } from 'lodash';
1+
import { countBy, get } from 'lodash';
22
import SqlString from 'sqlstring';
33
import type {
44
BuilderChartConfigWithDateRange,
@@ -58,13 +58,34 @@ export type ResourceTimingMarker = Omit<
5858
};
5959

6060
/**
61-
* Matches a timing log to its waterfall log row. Both queries select the same
62-
* timestamp expression, and another log on the span has another timestamp.
61+
* Matches a timing log to its waterfall log row: both queries select the same
62+
* timestamp expression. Another log on the span can share the key, so the
63+
* fold goes through getFoldedRowKeys.
6364
*/
6465
export function resourceTimingRowKey(spanId: unknown, timestamp: unknown) {
6566
return `${String(spanId)}|${String(timestamp)}`;
6667
}
6768

69+
/**
70+
* Log rows carry no event name, and an exception or console error on the span
71+
* in the same millisecond has the timing log's key. A key folds only when the
72+
* dots query drew a timing log for each log row with it. All rows with a key
73+
* share one timestamp, so an uncapped log list holds all of them or none.
74+
*/
75+
export function getFoldedRowKeys(
76+
logRows: readonly { SpanId?: unknown; Timestamp?: unknown }[],
77+
drawnRowCounts: ReadonlyMap<string, number>,
78+
): ReadonlySet<string> {
79+
const rowCounts = countBy(logRows, row =>
80+
resourceTimingRowKey(row.SpanId, row.Timestamp),
81+
);
82+
return new Set(
83+
[...drawnRowCounts]
84+
.filter(([key, drawn]) => rowCounts[key] <= drawn)
85+
.map(([key]) => key),
86+
);
87+
}
88+
6889
export function buildResourceTimingQuery({
6990
logSource,
7091
context,
@@ -191,7 +212,7 @@ export function getResourceTimingMarkers(
191212
}[],
192213
): {
193214
markersBySpanId: ReadonlyMap<string, ResourceTimingMarker[]>;
194-
drawnRowKeys: ReadonlySet<string>;
215+
drawnRowCounts: ReadonlyMap<string, number>;
195216
} {
196217
const spanStarts = new Map<string, number>();
197218
const shownSpanIds = new Set<string>();
@@ -208,7 +229,7 @@ export function getResourceTimingMarkers(
208229

209230
const foldable = hasSubSecondTimestamps(meta);
210231
const markersBySpanId = new Map<string, ResourceTimingMarker[]>();
211-
const drawnRowKeys = new Set<string>();
232+
const drawnRowCounts = new Map<string, number>();
212233
for (const row of data) {
213234
const spanId = isSet(row.span_id) ? String(row.span_id) : undefined;
214235
const spanStartMs = spanId != null ? spanStarts.get(spanId) : undefined;
@@ -221,8 +242,9 @@ export function getResourceTimingMarkers(
221242
...toMarkers(row.attributes, spanStartMs, fetchStart),
222243
]);
223244
if (foldable && shownSpanIds.has(spanId)) {
224-
drawnRowKeys.add(resourceTimingRowKey(spanId, row.timestamp));
245+
const key = resourceTimingRowKey(spanId, row.timestamp);
246+
drawnRowCounts.set(key, (drawnRowCounts.get(key) ?? 0) + 1);
225247
}
226248
}
227-
return { markersBySpanId, drawnRowKeys };
249+
return { markersBySpanId, drawnRowCounts };
228250
}

0 commit comments

Comments
 (0)