Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(hog-functions): use offset pagination for grouped logs instead of timestamp cursor (#64238)

Daniel Marchuk7bd14384d638
diff --git a/frontend/src/scenes/hog-functions/logs/logsViewerLogic.test.ts b/frontend/src/scenes/hog-functions/logs/logsViewerLogic.test.ts
index 22e31aa020bd..77ec381fb773 100644
--- a/frontend/src/scenes/hog-functions/logs/logsViewerLogic.test.ts
+++ b/frontend/src/scenes/hog-functions/logs/logsViewerLogic.test.ts
@@ -3,7 +3,13 @@ import { teamLogic } from 'scenes/teamLogic'
import { LogEntryLevel } from '~/types'
-import { groupLogs, LogEntry, toAbsoluteClickhouseTimestamp } from './logsViewerLogic'
+import {
+ buildGroupedLogsQuery,
+ groupLogs,
+ LogEntry,
+ LogEntryParams,
+ toAbsoluteClickhouseTimestamp,
+} from './logsViewerLogic'
const makeEntry = (instanceId: string, timestamp: string, level: LogEntryLevel = 'INFO'): LogEntry => ({
instanceId,
@@ -155,4 +161,44 @@ describe('logsViewerLogic', () => {
expect(groups.map((g) => g.instanceId)).toEqual(['c', 'b', 'a'])
})
})
+
+ describe('buildGroupedLogsQuery', () => {
+ const makeParams = (overrides: Partial<LogEntryParams> = {}): LogEntryParams => ({
+ sourceType: 'hog_flow',
+ sourceId: 'batch-job-id',
+ levels: ['INFO', 'ERROR'],
+ searchGroups: [],
+ order: 'DESC',
+ ...overrides,
+ })
+
+ it.each([
+ {
+ description: 'paginates the group subquery with the requested limit',
+ args: [10, 20],
+ expected: 'LIMIT 10',
+ },
+ { description: 'offsets the group subquery to the requested page', args: [10, 20], expected: 'OFFSET 20' },
+ { description: 'defaults to offset 0', args: [10], expected: 'OFFSET 0' },
+ {
+ description: 'orders groups with a stable instance_id tiebreaker so offset pages do not skip or repeat',
+ args: [10],
+ expected: 'ORDER BY max(timestamp) DESC, instance_id DESC',
+ },
+ ] as { description: string; args: [number, number?]; expected: string }[])(
+ '$description',
+ ({ args, expected }) => {
+ expect(buildGroupedLogsQuery(makeParams(), ...args)).toContain(expected)
+ }
+ )
+
+ it('pages by offset alone, without introducing a timestamp cursor', () => {
+ // A batch fires every instance within the same millisecond, so successive pages must differ ONLY by
+ // the offset — never by a `timestamp < cursor` boundary, which would hide the remaining groups.
+ const firstPage = buildGroupedLogsQuery(makeParams(), 10, 0)
+ const secondPage = buildGroupedLogsQuery(makeParams(), 10, 10)
+
+ expect(firstPage.replace('OFFSET 0', 'OFFSET 10')).toEqual(secondPage)
+ })
+ })
})
diff --git a/frontend/src/scenes/hog-functions/logs/logsViewerLogic.tsx b/frontend/src/scenes/hog-functions/logs/logsViewerLogic.tsx
index 6cc903011430..d76d247e1d50 100644
--- a/frontend/src/scenes/hog-functions/logs/logsViewerLogic.tsx
+++ b/frontend/src/scenes/hog-functions/logs/logsViewerLogic.tsx
@@ -147,11 +147,17 @@ const loadLogs = async (request: LogEntryParams): Promise<LogEntry[]> => {
)
}
-const loadGroupedLogs = async (request: LogEntryParams, excludeInstanceIds?: string[]): Promise<LogEntry[]> => {
- const excludeFilter =
- excludeInstanceIds && excludeInstanceIds.length > 0 ? hogql`AND instance_id NOT IN ${excludeInstanceIds}` : ''
-
- const query = hogql`
+// Grouped pagination keyset-pages over instance groups by offsetting the group subquery, ordered by
+// most recent activity. We deliberately do NOT paginate with a `timestamp < cursor` boundary: batch
+// workflows emit hundreds of instances within the same millisecond, so a timestamp cursor (truncated
+// to ms) excludes every remaining group at once and the viewer falsely reports "No older entries".
+// The instance_id tiebreaker keeps the ordering stable across pages.
+export const buildGroupedLogsQuery = (
+ request: LogEntryParams,
+ groupLimit: number,
+ groupOffset: number = 0
+): HogQLQueryString => {
+ return hogql`
SELECT instance_id, timestamp, level, message
FROM log_entries
WHERE 1=1
@@ -162,13 +168,21 @@ const loadGroupedLogs = async (request: LogEntryParams, excludeInstanceIds?: str
WHERE 1=1
${hogql.raw(buildBoundaryFilters(request))}
${hogql.raw(buildSearchFilters(request))}
- ${hogql.raw(excludeFilter as string)}
GROUP BY instance_id
- ORDER BY max(timestamp) ${hogql.raw(request.order)}
- LIMIT ${LOG_GROUP_LIMIT}
+ ORDER BY max(timestamp) ${hogql.raw(request.order)}, instance_id DESC
+ LIMIT ${groupLimit}
+ OFFSET ${groupOffset}
)
ORDER BY timestamp DESC
LIMIT ${LOG_GROUP_TOTAL_LOGS_LIMIT}`
+}
+
+const loadGroupedLogs = async (
+ request: LogEntryParams,
+ groupLimit: number = LOG_GROUP_LIMIT,
+ groupOffset: number = 0
+): Promise<LogEntry[]> => {
+ const query = buildGroupedLogsQuery(request, groupLimit, groupOffset)
const response = await api.queryHogQL(
query,
@@ -345,16 +359,11 @@ export const logsViewerLogic = kea<logsViewerLogicType>([
return groupLogs(results)
},
loadMoreGroupedLogs: async () => {
- if (!values.oldestLogTimestamp) {
- return values.groupedLogs
- }
- const logParams: LogEntryParams = {
- ...values.logEntryParams,
- dateTo: toAbsoluteClickhouseTimestamp(values.oldestLogTimestamp),
- }
-
- const existingInstanceIds = values.groupedLogs.map((group) => group.instanceId)
- const results = await loadGroupedLogs(logParams, existingInstanceIds)
+ const results = await loadGroupedLogs(
+ values.logEntryParams,
+ LOG_GROUP_LIMIT,
+ values.groupedLogs.length
+ )
if (!results.length) {
actions.markLogsEnd()