Skip to content

Commit 39f4fa5

Browse files
JoshuaKGoldbergCursor Agent
andauthored
fix(explore): increase logs infinite query maxPages when we don't have many rows locally (#112691)
Previously, the `maxPages` for infinitely querying logs was hardcoded to 30 (`MAX_LOGS_INFINITE_QUERY_PAGES`). This is good in case there are many big logs to amke sure the local browser doesn't run out of data. But if there are many "blank" pages (gaps) of query results, which can happen when the logs are very sporadic, we end up hitting the maximum page limit for queries and losing some. This goes for a higher 300 (`MAX_LOGS_INFINITE_QUERY_PAGES_EXPANDED`) when there are fewer than 500 (`LOCAL_LOG_ROWS_FOR_EXPANDED_INFINITE_PAGES`) rows locally. We're thinking this should allow more pages so we don't bump into the max storage - while not risking running out of browser memory. Fixes EXP-865 Made with [Cursor](https://cursor.com) The RCA and solution idea for this came from @k-fish (🥔), so essentially: Co-authored-by: @k-fish --------- Co-authored-by: Cursor Agent <noreply@cursor.com>
1 parent f4b9031 commit 39f4fa5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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_EXPANDED
418+
: MAX_LOGS_INFINITE_QUERY_PAGES;
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)