aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniil Leontev <d@leontiev.xyz>2025-02-20 12:17:32 +0100
committerGitHub <noreply@github.com>2025-02-20 12:17:32 +0100
commitd2ff4006418750aa8b91f280e66bdb623c891c95 (patch)
treef0d35114dc734f35e6fa47fd7d231a76f55100cf
parentbot(lockfile): update pascal, v (diff)
downloadnvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.tar
nvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.tar.gz
nvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.tar.bz2
nvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.tar.lz
nvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.tar.xz
nvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.tar.zst
nvim-treesitter-d2ff4006418750aa8b91f280e66bdb623c891c95.zip
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.
-rw-r--r--queries/promql/highlights.scm14
-rw-r--r--queries/promql/injections.scm7
-rw-r--r--tests/query/highlights/promql/regex.promql10
-rw-r--r--tests/query/highlights/yaml/promql-on-prometheus-rules.yaml33
-rw-r--r--tests/query/injections/promql/regex.promql10
-rw-r--r--tests/query/injections/yaml/promql-on-prometheus-rules.yaml14
6 files changed, 86 insertions, 2 deletions
diff --git a/queries/promql/highlights.scm b/queries/promql/highlights.scm
index 0f58f1578..a99fcdd05 100644
--- a/queries/promql/highlights.scm
+++ b/queries/promql/highlights.scm
@@ -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
diff --git a/queries/promql/injections.scm b/queries/promql/injections.scm
index 4e7c1b261..791aba3d8 100644
--- a/queries/promql/injections.scm
+++ b/queries/promql/injections.scm
@@ -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))
diff --git a/tests/query/highlights/promql/regex.promql b/tests/query/highlights/promql/regex.promql
new file mode 100644
index 000000000..fa61094a3
--- /dev/null
+++ b/tests/query/highlights/promql/regex.promql
@@ -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
diff --git a/tests/query/highlights/yaml/promql-on-prometheus-rules.yaml b/tests/query/highlights/yaml/promql-on-prometheus-rules.yaml
new file mode 100644
index 000000000..910405418
--- /dev/null
+++ b/tests/query/highlights/yaml/promql-on-prometheus-rules.yaml
@@ -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
diff --git a/tests/query/injections/promql/regex.promql b/tests/query/injections/promql/regex.promql
new file mode 100644
index 000000000..08ed74640
--- /dev/null
+++ b/tests/query/injections/promql/regex.promql
@@ -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
diff --git a/tests/query/injections/yaml/promql-on-prometheus-rules.yaml b/tests/query/injections/yaml/promql-on-prometheus-rules.yaml
index f064da375..942fb1369 100644
--- a/tests/query/injections/yaml/promql-on-prometheus-rules.yaml
+++ b/tests/query/injections/yaml/promql-on-prometheus-rules.yaml
@@ -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