Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/event-patterns-drain-ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/app': patch
---

perf(app): Event Patterns mines with the TypeScript Drain port instead of
`drain3` in Pyodide, so opening it no longer downloads a 14 MB Python runtime
or freezes the page while mining. Saved pattern filters still apply.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { mineEventPatterns } from '@/components/Patterns/mineEventPatterns';
import { reconstructTemplate } from '@/components/Patterns/reconstructTemplate';

describe('mineEventPatterns', () => {
it('leaves dates unmasked so the displayed pattern lines up with the log', () => {
const logs = [
'2026-11-19T10:00:01Z INFO user 42 logged in from 10.0.0.1',
'2026-11-18T11:22:33Z INFO user 7 logged in from 10.0.0.2',
];
const { patterns } = mineEventPatterns(logs);
expect(reconstructTemplate(logs[1], patterns[1].templateMined)).toBe(
'2026-11-<*>:<*>:<*> INFO user <*> logged in from 10.0.0.<*>',
);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {
buildCompactPatternPredicate,
buildNoisyBodyMatcher,
parsePatternPredicate,
} from '@hyperdx/common-utils/dist/drain';
import type { ConditionFilter, Filter } from '@hyperdx/common-utils/dist/types';

import {
DRAIN_EXTRA_DELIMITERS,
LEGACY_PATTERN_REGEX_OPTIONS,
PATTERN_REGEX_OPTIONS,
} from '@/components/Patterns/drainDelimiters';
import { mineEventPatterns } from '@/components/Patterns/mineEventPatterns';
import {
buildPatternFilter,
isPatternFilter,
Expand Down Expand Up @@ -55,14 +66,30 @@ describe('pattern filters', () => {
expect(
fits(filter, '\x1b[32minfo\x1b[39m: request \x1b[1;31mdone\x1b[0m'),
).toBe(true);
// Separators that JS `\s` has and Python's str.split() lacks.
expect(fits(filter, 'info\ufeffrequest done')).toBe(true);
// Separators that Python's str.split() has and JS `\s` lacks.
expect(fits(filter, 'info\x1erequest\x85done')).toBe(true);
expect(fits(filter, 'info\x1erequest\x85done')).toBe(false);
expect(fits(filter, 'warn: request done')).toBe(false);
expect(
fits(exclude('info request done'), '\x1b[32minfo\x1b[39m request done'),
).toBe(false);
});

it('splits a body where the miner did', () => {
const lines = [
'info\ufeffrequest done',
'info\x1erequest done',
'info\x85request done',
];
const { templates } = mineEventPatterns(lines);
const wholeLine = (line: string) =>
Object.values(templates).filter(t => fits(include(t), line));
expect(wholeLine(lines[0])).toEqual(['info request done']);
expect(wholeLine(lines[1])).toEqual(['info\x1erequest done']);
expect(wholeLine(lines[2])).toEqual(['info\x85request done']);
});

it('only reads back filters it built', () => {
// The same predicate without the ANSI step is not a pattern filter.
const condition = include('a <*>').condition.replace(
Expand Down Expand Up @@ -99,6 +126,35 @@ describe('pattern filters', () => {
).toEqual([facet, include('a <*>')]);
});

it('reads back and replaces an include saved when the miner was drain3', () => {
const { valueExpression } = parsePatternPredicate(
include('a <*>').condition,
PATTERN_REGEX_OPTIONS,
)!;
const saved: ConditionFilter = {
type: 'sql',
condition: buildCompactPatternPredicate(
valueExpression,
'a <*>',
LEGACY_PATTERN_REGEX_OPTIONS,
)!,
};
expect(saved.condition).not.toBe(include('a <*>').condition);
expect(parsePatternFilter(saved)).toMatchObject({
valueExpression: 'Body',
template: 'a <*>',
action: 'include',
});
expect(withPatternFilter([facet, saved], include('b <*>'))).toEqual([
facet,
include('b <*>'),
]);
// The same pattern is rebuilt the way the miner splits today.
expect(withPatternFilter([saved], exclude('a <*>'))).toEqual([
exclude('a <*>'),
]);
});

it('replaces an include on another column too', () => {
const other = buildPatternFilter('Message', 'b <*>', 'include')!;
expect(withPatternFilter([include('a <*>')], other)).toEqual([other]);
Expand All @@ -118,3 +174,33 @@ describe('pattern filters', () => {
]);
});
});

describe('pattern filters and Denoise on mined templates', () => {
const lines = [
'{"level":"info","msg":"order placed","orderId":"a1f3","total":12.50}',
'{"level":"info","msg":"order placed","orderId":"9c2e","total":3.75}',
'2026-11-19T10:00:01Z INFO user 42 logged in from 10.0.0.1',
'2026-11-18T11:22:33Z INFO user 7 logged in from 10.0.0.2',
];
const { patterns, templates } = mineEventPatterns(lines);

it('match every event of the template they were built from', () => {
const misses = lines.filter((line, i) => {
const template = templates[patterns[i].clusterId];
const filter = buildPatternFilter('Body', template, 'include');
return filter == null || !fits(filter, line);
});
expect(misses).toEqual([]);
});

it('let Denoise find every event of a noisy template', () => {
const misses = lines.filter((line, i) => {
const template = templates[patterns[i].clusterId];
const isNoisy = buildNoisyBodyMatcher([template], new Set([template]), {
extraDelimiters: DRAIN_EXTRA_DELIMITERS,
});
return !isNoisy(line);
});
expect(misses).toEqual([]);
});
});
15 changes: 14 additions & 1 deletion packages/app/src/components/Patterns/drainDelimiters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { ASCII_PUNCTUATION } from '@hyperdx/common-utils/dist/drain';
import {
ASCII_PUNCTUATION,
type PatternRegexOptions,
} from '@hyperdx/common-utils/dist/drain';

// Python's string.punctuation, the extra delimiters of the app's miner.
// Anything that splits bodies or templates the way the miner did (the denoise
// matcher, pattern filters) must use the same list.
export const DRAIN_EXTRA_DELIMITERS = ASCII_PUNCTUATION.split('');

export const PATTERN_REGEX_OPTIONS: PatternRegexOptions = {
extraDelimiters: DRAIN_EXTRA_DELIMITERS,
};

// For reading back filters saved under drain3, which split on str.split()
export const LEGACY_PATTERN_REGEX_OPTIONS: PatternRegexOptions = {
extraDelimiters: DRAIN_EXTRA_DELIMITERS,
whitespace: 'python',
};
18 changes: 18 additions & 0 deletions packages/app/src/components/Patterns/mineEventPatterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
TemplateMiner,
TemplateMinerConfig,
} from '@hyperdx/common-utils/dist/drain';

import { DRAIN_EXTRA_DELIMITERS } from '@/components/Patterns/drainDelimiters';

export function mineEventPatterns(logs: string[]) {
const config = new TemplateMinerConfig();
config.drainExtraDelimiters = DRAIN_EXTRA_DELIMITERS;
// Masked dates wouldn't line up word for word with the log
config.maskingInstructions = [];
const miner = new TemplateMiner(config);
const patterns = logs.map(log => miner.addLogMessage(log));
const templates: Record<string, string> = {};
for (const p of patterns) templates[p.clusterId] = p.templateMined;
return { patterns, templates };
}
25 changes: 12 additions & 13 deletions packages/app/src/components/Patterns/patternFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { escapeSqlString } from '@hyperdx/common-utils/dist/core/utils';
import {
buildCompactPatternPredicate,
parsePatternPredicate,
type PatternRegexOptions,
} from '@hyperdx/common-utils/dist/drain';
import type { ConditionFilter, Filter } from '@hyperdx/common-utils/dist/types';

import { DRAIN_EXTRA_DELIMITERS } from '@/components/Patterns/drainDelimiters';
import {
LEGACY_PATTERN_REGEX_OPTIONS,
PATTERN_REGEX_OPTIONS,
} from '@/components/Patterns/drainDelimiters';

export type PatternFilterAction = 'include' | 'exclude';

Expand All @@ -17,12 +19,6 @@ export type PatternFilter = {
action: PatternFilterAction;
};

// The templates come from the app's miner (Python Drain3), so the regex must
// split where it did.
const options: PatternRegexOptions = {
extraDelimiters: DRAIN_EXTRA_DELIMITERS,
whitespace: 'python',
};
const NOT = 'NOT ';

// The miner strips ANSI escape codes before mining (usePatterns), so the
Expand All @@ -44,7 +40,7 @@ export function buildPatternFilter(
const predicate = buildCompactPatternPredicate(
`${STRIP_ANSI_START}${valueExpression}${STRIP_ANSI_END}`,
template,
options,
PATTERN_REGEX_OPTIONS,
);
if (predicate == null) return null;
return {
Expand All @@ -57,10 +53,13 @@ export function buildPatternFilter(
export function parsePatternFilter(filter: Filter): PatternFilter | null {
if (filter.type !== 'sql') return null;
const excluded = filter.condition.startsWith(NOT);
const parsed = parsePatternPredicate(
excluded ? filter.condition.slice(NOT.length) : filter.condition,
options,
);
const predicate = excluded
? filter.condition.slice(NOT.length)
: filter.condition;
// Also read back filters saved under drain3
const parsed =
parsePatternPredicate(predicate, PATTERN_REGEX_OPTIONS) ??
parsePatternPredicate(predicate, LEGACY_PATTERN_REGEX_OPTIONS);
if (parsed == null) return null;
const { valueExpression, template } = parsed;
if (
Expand Down
Loading