Repository navigation
fix(common-utils): drop empty tokens like drain3's str.split() - #3908
karljohang wants to merge 3 commits into
Conversation
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 is attempting to deploy a commit to the HyperDX Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: 70c9394 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
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:
More detail in our contributing guide. |
Deep Review✅ No critical issues found. The diff is a small, correct parity fix to the TypeScript Drain port. 🟡 P2 — recommended
🔵 P3 nitpicks (2)
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. 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 |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 602cf66. Configure here.
| } | ||
| if (c.length === 0) return []; | ||
| return c.split(/\s+/); | ||
| return c.split(/\s+/).filter(Boolean); |
There was a problem hiding this comment.
🟡 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
There was a problem hiding this comment.
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.
PR Review
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
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.
|
Not deduplicating the tokenizers here: #3762 adds a shared |

Summary
The TypeScript Drain port is meant to mine the same patterns as
drain3, but it splits lines differently. The tokenizergetContentAsTokensturns 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.The empty token put
[INFO] cache missandINFO cache missin 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 fromdrain3and 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 asdrain3with the same templates. Only callers that set extra delimiters are affected.How to reproduce
Run
yarn ci:unit src/__tests__/drain.test.tsinpackages/common-utils:test_get_content_as_tokens_drops_empty_tokensfails onmainand 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
extraDelimitersare configured. After replacing delimiters with spaces,getContentAsTokensnow uses.filter(Boolean)on the whitespace split so leading/trailing delimiter positions no longer produce empty tokens (e.g.[INFO] cache missandINFO cache misstokenize the same way).TemplateMiner.extractParametersalso 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.