Skip to content

Commit 4ac7e82

Browse files
JoshuaKGoldbergCursor Agent
andcommitted
feat(explore): Vary logs infinite query maxPages by cached rows
Use a lower page cap until enough rows are cached locally, then allow a higher cap. Co-Authored-By: Cursor Agent <noreply@cursor.com> Made-with: Cursor
1 parent 33054d2 commit 4ac7e82

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

static/app/views/explore/logs/constants.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export const LOG_ATTRIBUTE_LAZY_LOAD_HOVER_TIMEOUT = 150;
2020
export const DEFAULT_TRACE_ITEM_HOVER_TIMEOUT = 150;
2121
export const DEFAULT_TRACE_ITEM_HOVER_TIMEOUT_WITH_AUTO_REFRESH = 400; // With autorefresh on, a stationary mouse can prefetch multiple rows since virtual time moves rows constantly.
2222
export const MAX_LOGS_INFINITE_QUERY_PAGES = 30; // This number * the refresh interval must be more seconds than 2 * the smallest time interval in the chart for streaming to work.
23+
/** Larger page cap once enough rows are cached (see useInfiniteLogsQuery). */
24+
export const MAX_LOGS_INFINITE_QUERY_PAGES_EXPANDED = 300;
25+
/** Below this many rows in the client cache, use {@link MAX_LOGS_INFINITE_QUERY_PAGES}. */
26+
export const LOCAL_LOG_ROWS_FOR_EXPANDED_INFINITE_PAGES = 500;
2327

2428
/**
2529
* These are required fields are always added to the query when fetching the log table.

static/app/views/explore/logs/useLogsQuery.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {useCallback, useEffect, useMemo, useState} from 'react';
22
import {logger} from '@sentry/react';
3+
import type {QueryClient} from '@tanstack/react-query';
34

45
import {type ApiResult} from 'sentry/api';
56
import {usePageFilters} from 'sentry/components/pageFilters/usePageFilters';
@@ -28,8 +29,10 @@ import {SAMPLING_MODE} from 'sentry/views/explore/hooks/useProgressiveQuery';
2829
import {useTraceItemDetails} from 'sentry/views/explore/hooks/useTraceItemDetails';
2930
import {
3031
AlwaysPresentLogFields,
32+
LOCAL_LOG_ROWS_FOR_EXPANDED_INFINITE_PAGES,
3133
MAX_LOG_INGEST_DELAY,
3234
MAX_LOGS_INFINITE_QUERY_PAGES,
35+
MAX_LOGS_INFINITE_QUERY_PAGES_EXPANDED,
3336
QUERY_PAGE_LIMIT,
3437
QUERY_PAGE_LIMIT_WITH_AUTO_REFRESH,
3538
} from 'sentry/views/explore/logs/constants';
@@ -402,6 +405,19 @@ type QueryKey = [
402405
'infinite',
403406
];
404407

408+
/**
409+
* `maxPages` is evaluated before `useInfiniteQuery` returns `data`, so we base it on the
410+
* query cache (same snapshot React Query will use for this key).
411+
*/
412+
function maxPagesForLogsInfiniteQuery(client: QueryClient, queryKey: QueryKey): number {
413+
const cached = client.getQueryData<InfiniteData<ApiResult<EventsLogsResult>>>(queryKey);
414+
const rows =
415+
cached?.pages?.reduce((n, page) => n + (page[0]?.data?.length ?? 0), 0) ?? 0;
416+
return rows < LOCAL_LOG_ROWS_FOR_EXPANDED_INFINITE_PAGES
417+
? MAX_LOGS_INFINITE_QUERY_PAGES
418+
: MAX_LOGS_INFINITE_QUERY_PAGES_EXPANDED;
419+
}
420+
405421
export function useInfiniteLogsQuery({
406422
disabled,
407423
highFidelity,
@@ -526,7 +542,7 @@ export function useInfiniteLogsQuery({
526542
initialPageParam,
527543
enabled: !disabled,
528544
staleTime: autoRefresh ? Infinity : getStaleTimeForEventView(other.eventView),
529-
maxPages: MAX_LOGS_INFINITE_QUERY_PAGES,
545+
maxPages: maxPagesForLogsInfiniteQuery(queryClient, queryKeyWithInfinite),
530546
refetchIntervalInBackground: true, // Don't refetch when tab is not visible
531547
});
532548

0 commit comments

Comments
 (0)