Repository navigation
feat(app): SLO builder for a new SLO #3803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import SloEditorPage from '@/components/Slos/SloEditorPage'; | ||
|
|
||
| export default SloEditorPage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { useEffect, useRef } from 'react'; | ||
| import { | ||
| type Control, | ||
| type UseFormGetValues, | ||
| type UseFormSetValue, | ||
| useFormState, | ||
| useWatch, | ||
| } from 'react-hook-form'; | ||
| import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; | ||
| import { buildSloAvailabilityCondition } from '@hyperdx/common-utils/dist/core/slo'; | ||
| import { SourceKind, type TSource } from '@hyperdx/common-utils/dist/types'; | ||
| import { Input } from '@mantine/core'; | ||
|
|
||
| import SearchWhereInput from '@/components/SearchInput/SearchWhereInput'; | ||
| import { SourceSelectControlled } from '@/components/SourceSelect'; | ||
| import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; | ||
|
|
||
| import type { SloFormValues } from './sloForm'; | ||
| import { SloFormRow } from './SloFormLayout'; | ||
|
|
||
| const SLO_SOURCE_KINDS = [SourceKind.Trace, SourceKind.Log]; | ||
|
|
||
| /** Which events count, and which of them are good. */ | ||
| export function SloIndicatorFields({ | ||
| control, | ||
| getValues, | ||
| setValue, | ||
| source, | ||
| }: { | ||
| control: Control<SloFormValues>; | ||
| getValues: UseFormGetValues<SloFormValues>; | ||
| setValue: UseFormSetValue<SloFormValues>; | ||
| source?: TSource; | ||
| }) { | ||
| const sourceId = useWatch({ control, name: 'source' }); | ||
| const { errors } = useFormState({ control, name: 'indicator.good' }); | ||
| const tableConnection = tcFromSource(source); | ||
|
|
||
| // The availability condition starts from the source's status or severity | ||
| // column. Switching sources replaces a seeded condition, never one the | ||
| // user wrote. | ||
| const seededGoodRef = useRef<string | undefined>(undefined); | ||
| useEffect(() => { | ||
| if (!source) return; | ||
| const current = getValues('indicator.good').trim(); | ||
| const condition = buildSloAvailabilityCondition(source); | ||
| if (current === '' || current === seededGoodRef.current) { | ||
| setValue('indicator.good', condition ?? '', { | ||
| shouldValidate: condition != null, | ||
| }); | ||
| } | ||
| seededGoodRef.current = condition; | ||
| }, [source, getValues, setValue]); | ||
|
|
||
| return ( | ||
| <> | ||
| <SloFormRow label="Source"> | ||
| {/* The select shows its own error. */} | ||
| <div data-testid="slo-source"> | ||
| <SourceSelectControlled | ||
| control={control} | ||
| name="source" | ||
| allowedSourceKinds={SLO_SOURCE_KINDS} | ||
| size="sm" | ||
| /> | ||
| </div> | ||
| </SloFormRow> | ||
| <SloFormRow | ||
| label="Eligible events" | ||
| helpText="The events the SLI counts. Leave empty to count every event in the source." | ||
| > | ||
| <SearchWhereInput | ||
| tableConnection={tableConnection} | ||
| control={control} | ||
| name="indicator.eligible.where" | ||
| sourceId={sourceId} | ||
| size="sm" | ||
| sqlPlaceholder="For example ServiceName = 'checkout'" | ||
| lucenePlaceholder="For example ServiceName:checkout" | ||
| data-testid="slo-eligible-input" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 P2 —
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| /> | ||
| </SloFormRow> | ||
| <SloFormRow | ||
| label="Good events" | ||
| helpText="The SQL condition a good event matches. An event where it evaluates to NULL counts as bad." | ||
| > | ||
| <SQLInlineEditorControlled | ||
| tableConnection={tableConnection} | ||
| sourceId={sourceId} | ||
| control={control} | ||
| name="indicator.good" | ||
| placeholder="For example StatusCode != 'Error'" | ||
| size="sm" | ||
| /> | ||
| <Input.Error mt={4}>{errors.indicator?.good?.message}</Input.Error> | ||
| </SloFormRow> | ||
| </> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import dynamic from 'next/dynamic'; | ||
| import Head from 'next/head'; | ||
| import Link from 'next/link'; | ||
| import { useRouter } from 'next/router'; | ||
| import { useForm, useFormState, useWatch } from 'react-hook-form'; | ||
| import { | ||
| Anchor, | ||
| Box, | ||
| Breadcrumbs, | ||
| Button, | ||
| Container, | ||
| Flex, | ||
| Grid, | ||
| Group, | ||
| Stack, | ||
| Text, | ||
| } from '@mantine/core'; | ||
| import { notifications } from '@mantine/notifications'; | ||
|
|
||
| import { usePageTitle } from '@/brand'; | ||
| import { InlineNameInputControlled } from '@/components/InlineNameInput/InlineNameInput'; | ||
| import { PageHeader } from '@/components/PageHeader'; | ||
| import { withAppNavForSurface } from '@/layout'; | ||
| import { useSaveSlo, useSlosEnabledGuard } from '@/slo'; | ||
| import { useSource } from '@/source'; | ||
| import { getApiErrorMessage } from '@/utils/apiErrors'; | ||
|
|
||
| import { SloIndicatorFields } from './SloDefinitionFields'; | ||
| import { | ||
| DEFAULT_SLO_FORM_VALUES, | ||
| sloFormResolver, | ||
| type SloFormValues, | ||
| toSloInput, | ||
| } from './sloForm'; | ||
| import { SloFormSection } from './SloFormLayout'; | ||
|
|
||
| function SloEditorPage() { | ||
| const enabled = useSlosEnabledGuard(); | ||
| const router = useRouter(); | ||
| const title = usePageTitle('New SLO'); | ||
|
|
||
| const { control, handleSubmit, setValue, getValues } = useForm<SloFormValues>( | ||
| { | ||
| defaultValues: DEFAULT_SLO_FORM_VALUES, | ||
| resolver: sloFormResolver, | ||
| }, | ||
| ); | ||
| const { errors } = useFormState({ control, name: 'name' }); | ||
| const sourceId = useWatch({ control, name: 'source' }); | ||
| const { data: source } = useSource({ id: sourceId || undefined }); | ||
|
|
||
| const save = useSaveSlo(); | ||
| const onSave = handleSubmit(async values => { | ||
| try { | ||
| const saved = await save.mutateAsync({ | ||
| slo: toSloInput(values, source), | ||
| }); | ||
| notifications.show({ color: 'green', message: `Saved ${saved.name}` }); | ||
| router.push('/slos'); | ||
| } catch (e) { | ||
| notifications.show({ | ||
| color: 'red', | ||
| title: 'Could not save the SLO', | ||
| message: await getApiErrorMessage(e, 'Something went wrong'), | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| if (!enabled) return null; | ||
|
|
||
| return ( | ||
| <div data-testid="slo-editor-page"> | ||
| <Head> | ||
| <title>{title}</title> | ||
| </Head> | ||
| <PageHeader | ||
| breadcrumbs={ | ||
| <Breadcrumbs fz="sm"> | ||
| <Anchor component={Link} href="/slos" fz="sm" c="dimmed"> | ||
| SLOs | ||
| </Anchor> | ||
| </Breadcrumbs> | ||
| } | ||
| > | ||
| {/* The page title, like a dashboard's or a saved search's name. */} | ||
| <Flex justify="space-between" align="flex-start" gap="sm"> | ||
| {/* Takes the free width, which the name grows into. */} | ||
| <Box flex={1} miw={0}> | ||
| <InlineNameInputControlled | ||
| control={control} | ||
| name="name" | ||
| size="md" | ||
| headingLevel={3} | ||
| placeholder="Untitled SLO" | ||
| aria-label="SLO name" | ||
| data-testid="slo-name-input" | ||
| /> | ||
| {errors.name?.message && ( | ||
| <Text size="xs" variant="danger" data-testid="slo-name-error"> | ||
| {errors.name.message} | ||
| </Text> | ||
| )} | ||
| </Box> | ||
| <Group gap="xs" wrap="nowrap"> | ||
| <Button component={Link} href="/slos" variant="secondary"> | ||
| Cancel | ||
| </Button> | ||
| <Button | ||
| variant="primary" | ||
| onClick={onSave} | ||
| loading={save.isPending} | ||
| data-testid="slo-save" | ||
| > | ||
| Create SLO | ||
| </Button> | ||
| </Group> | ||
| </Flex> | ||
| </PageHeader> | ||
| {/* Keeps the form's controls under the sticky header when scrolled. */} | ||
| <Container size="xl" py="md" style={{ isolation: 'isolate' }}> | ||
| <Grid> | ||
| <Grid.Col span={{ base: 12, lg: 8 }}> | ||
| <Stack gap="md"> | ||
| <SloFormSection | ||
| title="Indicator" | ||
| description="Which events count, and which of them are good. Good divided by eligible is the SLI." | ||
| > | ||
| <SloIndicatorFields | ||
| control={control} | ||
| getValues={getValues} | ||
| setValue={setValue} | ||
| source={source} | ||
| /> | ||
| </SloFormSection> | ||
| </Stack> | ||
| </Grid.Col> | ||
| </Grid> | ||
| </Container> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Client only, like Search and the chart explorer: the SQL editors and the | ||
| // themed charts render differently on the server. | ||
| const SloEditorPageDynamic = dynamic(async () => SloEditorPage, { ssr: false }); | ||
|
|
||
| // @ts-expect-error next/dynamic component type does not include the getLayout static | ||
| SloEditorPageDynamic.getLayout = withAppNavForSurface('slo'); | ||
|
|
||
| export default SloEditorPageDynamic; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { Flex, Paper, Stack, Text } from '@mantine/core'; | ||
|
|
||
| import { EDITOR_INPUT_HEIGHTS } from '@/components/editorInputHeights'; | ||
| import { FormRow } from '@/components/Sources/SourceForm/FormRow'; | ||
|
|
||
| /** A titled group of rows in the SLO builder. */ | ||
| export function SloFormSection({ | ||
| id, | ||
| title, | ||
| description, | ||
| children, | ||
| }: { | ||
| id?: string; | ||
| title: string; | ||
| description?: string; | ||
| children: React.ReactNode; | ||
| }) { | ||
| return ( | ||
| <Paper withBorder p="md" radius="sm" id={id}> | ||
| <Text fw={500}>{title}</Text> | ||
| {description && ( | ||
| <Text size="xs" c="dimmed"> | ||
| {description} | ||
| </Text> | ||
| )} | ||
| <Stack gap="md" mt="md"> | ||
| {children} | ||
| </Stack> | ||
| </Paper> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A row of the SLO builder: the Source form's label column, with the label | ||
| * kept in sentence case. `inline` centers a control shorter than an input, | ||
| * such as a radio group or a switch, on the label. | ||
| */ | ||
| export function SloFormRow({ | ||
| label, | ||
| helpText, | ||
| inline, | ||
| children, | ||
| }: { | ||
| label: string; | ||
| helpText?: string; | ||
| inline?: boolean; | ||
| children: React.ReactNode; | ||
| }) { | ||
| return ( | ||
| <FormRow label={<Text size="sm">{label}</Text>} helpText={helpText}> | ||
| {inline ? ( | ||
| <Flex mih={EDITOR_INPUT_HEIGHTS.sm} align="center"> | ||
| {children} | ||
| </Flex> | ||
| ) : ( | ||
| children | ||
| )} | ||
| </FormRow> | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 P2 — The effect that seeds and replaces the good condition has no test
The effect fills in the condition from the source and replaces it on a source switch only if the user hasn't edited it. That is the builder's main new logic, but
sloForm.test.tsonly covers the resolver andtoSloInput. Add a component test: pick source A, check the seeded condition, switch to source B and check it is replaced; then edit the condition, switch again and check the edit is kept.Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond