Skip to content

fix(common-utils): drop empty tokens like drain3's str.split() - #3908

Open
karljohang wants to merge 3 commits into
ClickHouse:mainfrom
karljohang:claude/drain-ts-tokenizer
Open

karljohang wants to merge 3 commits into
ClickHouse:mainfrom
karljohang:claude/drain-ts-tokenizer

Conversation

@karljohang

@karljohang karljohang commented Dec 4, 2026 •

Copy link
Copy Markdown

Summary

The TypeScript Drain port is meant to mine the same patterns as drain3, but it splits lines differently. The tokenizer getContentAsTokens turns the extra delimiters into spaces and splits on /\s+/, so a line that starts or ends with a delimiter keeps an empty token there. Python drops that empty token. #3762 includes this fix as part of a larger change. This lands it on its own so other work isn't blocked, and rebasing over it should be trivial.

Log line:    "[INFO] cache miss"
Python:      " INFO  cache miss".split()       → ['INFO', 'cache', 'miss']
JavaScript:  " INFO  cache miss".split(/\s+/)  → ['', 'INFO', 'cache', 'miss']

The empty token put [INFO] cache miss and INFO cache miss in different patterns and left a stray space at the start or end of templates. For logs emitted by HyperDX, the empty tokens made 1,743 of 7,990 templates differ from drain3 and put five lines in a different cluster. The port now puts all 1,209 lines of a generated sample of common log formats into the same clusters as drain3 with the same templates. Only callers that set extra delimiters are affected.

How to reproduce

Run yarn ci:unit src/__tests__/drain.test.ts in packages/common-utils: test_get_content_as_tokens_drops_empty_tokens fails on main and passes here.

How to test on Vercel preview

N/A — non-UI change.


Note

Medium Risk
Changes clustering and templates for callers using drain extra delimiters; behavior is intentional parity with drain3 but can shift existing mined patterns.

Overview
Aligns the TypeScript Drain tokenizer with drain3 when extraDelimiters are configured. After replacing delimiters with spaces, getContentAsTokens now uses .filter(Boolean) on the whitespace split so leading/trailing delimiter positions no longer produce empty tokens (e.g. [INFO] cache miss and INFO cache miss tokenize the same way).

TemplateMiner.extractParameters also trims the message after delimiter normalization so parameter extraction stays consistent with the mined template.

New unit tests cover tokenization, clustering equivalence across bracket-prefixed lines, and extracting parameters from parenthesis-wrapped messages. A patch changeset is included for @hyperdx/common-utils.

Reviewed by Cursor Bugbot for commit 70c9394. Bugbot is set up for automated code reviews on this repo. Configure here.

drewhammond and others added 2 commits December 4, 2026 15:20
The TypeScript port's tokenizer, getContentAsTokens, turns the extra
delimiters into spaces and then splits on /\s+/. A line that starts or ends
with a delimiter keeps an empty token there, which Python's str.split()
drops:

    Python:      " INFO  cache miss".split()       → ['INFO', 'cache', 'miss']
    JavaScript:  " INFO  cache miss".split(/\s+/)  → ['', 'INFO', 'cache', 'miss']

On logs emitted from HyperDX that changed 1,743 of 7,990 templates and
moved five lines to a different cluster compared with drain3. The two now
agree on all 1,209 lines of a generated sample in common log formats.
@karljohang
karljohang requested a review from a team as a code owner December 4, 2026 20:20
@vercel

vercel Bot commented Dec 4, 2026

Copy link
Copy Markdown

@karljohang is attempting to deploy a commit to the HyperDX Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 70c9394

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hyperdx/common-utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the needs-vouch Author needs a maintainer to vouch for them label Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Hi @karljohang, thanks for the pull request!

Before we review code from a first-time contributor, a maintainer vouches for them, and you're not on our list yet. There's nothing else to file and this PR stays open — a maintainer will look at it, usually within a day or two, and vouch for you right here if it's a genuine attempt at the problem.

To make that quick, check the PR description has:

  • The issue it addresses (ideally one labelled Accepting PR) and why the change is needed
  • Steps to reproduce the problem and verify the fix, starting from https://play.hyperdx.io or yarn app:dev:local connected to the demo server
  • Before/after screenshots if anything in the UI changed

More detail in our contributing guide.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

✅ No critical issues found.

The diff is a small, correct parity fix to the TypeScript Drain port. getContentAsTokens now drops empty tokens via .split(/\s+/).filter(Boolean) (reproducing the old empty-input guard and matching Python str.split() — only '' is falsy, so no real token like '0' is lost), and extractParameters trims the message after delimiter replacement so boundary-delimiter lines still match the anchored, trimmed template. The prior Cursor Bugbot boundary-extraction finding is confirmed resolved and covered by a new test.

🟡 P2 — recommended

  • packages/common-utils/src/drain/template-miner.ts:83 — the new unconditional message.trim() changes behavior even when no extra delimiters are configured: a whitespace-padded message now extracts parameters where it previously returned null, and this path has no test.
    • Fix: Add a test with drainExtraDelimiters empty that calls extractParameters on a leading/trailing-whitespace message and asserts the extracted values.
    • testing, correctness, kieran-typescript
🔵 P3 nitpicks (2)
  • packages/common-utils/src/__tests__/drain.test.ts:311 — the boundary-delimiter extraction test uses only the <*> wildcard with empty masking instructions, so the new trim + delimiter-stripping is never exercised alongside masked/parametrized tokens.

    • Fix: Add a case combining drainExtraDelimiters with a NUM masking instruction and assert both the wildcard and masked values extract with correct ordering.
  • packages/common-utils/src/drain/template-miner.ts:83 — the extra-delimiter replacement plus whitespace normalization is duplicated across getContentAsTokens (trim-then-replace+filter) and extractParameters (replace-then-trim); the two agree today only incidentally, with no shared code or test enforcing it.

    • Fix: Extract the shared normalization into one helper consumed by both paths, or add a comment at both sites noting they must stay semantically aligned.

Reviewers (8): correctness, testing, maintainability, project-standards, kieran-typescript, previous-comments, agent-native, learnings-researcher.

Testing gaps: The anchored single-space template regex still won't match internal consecutive/boundary delimiters that collapse to double spaces (e.g. a (b)c → a b c); this is pre-existing (the \ →\s+ substitution at template-miner.ts:160 is dead because escapeRegex does not escape spaces) and out of scope for this PR, but remains untested.

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 602cf66. Configure here.

}
if (c.length === 0) return [];
return c.split(/\s+/);
return c.split(/\s+/).filter(Boolean);

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.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

1 finding(s): 🔴 0 P0/P1 · 🟡 0 P2 · 🔵 1 P3

No inline comments. 1 P3 folded at the bottom.

🔵 1 P3 — no action needed before merge
  • 🔵 P3 packages/common-utils/src/drain/drain.ts:109 — Drain tokenizer fix duplicates tokenize in noisy-matcher.ts; the delimiter-to-space loop now exists in three places → packages/common-utils/src/drain/noisy-matcher.ts:78 tokenize already does trim → delimiters to spaces → trim → drop empties, and its comment says it 'Mirrors Drain's tokenizer'. After this change it behaves exactly like Drain.getContentAsTokens. TemplateMiner.extractParameters (template-miner.ts:80-83) also has its own copy of the delimiter loop and now adds a trim. Move the normalization into one exported helper in drain.ts, for example normalizeDelimiters(text, delimiters) plus a tokenize built on it. Then have getContentAsTokens, noisy-matcher.ts and extractParameters all call it, so the tokenizer can't drift apart again (that drift is the bug this PR fixes).

Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

Templates no longer start or end with a space, so extractParameters
trims the line after turning delimiters into spaces; the template
regex is anchored. Adds tests for empty and delimiter-only input and
for lines that differ only in boundary delimiters sharing a cluster.
@karljohang

karljohang commented Dec 4, 2026 •

Copy link
Copy Markdown
Author

Not deduplicating the tokenizers here: #3762 adds a shared tokenizeContent in drain.ts, which is the natural place for this.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external needs-vouch Author needs a maintainer to vouch for them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants