: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): show 24h drop impact per sampling rule in UI (#57583)
Daniel Visca82701548f316
diff --git a/products/logs/frontend/components/LogsSampling/LogsSamplingRulesSortableTable.tsx b/products/logs/frontend/components/LogsSampling/LogsSamplingRulesSortableTable.tsxindex e64dbebc3c10..1970156f0a5a 100644--- a/products/logs/frontend/components/LogsSampling/LogsSamplingRulesSortableTable.tsx+++ b/products/logs/frontend/components/LogsSampling/LogsSamplingRulesSortableTable.tsx@@ -20,10 +20,12 @@ import clsx from 'clsx'import { useActions, useValues } from 'kea'import { useEffect, useState } from 'react'-import { IconPlus } from '@posthog/icons'+import { IconInfo, IconPlus } from '@posthog/icons'import { LemonButton, LemonSwitch } from '@posthog/lemon-ui'import { SortableDragIcon } from 'lib/lemon-ui/icons'+import { Tooltip } from 'lib/lemon-ui/Tooltip/Tooltip'+import { compactNumber } from 'lib/utils'import { urls } from 'scenes/urls'import { LogsSamplingRuleApi } from 'products/logs/frontend/generated/api.schemas'@@ -31,12 +33,16 @@ import { LogsSamplingRuleApi } from 'products/logs/frontend/generated/api.schemaimport { logsSamplingSectionLogic } from './logsSamplingSectionLogic'import { ruleTypeLabel } from './ruleTypeLabel'+type RuleDropImpactCellState = 'loading' | 'ok' | 'error' | 'unknown'+interface SortableRowProps {row: LogsSamplingRuleApiorderIndex: numberdisabledReason: string | nullruleEnabledTogglePendingId: string | nullsaveRulesOrderPending: boolean+ ruleDropImpactCellState: RuleDropImpactCellState+ dropped24h: numberonSetRuleEnabled: (ruleId: string, enabled: boolean) => void}@@ -46,6 +52,8 @@ function SortableRow({disabledReason,ruleEnabledTogglePendingId,saveRulesOrderPending,+ ruleDropImpactCellState,+ dropped24h,onSetRuleEnabled,}: SortableRowProps): JSX.Element {const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({@@ -92,6 +100,26 @@ function SortableRow({</LemonButton></td><td className="py-2 px-2 text-secondary text-sm align-middle">{ruleTypeLabel(row.rule_type)}</td>+ <td className="py-2 px-2 text-secondary text-sm align-middle whitespace-nowrap">+ {ruleDropImpactCellState === 'loading' ? (+ <span className="text-muted">…</span>+ ) : ruleDropImpactCellState === 'ok' ? (+ <span>+ ~{compactNumber(dropped24h)} dropped <span className="text-muted">(24h)</span>+ </span>+ ) : (+ <span+ className="text-muted"+ title={+ ruleDropImpactCellState === 'error'+ ? 'Could not load 24h drop totals'+ : 'Drop impact not available'+ }+ >+ —+ </span>+ )}+ </td><td className="py-2 px-2 w-32 align-middle"><LemonSwitchchecked={row.enabled ?? false}@@ -111,8 +139,14 @@ function SortableRow({}export function LogsSamplingRulesSortableTable(): JSX.Element {- const { rules, rulesLoading, saveRulesOrderPending, ruleEnabledTogglePendingId } =- useValues(logsSamplingSectionLogic)+ const {+ rules,+ rulesLoading,+ saveRulesOrderPending,+ ruleEnabledTogglePendingId,+ ruleDropImpact,+ ruleDropImpactCellState,+ } = useValues(logsSamplingSectionLogic)const { loadRules, saveRulesOrder, setRuleEnabled } = useActions(logsSamplingSectionLogic)const [localRules, setLocalRules] = useState<LogsSamplingRuleApi[]>(rules)@@ -200,6 +234,21 @@ export function LogsSamplingRulesSortableTable(): JSX.Element {<th className="py-2 px-2 text-left text-xs font-semibold text-muted-alt uppercase tracking-wider min-w-0">Type</th>+ <th className="py-2 px-2 text-left text-xs font-semibold text-muted-alt uppercase tracking-wider whitespace-nowrap">+ <span className="inline-flex items-center gap-1">+ Impact (24h)+ <Tooltip+ title={+ <>+ Log lines dropped by this rule in ingestion metrics. Rules scoped to+ a service only affect that service; others count across the project.+ </>+ }+ >+ <IconInfo className="text-muted-alt text-base shrink-0" />+ </Tooltip>+ </span>+ </th><th className="py-2 px-2 w-32 text-left text-xs font-semibold text-muted-alt uppercase tracking-wider">Enabled</th>@@ -208,7 +257,7 @@ export function LogsSamplingRulesSortableTable(): JSX.Element {<tbody>{localRules.length === 0 ? (<tr>- <td colSpan={5} className="text-center py-8 text-muted">+ <td colSpan={6} className="text-center py-8 text-muted">No drop rules yet</td></tr>@@ -225,6 +274,8 @@ export function LogsSamplingRulesSortableTable(): JSX.Element {disabledReason={dragDisabledReason}ruleEnabledTogglePendingId={ruleEnabledTogglePendingId}saveRulesOrderPending={saveRulesOrderPending}+ ruleDropImpactCellState={ruleDropImpactCellState}+ dropped24h={ruleDropImpact[row.id] ?? 0}onSetRuleEnabled={setRuleEnabled}/>))}diff --git a/products/logs/frontend/components/LogsSampling/logsSamplingSectionLogic.ts b/products/logs/frontend/components/LogsSampling/logsSamplingSectionLogic.tsindex ec8f37a56ba8..d17e5aab8b87 100644--- a/products/logs/frontend/components/LogsSampling/logsSamplingSectionLogic.ts+++ b/products/logs/frontend/components/LogsSampling/logsSamplingSectionLogic.ts@@ -1,4 +1,4 @@-import { actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'+import { actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'import { loaders } from 'kea-loaders'import { lemonToast } from '@posthog/lemon-ui'@@ -13,6 +13,7 @@ import {import { LogsSamplingRuleApi } from 'products/logs/frontend/generated/api.schemas'import type { logsSamplingSectionLogicType } from './logsSamplingSectionLogicType'+import { fetchSamplingRuleDropTotalsLast24h } from './samplingRuleDropImpact'export const logsSamplingSectionLogic = kea<logsSamplingSectionLogicType>([path(['products', 'logs', 'frontend', 'components', 'LogsSampling', 'logsSamplingSectionLogic']),@@ -56,9 +57,59 @@ export const logsSamplingSectionLogic = kea<logsSamplingSectionLogicType>([},},],+ ruleDropImpact: [+ {} as Record<string, number>,+ {+ loadRuleDropImpact: async (_, breakpoint) => {+ const rules = values.rules+ const ids = rules.map((r) => r.id)+ if (ids.length === 0) {+ return {}+ }+ await breakpoint(1)+ return await fetchSamplingRuleDropTotalsLast24h(ids)+ },+ },+ ],})),+ reducers({+ ruleDropImpactStatus: [+ 'idle' as 'idle' | 'loading' | 'ok' | 'error',+ {+ loadRuleDropImpact: () => 'loading',+ loadRuleDropImpactSuccess: () => 'ok',+ loadRuleDropImpactFailure: () => 'error',+ },+ ],+ }),++ selectors({+ ruleDropImpactCellState: [+ (s) => [s.ruleDropImpactStatus, s.ruleDropImpactLoading, s.rules],+ (+ status: 'idle' | 'loading' | 'ok' | 'error',+ loading: boolean,+ rules: LogsSamplingRuleApi[]+ ): 'loading' | 'ok' | 'error' | 'unknown' => {+ if (loading || (status === 'idle' && rules.length > 0)) {+ return 'loading'+ }+ if (status === 'error') {+ return 'error'+ }+ if (status === 'ok') {+ return 'ok'+ }+ return 'unknown'+ },+ ],+ }),+listeners(({ actions, values }) => ({+ loadRulesSuccess: () => {+ actions.loadRuleDropImpact(undefined)+ },saveRulesOrder: async ({ orderedIds }) => {const projectId = values.currentTeamIdif (projectId === null) {diff --git a/products/logs/frontend/components/LogsSampling/samplingRuleDropImpact.ts b/products/logs/frontend/components/LogsSampling/samplingRuleDropImpact.tsnew file mode 100644index 000000000000..88ba4d73373c--- /dev/null+++ b/products/logs/frontend/components/LogsSampling/samplingRuleDropImpact.ts@@ -0,0 +1,35 @@+import api from 'lib/api'++import { type HogQLQueryString, hogql } from '~/queries/utils'++/** Ingestion writes per-rule drops to app_metrics2; HogQL exposes it as `app_metrics`. */+export async function fetchSamplingRuleDropTotalsLast24h(ruleIds: string[]): Promise<Record<string, number>> {+ if (ruleIds.length === 0) {+ return {}+ }+ const query = hogql`+ SELECT instance_id, sum(count) AS dropped+ FROM app_metrics+ WHERE app_source = ${'logs'}+ AND metric_name = ${'sampling_records_dropped_by_rule'}+ AND instance_id IN ${ruleIds}+ AND timestamp >= now() - INTERVAL 24 HOUR+ GROUP BY instance_id+ ` as HogQLQueryString++ const response = await api.queryHogQL(query, {+ scene: 'logs_sampling',+ productKey: 'logs',+ name: 'sampling_rule_drop_totals_24h',+ })++ const out: Record<string, number> = {}+ for (const row of response.results ?? []) {+ const instanceId = String(row[0] ?? '')+ if (!instanceId) {+ continue+ }+ out[instanceId] = Number(row[1] ?? 0)+ }+ return out+}diff --git a/products/logs/frontend/scenes/LogsSamplingDetailScene/LogsSamplingDetailScene.tsx b/products/logs/frontend/scenes/LogsSamplingDetailScene/LogsSamplingDetailScene.tsxindex 7439f1a88ed6..71c521d15d1a 100644--- a/products/logs/frontend/scenes/LogsSamplingDetailScene/LogsSamplingDetailScene.tsx+++ b/products/logs/frontend/scenes/LogsSamplingDetailScene/LogsSamplingDetailScene.tsx@@ -1,8 +1,11 @@import { BindLogic, useActions, useValues } from 'kea'import { Form } from 'kea-forms'+import { IconInfo } from '@posthog/icons'import { LemonButton, LemonDialog } from '@posthog/lemon-ui'+import { Tooltip } from 'lib/lemon-ui/Tooltip/Tooltip'+import { compactNumber } from 'lib/utils'import { SceneExport } from 'scenes/sceneTypes'import { SceneContent } from '~/layout/scenes/components/SceneContent'@@ -53,6 +56,7 @@ function LogsSamplingDetailFormBody({ rule }: { rule: LogsSamplingRuleApi }): JSconst { deleteRule } = useActions(logsSamplingDetailSceneLogic)const { setSamplingFormValue } = useActions(logsSamplingFormLogic(formProps))const { samplingForm, isSamplingFormSubmitting } = useValues(logsSamplingFormLogic(formProps))+ const { ruleDropImpact24h, ruleDropImpact24hLoading } = useValues(logsSamplingDetailSceneLogic)const confirmDelete = (): void => {LemonDialog.open({@@ -82,6 +86,31 @@ function LogsSamplingDetailFormBody({ rule }: { rule: LogsSamplingRuleApi }): JS</LemonButton>}/>+ <div className="px-4 pt-2 pb-0 text-secondary text-sm flex items-center gap-1.5">+ {ruleDropImpact24hLoading ? (+ <span className="text-muted">Loading drop impact…</span>+ ) : ruleDropImpact24h === null ? (+ <span className="text-muted" title="Drop impact for the last 24 hours is not available">+ —+ </span>+ ) : (+ <>+ <span>+ ~{compactNumber(ruleDropImpact24h)} log lines dropped in the last 24 hours (ingestion).+ </span>+ <Tooltip+ title={+ <>+ From app metrics keyed by this rule. Service-scoped rules only affect that service;+ others are counted across the project.+ </>+ }+ >+ <IconInfo className="text-muted-alt text-base shrink-0" />+ </Tooltip>+ </>+ )}+ </div><div className="flex flex-col gap-6 p-4"><Form logic={logsSamplingFormLogic} props={formProps} formKey="samplingForm" enableFormOnSubmit><LogsSamplingForm />diff --git a/products/logs/frontend/scenes/LogsSamplingDetailScene/logsSamplingDetailSceneLogic.ts b/products/logs/frontend/scenes/LogsSamplingDetailScene/logsSamplingDetailSceneLogic.tsindex 2daf7c5cb0e5..20806163446b 100644--- a/products/logs/frontend/scenes/LogsSamplingDetailScene/logsSamplingDetailSceneLogic.ts+++ b/products/logs/frontend/scenes/LogsSamplingDetailScene/logsSamplingDetailSceneLogic.ts@@ -13,6 +13,7 @@ import {buildSamplingFormDefaults,logsSamplingFormLogic,} from 'products/logs/frontend/components/LogsSampling/logsSamplingFormLogic'+import { fetchSamplingRuleDropTotalsLast24h } from 'products/logs/frontend/components/LogsSampling/samplingRuleDropImpact'import { logsSamplingRulesDestroy, logsSamplingRulesRetrieve } from 'products/logs/frontend/generated/api'import { LogsSamplingRuleApi } from 'products/logs/frontend/generated/api.schemas'import { logsDropRulesSettingsUrl } from 'products/logs/frontend/logsDropRulesSettingsUrl'@@ -52,6 +53,16 @@ export const logsSamplingDetailSceneLogic = kea<logsSamplingDetailSceneLogicTypeloadRule: async () => logsSamplingRulesRetrieve(String(values.currentTeamId), props.id),},],+ ruleDropImpact24h: [+ null as number | null,+ {+ loadRuleDropImpact24h: async (_, breakpoint) => {+ await breakpoint(1)+ const map = await fetchSamplingRuleDropTotalsLast24h([props.id])+ return map[props.id] ?? 0+ },+ },+ ],})),selectors({@@ -74,6 +85,7 @@ export const logsSamplingDetailSceneLogic = kea<logsSamplingDetailSceneLogicTypeif (values.rule) {actions.resetSamplingForm(buildSamplingFormDefaults(values.rule))}+ actions.loadRuleDropImpact24h(undefined)},deleteRule: async () => {try {