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(alerts): stop filing issues when the alerts setup check gets a 403 (#108982)

posthog[bot]ebebc5953713
diff --git a/products/alerts/frontend/emptyState/alertsSetupLogic.test.ts b/products/alerts/frontend/emptyState/alertsSetupLogic.test.ts
index 6d2897e0a95c..15e9011a4cde 100644
--- a/products/alerts/frontend/emptyState/alertsSetupLogic.test.ts
+++ b/products/alerts/frontend/emptyState/alertsSetupLogic.test.ts
@@ -1,5 +1,6 @@
import { expectLogic } from 'kea-test-utils'
+import { ApiError } from 'lib/api-error'
import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import * as appContext from 'lib/utils/getAppContext'
@@ -70,9 +71,27 @@ describe('alertsSetupLogic', () => {
expect(insightAlertsSpy).not.toHaveBeenCalled()
})
- it('fails open to unknown when a count query fails before any answer', async () => {
- insightAlertsSpy.mockRejectedValue(new Error('network down'))
- logAlertsSpy.mockResolvedValue({ count: 0, results: [] })
- expect(await mountAndReadStatus()).toBe('unknown')
+ // A backend 403 means that kind is unreadable, so it must not fail the check and file an error.
+ it.each([
+ ['a 403 with log alerts readable', new ApiError(undefined, 403), 3, 'detectStatusSuccess', 'has-data'],
+ [
+ 'a 403 on both kinds',
+ new ApiError(undefined, 403),
+ new ApiError(undefined, 403),
+ 'detectStatusSuccess',
+ 'unknown',
+ ],
+ ['any other failure', new Error('network down'), 0, 'detectStatusFailure', 'unknown'],
+ ] as const)('handles %s on the insight alerts count', async (_name, insightError, logAlerts, outcome, expected) => {
+ insightAlertsSpy.mockRejectedValue(insightError)
+ if (typeof logAlerts === 'number') {
+ logAlertsSpy.mockResolvedValue({ count: logAlerts, results: [] })
+ } else {
+ logAlertsSpy.mockRejectedValue(logAlerts)
+ }
+ const logic = alertsSetupLogic()
+ logic.mount()
+ await expectLogic(logic).toDispatchActions([outcome]).toFinishAllListeners()
+ expect(productSetupStatusLogic({ productKey: ProductKey.ALERTS }).values.status).toBe(expected)
})
})
diff --git a/products/alerts/frontend/emptyState/alertsSetupLogic.ts b/products/alerts/frontend/emptyState/alertsSetupLogic.ts
index 0f76721f3324..966d8d57bb08 100644
--- a/products/alerts/frontend/emptyState/alertsSetupLogic.ts
+++ b/products/alerts/frontend/emptyState/alertsSetupLogic.ts
@@ -1,3 +1,4 @@
+import { ApiError } from 'lib/api-error'
import { createSetupDetectionLogic } from 'lib/components/ProductEmptyState/setupDetectionLogic'
import { projectLogic } from 'scenes/projectLogic'
@@ -9,6 +10,18 @@ import { logsAlertsList } from 'products/logs/frontend/generated/api'
import { alertsList } from '../generated/api'
import { hasEffectiveResourceAccess } from '../utils'
+// The frontend access check can pass while the backend still answers 403. Treat that kind as unreadable.
+async function countUnlessForbidden(request: Promise<{ count: number }>): Promise<number | null> {
+ try {
+ return (await request).count
+ } catch (error) {
+ if (error instanceof ApiError && error.status === 403) {
+ return null
+ }
+ throw error
+ }
+}
+
/**
* Setup detection for the alerts empty state. The scene serves both alert kinds, so
* either one counts as set up - a project alerting only on logs must not be told it
@@ -29,11 +42,14 @@ export const alertsSetupLogic = createSetupDetectionLogic({
}
const projectId = String(projectLogic.findMounted()?.values.currentProjectId)
- const [insightAlerts, logAlerts] = await Promise.all([
- canViewInsightAlerts ? alertsList(projectId, { limit: 1 }) : null,
- canViewLogAlerts ? logsAlertsList(projectId, { limit: 1 }) : null,
+ const counts = await Promise.all([
+ canViewInsightAlerts ? countUnlessForbidden(alertsList(projectId, { limit: 1 })) : null,
+ canViewLogAlerts ? countUnlessForbidden(logsAlertsList(projectId, { limit: 1 })) : null,
])
- const count = (insightAlerts?.count ?? 0) + (logAlerts?.count ?? 0)
- return count > 0 ? 'has-data' : 'needs-setup'
+ const readableCounts = counts.filter((count): count is number => count !== null)
+ if (readableCounts.length === 0) {
+ return 'unknown'
+ }
+ return readableCounts.some((count) => count > 0) ? 'has-data' : 'needs-setup'
},
})