Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/drain-empty-tokens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/common-utils': patch
---

fix: a Drain miner with extra delimiters set drops the empty token (like
`drain3` does) where a line starts or ends with a delimiter.
40 changes: 40 additions & 0 deletions packages/common-utils/src/__tests__/drain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ describe('Drain', () => {
expect(model.clusterCount).toBe(2);
});

it('test_get_content_as_tokens_drops_empty_tokens', () => {
const model = new Drain(4, 0.4, 100, null, ['[', ']', '(', ')']);
expect(model.getContentAsTokens('[INFO] cache miss (ttl 30s)')).toEqual([
'INFO',
'cache',
'miss',
'ttl',
'30s',
]);
expect(model.getContentAsTokens('')).toEqual([]);
expect(model.getContentAsTokens(' ')).toEqual([]);
expect(model.getContentAsTokens('[]()')).toEqual([]);
});

it('test_add_log_message_ignores_boundary_delimiters', () => {
const model = new Drain(4, 0.4, 100, null, ['[', ']']);
const [first] = model.addLogMessage('[INFO] cache miss');
const [second, changeType] = model.addLogMessage('INFO cache miss');
expect(second.clusterId).toBe(first.clusterId);
expect(changeType).toBe('none');
expect(second.getTemplate()).toBe('INFO cache miss');
});

it('test_add_log_message', () => {
const model = new Drain();
const entries = `
Expand Down Expand Up @@ -284,4 +307,21 @@ describe('TemplateMiner', () => {
expect(params).not.toBeNull();
expect(params!.length).toBeGreaterThan(0);
});

it('extract_parameters_with_boundary_delimiters', () => {
const config = new TemplateMinerConfig();
config.drainExtraDelimiters = ['(', ')'];
config.maskingInstructions = [];
const miner = new TemplateMiner(config);

miner.addLogMessage('(user alice logged in)');
const { templateMined } = miner.addLogMessage('(user bob logged in)');

expect(templateMined).toBe('user <*> logged in');
expect(
miner
.extractParameters(templateMined, '(user bob logged in)', false)
?.map(p => p.value),
).toEqual(['bob']);
});
});
3 changes: 1 addition & 2 deletions packages/common-utils/src/drain/drain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ export class Drain {
for (const delimiter of this.extraDelimiters) {
c = c.split(delimiter).join(' ');
}
if (c.length === 0) return [];
return c.split(/\s+/);
return c.split(/\s+/).filter(Boolean);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 P2 — extractParameters no longer matches lines that start or end with a delimiter

TemplateMiner.extractParameters (template-miner.ts:79-87) swaps each delimiter for a space without trimming, then matches against ^…$. Before this PR, a template mined from [INFO] cache miss started with a space ( INFO cache miss), which became ^\s+INFO… and matched INFO cache miss. Now the template is INFO cache miss and the match returns null. Fix: trim message after the delimiter loop, or allow \s* at both ends of the regex in getTemplateParameterExtractionRegex. Add a test with a delimiter-wrapped line. Today only tests call extractParameters, but it is a public method.

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

@karljohang karljohang Dec 4, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 70c9394. The [INFO] cache miss example never matched, before or after this PR. The \\ → \\s+ replace in getTemplateParameterExtractionRegex never fires because escapeRegex doesn't escape spaces, so the doubled space after INFO fails. drain3 has the same dead line. Lines like (user bob logged in) did break in this PR, and they match again now. The new test covers that.

}

getSeqDistance(
Expand Down
1 change: 1 addition & 0 deletions packages/common-utils/src/drain/template-miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class TemplateMiner {
for (const delimiter of this.extraDelimiters) {
message = message.split(delimiter).join(' ');
}
message = message.trim();

const { regex: templateRegex, paramMap } =
this.getTemplateParameterExtractionRegex(logTemplate, exactMatching);
Expand Down
Loading