Skip to content

Commit 8831c70

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

File tree

11 files changed

+241
-2
lines changed

11 files changed

+241
-2
lines changed

components.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

Lines changed: 4 additions & 0 deletions
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

Lines changed: 99 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-promql.html

Lines changed: 17 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
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.
Lines changed: 11 additions & 0 deletions
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.
Lines changed: 38 additions & 0 deletions
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.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy