mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 11:06:54 -04:00
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:
parent
a15dd9280f
commit
d2ff400641
6 changed files with 86 additions and 2 deletions
10
tests/query/highlights/promql/regex.promql
Normal file
10
tests/query/highlights/promql/regex.promql
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
foo{path=~"^foo$"}[5m] or
|
||||
# ^ @string.regexp
|
||||
foo{path!~"[a-zA-Z0-9]{1,3}"}[5m] or
|
||||
# ^ @string.regexp
|
||||
foo{path="/api/users/{userId}"}[5m] or
|
||||
# ^ @string
|
||||
foo{path!="/api/users/{userId}"}[5m]
|
||||
# ^ @string
|
||||
|
||||
# vim: ft=promql
|
||||
33
tests/query/highlights/yaml/promql-on-prometheus-rules.yaml
Normal file
33
tests/query/highlights/yaml/promql-on-prometheus-rules.yaml
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
groups:
|
||||
- name: Hardware alerts
|
||||
rules:
|
||||
- alert: Node down
|
||||
expr: up{job="node_exporter"} == 0
|
||||
# ^ @type
|
||||
for: 3m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
title: Node {{ $labels.instance }} is down
|
||||
description: Failed to scrape {{ $labels.job }} on {{ $labels.instance }} for more than 3 minutes. Node seems down.
|
||||
- alert: Node down
|
||||
expr: |
|
||||
up{job="node_exporter"} == 0
|
||||
# ^ @type
|
||||
for: 3m
|
||||
labels:
|
||||
severity: warning
|
||||
- alert: Regex and String matching
|
||||
expr: |
|
||||
foo{path=~"^foo$"}[5m] or foo{path!~"[a-zA-Z0-9]{1,3}"}[5m] or foo{path="/api/users/{userId}"}[5m] or foo{path!="/api/users/{userId}"}[5m]
|
||||
# ^ @type
|
||||
# ^ @string.regexp
|
||||
# ^ @string.regexp
|
||||
# ^ @string
|
||||
# ^ @string
|
||||
for: 3m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
title: Foo
|
||||
description: Bar
|
||||
10
tests/query/injections/promql/regex.promql
Normal file
10
tests/query/injections/promql/regex.promql
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
foo{path=~"^foo$"}[5m] or
|
||||
# ^ @regex
|
||||
foo{path!~"[a-zA-Z0-9]{1,3}"}[5m] or
|
||||
# ^ @regex
|
||||
foo{path="/api/users/{userId}"}[5m] or
|
||||
# ^ @!regex
|
||||
foo{path!="/api/users/{userId}"}[5m]
|
||||
# ^ @!regex
|
||||
|
||||
# vim: ft=promql
|
||||
|
|
@ -17,3 +17,17 @@ groups:
|
|||
for: 3m
|
||||
labels:
|
||||
severity: warning
|
||||
- alert: Regex and String matching
|
||||
expr: |
|
||||
foo{path=~"^foo$"}[5m] or foo{path!~"[a-zA-Z0-9]{1,3}"}[5m] or foo{path="/api/users/{userId}"}[5m] or foo{path!="/api/users/{userId}"}[5m]
|
||||
# ^ @promql
|
||||
# ^ @regex
|
||||
# ^ @regex
|
||||
# ^ @!regex
|
||||
# ^ @!regex
|
||||
for: 3m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
title: Foo
|
||||
description: Bar
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue