feat(promql): do not use regex for string labels (#7669)

In Prometheus, label values are treated as strings when used with the
`=` and `!=` operators, and as regular expressions when used with the
`=~` and `!~` operators.

Injecting and then highlighting all label values as regex leads to a
situation where entirely valid **string** label values containing regex
special characters are mistakenly parsed and highlighted as regex. This
results in syntax errors, causing labels to be highlighted incorrectly.

For example, in `foo{bar=~"[a-z]{1,3}"}`, `{` and `}` are regex special
characters, so regex highlighting is expected. However, in
`foo{path="/foo/{id}"}`, `{` and `}` are just part of the string and
have no special meaning, so the whole value should be highlighted as a
string.
This commit is contained in:
Daniil Leontev 2025-02-20 12:17:32 +01:00 committed by GitHub
parent a15dd9280f
commit d2ff400641
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 86 additions and 2 deletions

View file

@ -36,7 +36,19 @@
(label_name) @variable.member
(label_value) @string.regexp
((label_name)
[
"=~"
"!~"
]
(label_value) @string.regexp)
((label_name)
[
"="
"!="
]
(label_value) @string)
(function_name) @function.call

View file

@ -1,6 +1,11 @@
((comment) @injection.content
(#set! injection.language "comment"))
((label_value) @injection.content
((label_name)
[
"=~"
"!~"
]
(label_value) @injection.content
(#set! injection.language "regex")
(#offset! @injection.content 0 1 0 -1))