diff --git a/packages/app/.env.development b/packages/app/.env.development index af4a79ce0..0847ed16d 100644 --- a/packages/app/.env.development +++ b/packages/app/.env.development @@ -14,3 +14,4 @@ NEXT_PUBLIC_OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:${HDX_DEV_OTEL_HTTP_PO # NEXT_PUBLIC_OTEL_EXPORTER_OTLP_LOGS_ENDPOINT= NEXT_PUBLIC_ENABLE_PROMQL=true NEXT_PUBLIC_ENABLE_ALERT_DETAILS=true +NEXT_PUBLIC_ENABLE_SLOS=true diff --git a/packages/app/pages/slos/index.tsx b/packages/app/pages/slos/index.tsx new file mode 100644 index 000000000..703744aba --- /dev/null +++ b/packages/app/pages/slos/index.tsx @@ -0,0 +1,3 @@ +import SlosListPage from '@/components/Slos/SlosListPage'; + +export default SlosListPage; diff --git a/packages/app/src/components/AppNav/AppNav.tsx b/packages/app/src/components/AppNav/AppNav.tsx index ad2746c8e..a3a07b407 100644 --- a/packages/app/src/components/AppNav/AppNav.tsx +++ b/packages/app/src/components/AppNav/AppNav.tsx @@ -17,6 +17,7 @@ import { IconSettings, IconSitemap, IconTable, + IconTargetArrow, } from '@tabler/icons-react'; import api from '@/api'; @@ -25,7 +26,7 @@ import { AlertStatusIcon } from '@/components/AlertStatusIcon'; import { InstanceLabel } from '@/components/AppNav/InstanceLabel'; import { useMobileNavOverlay } from '@/components/AppNav/useMobileNavOverlay'; import { useCompactViewport } from '@/components/CompactViewport/useCompactViewport'; -import { APP_VERSION, IS_LOCAL_MODE } from '@/config'; +import { APP_VERSION, IS_LOCAL_MODE, IS_SLOS_ENABLED } from '@/config'; import { Dashboard, useDashboards } from '@/dashboard'; import { useFavorites } from '@/favorites'; import { setHdxIdentity } from '@/hdxDebug'; @@ -92,6 +93,18 @@ const NAV_LINKS: NavLinkConfig[] = [ icon: , cloudOnly: true, }, + ...(IS_SLOS_ENABLED + ? [ + { + id: 'slos', + label: 'SLOs', + href: '/slos', + icon: , + cloudOnly: true, + isBeta: true, + }, + ] + : []), { id: 'sessions', label: 'Client sessions', diff --git a/packages/app/src/components/Slos/SloStateBadge.tsx b/packages/app/src/components/Slos/SloStateBadge.tsx new file mode 100644 index 000000000..9acff4e63 --- /dev/null +++ b/packages/app/src/components/Slos/SloStateBadge.tsx @@ -0,0 +1,32 @@ +import { Badge } from '@clickhouse/click-ui'; +import { Tooltip } from '@mantine/core'; + +import { + type SloDisplayState, + sloStateDescription, + sloStateLabel, +} from './sloUtils'; + +export function SloStateBadge({ state }: { state: SloDisplayState }) { + const label = sloStateLabel(state); + const badge = (() => { + switch (state) { + case 'breached': + return ; + case 'burning': + return ; + case 'error': + return ; + case 'healthy': + return ; + case 'no_data': + case 'pending': + return ; + } + })(); + return ( + + {badge} + + ); +} diff --git a/packages/app/src/components/Slos/SloTable.tsx b/packages/app/src/components/Slos/SloTable.tsx new file mode 100644 index 000000000..58610dac8 --- /dev/null +++ b/packages/app/src/components/Slos/SloTable.tsx @@ -0,0 +1,158 @@ +import { formatDistanceToNowStrict } from 'date-fns'; +import { Badge } from '@clickhouse/click-ui'; +import type { + SloApiResponse, + SloBurnStats, +} from '@hyperdx/common-utils/dist/slo'; +import { Group, Stack, Table, Text, Tooltip } from '@mantine/core'; + +import { useSources } from '@/source'; + +import { SloStateBadge } from './SloStateBadge'; +import { + budgetColorVar, + formatBudget, + formatBurnRate, + formatSli, + formatTarget, + getSloDisplayState, +} from './sloUtils'; + +function Header({ label, help }: { label: string; help: string }) { + return ( + + + {label} + + + ); +} + +function BudgetCell({ budgetRemaining }: { budgetRemaining?: number | null }) { + const overspent = budgetRemaining != null && budgetRemaining <= 0; + return ( + + + + {overspent ? 'Overspent' : formatBudget(budgetRemaining)} + + + + ); +} + +function BurnRateCell({ burn }: { burn?: SloBurnStats[] }) { + return ( + + + {formatBurnRate(burn?.[0]?.burnRate)} + + {burn?.[0] ? ` / ${burn[0].window}` : ''} + + + + ); +} + +function SloRow({ + slo, + sourceName, +}: { + slo: SloApiResponse; + sourceName?: string; +}) { + const { status } = slo; + return ( + + + + + {slo.name} + + + + {sourceName ?? 'Unknown source'} + + {slo.tags.map(tag => ( + + ))} + + + + + + + + + + + + {formatSli(status?.sli)} + + + + + + {formatTarget(slo.target)} + + {` / ${slo.window}`} + + + + + ); +} + +export function SloTable({ slos }: { slos: SloApiResponse[] }) { + const { data: sources } = useSources(); + + return ( + + + + + Name + State +
+
+
+ Target + + + + {slos.map(slo => ( + s.id === slo.source)?.name} + /> + ))} + +
+
+ ); +} diff --git a/packages/app/src/components/Slos/SlosListPage.tsx b/packages/app/src/components/Slos/SlosListPage.tsx new file mode 100644 index 000000000..a0e2e3e7b --- /dev/null +++ b/packages/app/src/components/Slos/SlosListPage.tsx @@ -0,0 +1,131 @@ +import { useMemo } from 'react'; +import Head from 'next/head'; +import { parseAsStringEnum, useQueryState } from 'nuqs'; +import { compareSlosWorstFirst } from '@hyperdx/common-utils/dist/core/sloStats'; +import { + Alert, + Button, + Container, + Flex, + Group, + Skeleton, + Text, +} from '@mantine/core'; +import { IconTargetArrow } from '@tabler/icons-react'; + +import { usePageTitle } from '@/brand'; +import EmptyState from '@/components/EmptyState'; +import { PageHeader } from '@/components/PageHeader'; +import { withAppNavForSurface } from '@/layout'; +import { useSlos, useSlosEnabledGuard } from '@/slo'; + +import { SloTable } from './SloTable'; +import { + getSloDisplayState, + SLO_DISPLAY_STATES, + type SloDisplayState, + sloStateLabel, +} from './sloUtils'; + +export default function SlosListPage() { + const enabled = useSlosEnabledGuard(); + const title = usePageTitle('SLOs'); + const { data: slos, isPending, isError, refetch } = useSlos(); + + const [stateFilter, setStateFilter] = useQueryState( + 'state', + parseAsStringEnum(SLO_DISPLAY_STATES), + ); + + // Worst first, so what needs attention is on top. + const filtered = useMemo( + () => + (slos ?? []) + .filter(slo => !stateFilter || getSloDisplayState(slo) === stateFilter) + .sort(compareSlosWorstFirst), + [slos, stateFilter], + ); + + const counts = useMemo(() => { + const result = new Map(); + for (const slo of slos ?? []) { + const state = getSloDisplayState(slo); + result.set(state, (result.get(state) ?? 0) + 1); + } + return result; + }, [slos]); + + if (!enabled) return null; + const hasNone = !isPending && !isError && (slos ?? []).length === 0; + + return ( +
+ + {title} + + +
+ {hasNone ? ( + + } + title="No SLOs yet" + description="Define a target for the share of good events in your logs or traces, and get alerted when the error budget burns too fast." + /> + + ) : ( + + + {/* Counted once loaded, so a failed load never reads as 0. */} + {slos && ( + + {slos.length === 1 ? '1 SLO' : `${slos.length} SLOs`} + + )} + {SLO_DISPLAY_STATES.filter(s => counts.get(s)).map(s => ( + + ))} + + {isPending ? ( + + ) : isError ? ( + + + Something went wrong fetching your SLOs. + + + + ) : filtered.length > 0 ? ( + + ) : ( + } + title="No matching SLOs" + description="Try adjusting your search or filters." + /> + )} + + )} +
+
+ ); +} + +SlosListPage.getLayout = withAppNavForSurface('slo'); diff --git a/packages/app/src/components/Slos/sloUtils.ts b/packages/app/src/components/Slos/sloUtils.ts new file mode 100644 index 000000000..2e6418c00 --- /dev/null +++ b/packages/app/src/components/Slos/sloUtils.ts @@ -0,0 +1,88 @@ +import type { SloApiResponse, SloState } from '@hyperdx/common-utils/dist/slo'; + +/** An SLO's state as shown: pending until the background computes it. */ +export type SloDisplayState = SloState | 'pending'; + +/** The states an SLO can be shown in. */ +export const SLO_DISPLAY_STATES: SloDisplayState[] = [ + 'breached', + 'burning', + 'error', + 'healthy', + 'no_data', + 'pending', +]; + +export function getSloDisplayState(slo: SloApiResponse): SloDisplayState { + return slo.status?.state ?? 'pending'; +} + +export function sloStateLabel(state: SloDisplayState): string { + switch (state) { + case 'healthy': + return 'Healthy'; + case 'burning': + return 'Burning'; + case 'breached': + return 'Breached'; + case 'no_data': + return 'No data'; + case 'error': + return 'Error'; + case 'pending': + return 'Pending'; + } +} + +export function sloStateDescription(state: SloDisplayState): string { + switch (state) { + case 'healthy': + return 'Within budget, and not spending it fast.'; + case 'burning': + return 'Spending the error budget fast: the fast burn or slow burn rule holds.'; + case 'breached': + return 'The error budget for the window is spent.'; + case 'no_data': + return 'No eligible events in the window.'; + case 'error': + return 'The last update failed. The numbers shown are the last ones computed.'; + case 'pending': + return 'The numbers are computed in the background within a few minutes of a save.'; + } +} + +/** SLIs near 100% need more decimals to tell 99.9% from 99.99%. */ +export function formatSli(sli: number | null | undefined): string { + if (sli == null) return '-'; + const pct = sli * 100; + // No bad events at all; more digits would suggest some. + if (sli === 1) return '100%'; + if (pct >= 99.99) return `${pct.toFixed(3)}%`; + if (pct >= 99) return `${pct.toFixed(2)}%`; + return `${pct.toFixed(1)}%`; +} + +export function formatTarget(target: number): string { + return `${Number(target.toFixed(3))}%`; +} + +export function formatBudget(budget: number | null | undefined): string { + if (budget == null) return '-'; + const pct = budget * 100; + return `${Intl.NumberFormat('en-US', { + maximumFractionDigits: Math.abs(pct) < 10 ? 1 : 0, + }).format(pct)}%`; +} + +export function formatBurnRate(burnRate: number | null | undefined): string { + if (burnRate == null) return '-'; + if (burnRate >= 100) return `${Math.round(burnRate)}x`; + return `${burnRate.toFixed(burnRate >= 10 ? 1 : 2)}x`; +} + +export function budgetColorVar(budget: number | null | undefined): string { + if (budget == null) return 'var(--color-text-muted)'; + if (budget <= 0) return 'var(--color-text-danger)'; + if (budget < 0.25) return 'var(--color-text-warning)'; + return 'var(--color-text-success)'; +} diff --git a/packages/app/src/config.ts b/packages/app/src/config.ts index dcedfff10..d557bc6c0 100644 --- a/packages/app/src/config.ts +++ b/packages/app/src/config.ts @@ -96,6 +96,11 @@ export const INSTANCE_TITLE_SUFFIX = INSTANCE_LABEL ? ` ${INSTANCE_LABEL}` : ''; // dev (.env.development) and CI (e2e webserver) while the feature bakes. export const IS_ALERT_DETAILS_ENABLED = env('NEXT_PUBLIC_ENABLE_ALERT_DETAILS') === 'true'; +// SLOs (/slos). Default off while the feature bakes; enabled in dev +// (.env.development). The API gates its routes with ENABLE_SLOS, and local +// mode has no API to evaluate them. +export const IS_SLOS_ENABLED = + env('NEXT_PUBLIC_ENABLE_SLOS') === 'true' && !IS_LOCAL_MODE; // Not exported: IS_IAC_EXPORT_ENABLED below is the only gate callers should // read. Leaving the raw flag importable re-opens the "forgot the local-mode // check" mistake that folding the two together was meant to close. diff --git a/packages/app/src/slo.ts b/packages/app/src/slo.ts new file mode 100644 index 000000000..c381d052e --- /dev/null +++ b/packages/app/src/slo.ts @@ -0,0 +1,33 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; +import { useQuery } from '@tanstack/react-query'; + +import { hdxServer } from './api'; +import { IS_SLOS_ENABLED } from './config'; + +const SLOS_QUERY_KEY = ['slos']; +// Snapshots refresh server-side every few minutes; polling the stored state +// is cheap because it never runs a ClickHouse query. +const SLO_POLL_MS = 60_000; + +/** + * Direct-URL guard while the feature bakes: the nav only links to SLOs when + * the flag is on, but the routes must bounce too. + */ +export function useSlosEnabledGuard(): boolean { + const router = useRouter(); + useEffect(() => { + if (!IS_SLOS_ENABLED) router.replace('/search'); + }, [router]); + return IS_SLOS_ENABLED; +} + +export function useSlos() { + return useQuery({ + queryKey: SLOS_QUERY_KEY, + queryFn: () => hdxServer('slos').json(), + enabled: IS_SLOS_ENABLED, + refetchInterval: SLO_POLL_MS, + }); +}