-
Notifications
You must be signed in to change notification settings - Fork 407
feat: add pattern column selector for event pattern matching on any column #2460
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
base: main
Are you sure you want to change the base?
Changes from all commits
aee2ab9
990d9a9
304df27
b20d0b4
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,5 @@ | ||
| --- | ||
| '@hyperdx/app': patch | ||
| --- | ||
|
|
||
| feat: Allow selecting the column or SQL expression used for event pattern grouping (with shareable URL state) |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||
| import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; | ||||||||||||||
| import { Box } from '@mantine/core'; | ||||||||||||||
|
|
||||||||||||||
| import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; | ||||||||||||||
| import { useSource } from '@/source'; | ||||||||||||||
|
|
||||||||||||||
| export function buildPatternColumnExpression({ | ||||||||||||||
| patternColumn, | ||||||||||||||
| fallback, | ||||||||||||||
| }: { | ||||||||||||||
| patternColumn: string | null | undefined; | ||||||||||||||
| fallback: string; | ||||||||||||||
| }): string { | ||||||||||||||
| if (!patternColumn) return fallback; | ||||||||||||||
| return `toString(${patternColumn})`; | ||||||||||||||
|
Comment on lines
+14
to
+15
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.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function PatternColumnSelector({ | ||||||||||||||
| sourceId, | ||||||||||||||
| value, | ||||||||||||||
| onChange, | ||||||||||||||
| onSubmit, | ||||||||||||||
| dateRange, | ||||||||||||||
| }: { | ||||||||||||||
| sourceId: string | undefined; | ||||||||||||||
| value: string; | ||||||||||||||
| onChange?: (value: string) => void; | ||||||||||||||
| onSubmit?: () => void; | ||||||||||||||
| dateRange?: [Date, Date]; | ||||||||||||||
| }) { | ||||||||||||||
| const { data: source } = useSource({ id: sourceId }); | ||||||||||||||
| const tableConnection = tcFromSource(source); | ||||||||||||||
|
|
||||||||||||||
| if (!onChange) return null; | ||||||||||||||
|
|
||||||||||||||
| return ( | ||||||||||||||
| <Box py="xs" maw={600}> | ||||||||||||||
| <SQLInlineEditor | ||||||||||||||
| tableConnection={tableConnection} | ||||||||||||||
| value={value} | ||||||||||||||
| onChange={onChange} | ||||||||||||||
| onSubmit={onSubmit} | ||||||||||||||
| enableHotkey | ||||||||||||||
| label="Pattern Expression" | ||||||||||||||
| placeholder="Default (body) — column name or expression" | ||||||||||||||
| size="xs" | ||||||||||||||
| allowMultiline={false} | ||||||||||||||
| sourceId={sourceId} | ||||||||||||||
| dateRange={dateRange} | ||||||||||||||
| /> | ||||||||||||||
| </Box> | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { buildPatternColumnExpression } from '../PatternColumnSelector'; | ||
|
|
||
| describe('buildPatternColumnExpression', () => { | ||
| const fallback = 'Body'; | ||
|
|
||
| it('returns the fallback when no expression is provided', () => { | ||
| expect( | ||
| buildPatternColumnExpression({ patternColumn: null, fallback }), | ||
| ).toBe(fallback); | ||
| expect( | ||
| buildPatternColumnExpression({ patternColumn: undefined, fallback }), | ||
| ).toBe(fallback); | ||
| expect(buildPatternColumnExpression({ patternColumn: '', fallback })).toBe( | ||
| fallback, | ||
| ); | ||
| }); | ||
|
|
||
| it('wraps a plain column reference in toString()', () => { | ||
| expect( | ||
| buildPatternColumnExpression({ | ||
| patternColumn: 'ResourceAttributes', | ||
| fallback, | ||
| }), | ||
| ).toBe('toString(ResourceAttributes)'); | ||
| }); | ||
|
|
||
| it('wraps an arbitrary SQL expression in toString()', () => { | ||
| expect( | ||
| buildPatternColumnExpression({ | ||
| patternColumn: "concatWithSeparator(' ', Body, LogAttributes)", | ||
| fallback, | ||
| }), | ||
| ).toBe("toString(concatWithSeparator(' ', Body, LogAttributes))"); | ||
|
|
||
| expect( | ||
| buildPatternColumnExpression({ | ||
| patternColumn: "JSONExtractString(Body, 'message')", | ||
| fallback, | ||
| }), | ||
| ).toBe("toString(JSONExtractString(Body, 'message'))"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { reconstructTemplate } from '../reconstructTemplate'; | ||
|
|
||
| describe('reconstructTemplate', () => { | ||
| it('returns the original log when the template is empty', () => { | ||
| expect(reconstructTemplate('hello world', '')).toBe('hello world'); | ||
| }); | ||
|
|
||
| it('restores JSON separators around stable and variable tokens', () => { | ||
| expect( | ||
| reconstructTemplate( | ||
| `{"hostname":"foo","pid":12345,"time":1700000000}`, | ||
| 'hostname foo pid <*> time <*>', | ||
| ), | ||
| ).toBe(`{"hostname":"foo","pid":<*>,"time":<*>}`); | ||
| }); | ||
|
|
||
| it('restores ClickHouse Map (single-quoted) separators', () => { | ||
| expect( | ||
| reconstructTemplate( | ||
| `{'hostname':'Aarons-MacBook-Pro.local','pid':12345,'time':1700000000}`, | ||
| 'hostname Aarons MacBook Pro local pid <*> time <*>', | ||
| ), | ||
| ).toBe(`{'hostname':'Aarons-MacBook-Pro.local','pid':<*>,'time':<*>}`); | ||
| }); | ||
|
|
||
| it('restores key=value separators', () => { | ||
| expect( | ||
| reconstructTemplate( | ||
| 'level=info msg=hello user_id=42', | ||
| 'level info msg hello user id <*>', | ||
| ), | ||
| ).toBe('level=info msg=hello user_id=<*>'); | ||
| }); | ||
|
|
||
| it('keeps the original token when the template runs short', () => { | ||
| expect(reconstructTemplate('alpha beta gamma delta', 'alpha beta')).toBe( | ||
| 'alpha beta gamma delta', | ||
| ); | ||
| }); | ||
|
|
||
| it('preserves leading and trailing separators', () => { | ||
| expect(reconstructTemplate('[INFO] hello world', 'INFO hello world')).toBe( | ||
| '[INFO] hello world', | ||
| ); | ||
| }); | ||
|
|
||
| it('collapses newlines and tabs in the original log to single spaces', () => { | ||
| expect( | ||
| reconstructTemplate( | ||
| 'Error:\n message: "failed"\n code: 500', | ||
| 'Error message failed code <*>', | ||
| ), | ||
| ).toBe('Error: message: "failed" code: <*>'); | ||
|
|
||
| expect(reconstructTemplate('foo\n\n\nbar', 'foo bar')).toBe('foo bar'); | ||
|
|
||
| expect(reconstructTemplate('foo\tbar', 'foo bar')).toBe('foo bar'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export function reconstructTemplate( | ||
| originalLog: string, | ||
| templateMined: string, | ||
| ): string { | ||
| const normalized = originalLog.replace(/\s+/g, ' '); | ||
| const tokens = templateMined.split(' ').filter(t => t.length > 0); | ||
| if (tokens.length === 0) return normalized; | ||
|
|
||
| const tokenOrSeparator = /([A-Za-z0-9]+)|([^A-Za-z0-9]+)/g; | ||
| let result = ''; | ||
| let tokenIdx = 0; | ||
| let match: RegExpExecArray | null; | ||
| while ((match = tokenOrSeparator.exec(normalized)) !== null) { | ||
|
Comment on lines
+11
to
+13
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.
|
||
| if (match[1] !== undefined) { | ||
| result += tokens[tokenIdx] ?? match[1]; | ||
| tokenIdx++; | ||
| } else { | ||
| result += match[2]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.