Skip to content

Commit 8831c70

Browse files
authored
Added PromQL language (#2628)
1 parent 9df20c5 commit 8831c70

11 files changed

+241
-2
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+4
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,10 @@
875875
"title": "Prolog",
876876
"owner": "Golmote"
877877
},
878+
"promql": {
879+
"title": "PromQL",
880+
"owner": "arendjr"
881+
},
878882
"properties": {
879883
"title": ".properties",
880884
"owner": "Golmote"

components/prism-promql.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Thanks to: https://github.com/prometheus-community/monaco-promql/blob/master/src/promql/promql.ts
2+
// As well as: https://kausal.co/blog/slate-prism-add-new-syntax-promql/
3+
4+
(function (Prism) {
5+
// PromQL Aggregation Operators
6+
// (https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators)
7+
var aggregations = [
8+
'sum',
9+
'min',
10+
'max',
11+
'avg',
12+
'group',
13+
'stddev',
14+
'stdvar',
15+
'count',
16+
'count_values',
17+
'bottomk',
18+
'topk',
19+
'quantile'
20+
];
21+
22+
// PromQL vector matching + the by and without clauses
23+
// (https://prometheus.io/docs/prometheus/latest/querying/operators/#vector-matching)
24+
var vectorMatching = [
25+
'on',
26+
'ignoring',
27+
'group_right',
28+
'group_left',
29+
'by',
30+
'without',
31+
];
32+
33+
// PromQL offset modifier
34+
// (https://prometheus.io/docs/prometheus/latest/querying/basics/#offset-modifier)
35+
var offsetModifier = ['offset'];
36+
37+
var keywords = aggregations.concat(vectorMatching, offsetModifier);
38+
39+
Prism.languages.promql = {
40+
'comment': {
41+
pattern: /(^[ \t]*)#.*/m,
42+
lookbehind: true
43+
},
44+
'vector-match': {
45+
// Match the comma-separated label lists inside vector matching:
46+
pattern: new RegExp('((?:' + vectorMatching.join('|') + ')\\s*)\\([^)]*\\)'),
47+
lookbehind: true,
48+
inside: {
49+
'label-key': {
50+
pattern: /\b[^,]*\b/,
51+
alias: 'attr-name',
52+
},
53+
'punctuation': /[(),]/
54+
},
55+
},
56+
'context-labels': {
57+
pattern: /\{[^{}]*\}/,
58+
inside: {
59+
'label-key': {
60+
pattern: /\b[a-z_]\w*(?=\s*(?:=~?|![=~]))/,
61+
alias: 'attr-name',
62+
},
63+
'label-value': {
64+
pattern: /(["'`])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
65+
greedy: true,
66+
alias: 'attr-value',
67+
},
68+
'punctuation': /\{|\}|=~?|![=~]|,/,
69+
},
70+
},
71+
'context-range': [
72+
{
73+
pattern: /\[[\w\s:]+\]/, // [1m]
74+
inside: {
75+
'punctuation': /\[|\]|:/,
76+
'range-duration': {
77+
pattern: /\b(?:\d+(?:[smhdwy]|ms))+\b/i,
78+
alias: 'number',
79+
},
80+
},
81+
},
82+
{
83+
pattern: /(\boffset\s+)\w+/, // offset 1m
84+
lookbehind: true,
85+
inside: {
86+
'range-duration': {
87+
pattern: /\b(?:\d+(?:[smhdwy]|ms))+\b/i,
88+
alias: 'number',
89+
},
90+
},
91+
},
92+
],
93+
'keyword': new RegExp('\\b(?:' + keywords.join('|') + ')\\b', 'i'),
94+
'function': /\b[a-zA-Z_]\w*(?=\s*\()/i,
95+
'number': /[-+]?(?:(?:\b\d+(?:\.\d+)?|\B\.\d+)(?:e[-+]?\d+)?\b|\b(?:0x[0-9a-f]+|nan|inf)\b)/i,
96+
'operator': /[\^*/%+-]|==|!=|<=|<|>=|>|\b(?:and|unless|or)\b/i,
97+
'punctuation': /[{};()`,.[\]]/,
98+
};
99+
})(Prism);

components/prism-promql.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-promql.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<h2>Examples</h2>
2+
<pre><code># These examples are taken from: https://prometheus.io/docs/prometheus/latest/querying/examples/
3+
4+
http_requests_total{job="apiserver", handler="/api/comments"}[5m]
5+
6+
http_requests_total{job=~".*server"}
7+
8+
max_over_time(deriv(rate(distance_covered_total[5s])[30s:5s])[10m:])
9+
10+
sum by (job) (
11+
rate(http_requests_total[5m])
12+
)
13+
14+
sum by (app, proc) (
15+
instance_memory_limit_bytes - instance_memory_usage_bytes
16+
) / 1024 / 1024
17+
</code></pre>

plugins/show-language/prism-show-language.js

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"pq": "PowerQuery",
148148
"mscript": "PowerQuery",
149149
"powershell": "PowerShell",
150+
"promql": "PromQL",
150151
"properties": ".properties",
151152
"protobuf": "Protocol Buffers",
152153
"purebasic": "PureBasic",

plugins/show-language/prism-show-language.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
sum by (job) (
2+
rate(http_requests_total[5m])
3+
)
4+
5+
----------------------------------------------------
6+
7+
[
8+
["keyword", "sum"],
9+
["keyword", "by"],
10+
["vector-match", [
11+
["punctuation", "("],
12+
["label-key", "job"],
13+
["punctuation", ")"]
14+
]],
15+
["punctuation", "("],
16+
["function", "rate"],
17+
["punctuation", "("],
18+
"http_requests_total",
19+
["context-range", [
20+
["punctuation", "["],
21+
["range-duration", "5m"],
22+
["punctuation", "]"]
23+
]],
24+
["punctuation", ")"],
25+
["punctuation", ")"]
26+
]
27+
28+
----------------------------------------------------
29+
30+
Checks aggregate query.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# These examples are taken from ...
2+
3+
----------------------------------------------------
4+
5+
[
6+
["comment", "# These examples are taken from ..."]
7+
]
8+
9+
----------------------------------------------------
10+
11+
Checks for comments.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
max_over_time(deriv(rate(distance_covered_total[5s])[30s:5s])[10m:])
2+
3+
----------------------------------------------------
4+
5+
[
6+
["function", "max_over_time"],
7+
["punctuation", "("],
8+
["function", "deriv"],
9+
["punctuation", "("],
10+
["function", "rate"],
11+
["punctuation", "("],
12+
"distance_covered_total",
13+
["context-range", [
14+
["punctuation", "["],
15+
["range-duration", "5s"],
16+
["punctuation", "]"]
17+
]],
18+
["punctuation", ")"],
19+
["context-range", [
20+
["punctuation", "["],
21+
["range-duration", "30s"],
22+
["punctuation", ":"],
23+
["range-duration", "5s"],
24+
["punctuation", "]"]
25+
]],
26+
["punctuation", ")"],
27+
["context-range", [
28+
["punctuation", "["],
29+
["range-duration", "10m"],
30+
["punctuation", ":"],
31+
["punctuation", "]"]
32+
]],
33+
["punctuation", ")"]
34+
]
35+
36+
----------------------------------------------------
37+
38+
Checks subquery.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
http_requests_total{job="apiserver", handler="/api/comments"}[5m]
2+
3+
http_requests_total{job=~".*server"}
4+
5+
----------------------------------------------------
6+
7+
[
8+
"http_requests_total",
9+
["context-labels", [
10+
["punctuation", "{"],
11+
["label-key", "job"],
12+
["punctuation", "="],
13+
["label-value", "\"apiserver\""],
14+
["punctuation", ","],
15+
["label-key", "handler"],
16+
["punctuation", "="],
17+
["label-value", "\"/api/comments\""],
18+
["punctuation", "}"]
19+
]],
20+
["context-range", [
21+
["punctuation", "["],
22+
["range-duration", "5m"],
23+
["punctuation", "]"]
24+
]],
25+
26+
"\r\n\r\nhttp_requests_total",
27+
["context-labels", [
28+
["punctuation", "{"],
29+
["label-key", "job"],
30+
["punctuation", "=~"],
31+
["label-value", "\".*server\""],
32+
["punctuation", "}"]
33+
]]
34+
]
35+
36+
----------------------------------------------------
37+
38+
Checks simple time series queries.

0 commit comments

Comments
 (0)