Skip to content

fix(lexers): recognize colons in PromQL metric names - #1495

Merged
github-actions[bot] merged 1 commit into
alecthomas:masterfrom
spicysharkco:fix/promql-metric-name-colons
Nov 17, 2026
Merged

github-actions[bot] merged 1 commit into
alecthomas:masterfrom
spicysharkco:fix/promql-metric-name-colons

Conversation

@spicysharkco

Copy link
Copy Markdown
Contributor

For these PromQL metric names:

instance_path:requests:rate5m
:requests:rate5m:

Main splits each name at its colons:

  {"type":"NameVariable","value":"instance_path"},
  {"type":"Error","value":":"},
  {"type":"NameVariable","value":"requests"},
  {"type":"Error","value":":"},
  {"type":"NameVariable","value":"rate5m"},

The second name also starts and ends with {"type":"Error","value":":"}. Each name should be one NameVariable token.

In the current Prometheus docs, Metric names and labels gives the pattern [a-zA-Z_:][a-zA-Z0-9_:]* and reserves colons for recording rules. The recording rules guide uses the form level:metric:operations, and its examples include instance_path:requests:rate5m.

The name rule accepts a letter or underscore followed by letters, digits and underscores. This adds : to both character classes, as in the Prometheus pattern.

The new golden fails without the rule change:

go test -count=1 -run 'TestLexers/PromQL' ./lexers

go test -count=1 ./... passes.

The name rule accepts a letter or underscore followed by letters,
digits and underscores, so each colon in a recording-rule name such as
instance_path:requests:rate5m lexes as an error. Allow colons in both
character classes, as the Prometheus metric name pattern does.

https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
https://prometheus.io/docs/practices/rules/#naming
@github-actions
github-actions Bot enabled auto-merge (squash) November 17, 2026 13:56
@github-actions
github-actions Bot merged commit 0370886 into alecthomas:master Nov 17, 2026
4 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 17, 2026
PromQL allows single-quoted, double-quoted, and raw backtick strings.
The quoted forms use backslash escapes, while raw strings can contain
newlines.

The lexer currently ends a string on either quote type and does not
account for escapes. That splits valid strings into incorrect tokens,
including Error tokens. Raw backtick strings are not recognized either.
In a label value, an escaped quote followed by a brace can also end the
label state too early.

Use a shared string state for expressions and label values. Keep the
existing Punctuation delimiters and LiteralString contents. Add golden
coverage for escaped and opposite quotes, raw and multiline strings,
braces inside label values, escaped trailing backslashes, and ordinary
strings/selectors.

Validation on base and candidate:
- Full ordinary suite with pinned Go 1.27.3 and shuffle seed 42 passes.
- Go 1.25 and 1.27.3 race/shuffle runs pass with existing TestGet,
TestLongTokensMatch, and TestLexers/OpenEdge_ABL excluded.
- Focused PromQL tests pass on Linux/386.
- With only the new golden copied to unchanged base, the new case fails
while existing PromQL cases pass.

Candidate-only checks:
- Focused PromQL race tests pass 100 times.
- golangci-lint 2.15.0 passes.
- Regenerating lexer metadata produces no change.
- HTML output was rendered and visually checked against base.

Full race runs are not uniformly green: TestLongTokensMatch hits its
existing regex timeout on both trees under Go 1.26.6, and also on the
candidate in one pinned Go run. Bounded runs also hit an existing
OpenEdge_ABL test timeout on both trees; default-order runs stalled at
TestGet/ViaGoooFilename on both. Plain vet returns identical existing
unkeyed Rule warnings on both trees.

This extends the PromQL support in #422 and #474 while retaining the
metric-colon behavior from #1495.

The lexer XML has a trailing newline versus the original draft. The
complete diff is three files, +166/-8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant