Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PromQL language support #2628

Merged
merged 9 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@
"title": "Prolog",
"owner": "Golmote"
},
"promql": {
"title": "PromQL",
"owner": "arendjr"
},
"properties": {
"title": ".properties",
"owner": "Golmote"
Expand Down
99 changes: 99 additions & 0 deletions components/prism-promql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Thanks to: https://github.com/prometheus-community/monaco-promql/blob/master/src/promql/promql.ts
// As well as: https://kausal.co/blog/slate-prism-add-new-syntax-promql/

(function (Prism) {
// PromQL Aggregation Operators
// (https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators)
var aggregations = [
'sum',
'min',
'max',
'avg',
'group',
'stddev',
'stdvar',
'count',
'count_values',
'bottomk',
'topk',
'quantile'
];

// PromQL vector matching + the by and without clauses
// (https://prometheus.io/docs/prometheus/latest/querying/operators/#vector-matching)
var vectorMatching = [
'on',
'ignoring',
'group_right',
'group_left',
'by',
'without',
];

// PromQL offset modifier
// (https://prometheus.io/docs/prometheus/latest/querying/basics/#offset-modifier)
var offsetModifier = ['offset'];

var keywords = aggregations.concat(vectorMatching, offsetModifier);

Prism.languages.promql = {
'comment': {
pattern: /(^[ \t]*)#.*/m,
lookbehind: true
},
'vector-match': {
// Match the comma-separated label lists inside vector matching:
pattern: new RegExp('((?:' + vectorMatching.join('|') + ')\\s*)\\([^)]*\\)'),
lookbehind: true,
inside: {
'label-key': {
pattern: /\b[^,]*\b/,
alias: 'attr-name',
},
'punctuation': /[(),]/
},
},
'context-labels': {
pattern: /\{[^{}]*\}/,
inside: {
'label-key': {
pattern: /\b[a-z_]\w*(?=\s*(?:=~?|![=~]))/,
alias: 'attr-name',
},
'label-value': {
pattern: /(["'`])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
greedy: true,
alias: 'attr-value',
},
'punctuation': /\{|\}|=~?|![=~]|,/,
},
},
'context-range': [
{
pattern: /\[[\w\s:]+\]/, // [1m]
inside: {
'punctuation': /\[|\]|:/,
'range-duration': {
pattern: /\b(?:\d+(?:[smhdwy]|ms))+\b/i,
alias: 'number',
},
},
},
{
pattern: /(\boffset\s+)\w+/, // offset 1m
lookbehind: true,
inside: {
'range-duration': {
pattern: /\b(?:\d+(?:[smhdwy]|ms))+\b/i,
alias: 'number',
},
},
},
],
'keyword': new RegExp('\\b(?:' + keywords.join('|') + ')\\b', 'i'),
'function': /\b[a-zA-Z_]\w*(?=\s*\()/i,
'number': /[-+]?(?:(?:\b\d+(?:\.\d+)?|\B\.\d+)(?:e[-+]?\d+)?\b|\b(?:0x[0-9a-f]+|nan|inf)\b)/i,
'operator': /[\^*/%+-]|==|!=|<=|<|>=|>|\b(?:and|unless|or)\b/i,
'punctuation': /[{};()`,.[\]]/,
};
})(Prism);
1 change: 1 addition & 0 deletions components/prism-promql.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/prism-promql.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h2>Examples</h2>
<pre><code># These examples are taken from: https://prometheus.io/docs/prometheus/latest/querying/examples/

http_requests_total{job="apiserver", handler="/api/comments"}[5m]

http_requests_total{job=~".*server"}

max_over_time(deriv(rate(distance_covered_total[5s])[30s:5s])[10m:])

sum by (job) (
rate(http_requests_total[5m])
)

sum by (app, proc) (
instance_memory_limit_bytes - instance_memory_usage_bytes
) / 1024 / 1024
</code></pre>
1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"pq": "PowerQuery",
"mscript": "PowerQuery",
"powershell": "PowerShell",
"promql": "PromQL",
"properties": ".properties",
"protobuf": "Protocol Buffers",
"purebasic": "PureBasic",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/languages/promql/aggregate_selection.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
sum by (job) (
rate(http_requests_total[5m])
)

----------------------------------------------------

[
["keyword", "sum"],
["keyword", "by"],
["vector-match", [
["punctuation", "("],
["label-key", "job"],
["punctuation", ")"]
]],
["punctuation", "("],
["function", "rate"],
["punctuation", "("],
"http_requests_total",
["context-range", [
["punctuation", "["],
["range-duration", "5m"],
["punctuation", "]"]
]],
["punctuation", ")"],
["punctuation", ")"]
]

----------------------------------------------------

Checks aggregate query.
11 changes: 11 additions & 0 deletions tests/languages/promql/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# These examples are taken from ...

----------------------------------------------------

[
["comment", "# These examples are taken from ..."]
]

----------------------------------------------------

Checks for comments.
38 changes: 38 additions & 0 deletions tests/languages/promql/subquery_selection.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
max_over_time(deriv(rate(distance_covered_total[5s])[30s:5s])[10m:])

----------------------------------------------------

[
["function", "max_over_time"],
["punctuation", "("],
["function", "deriv"],
["punctuation", "("],
["function", "rate"],
["punctuation", "("],
"distance_covered_total",
["context-range", [
["punctuation", "["],
["range-duration", "5s"],
["punctuation", "]"]
]],
["punctuation", ")"],
["context-range", [
["punctuation", "["],
["range-duration", "30s"],
["punctuation", ":"],
["range-duration", "5s"],
["punctuation", "]"]
]],
["punctuation", ")"],
["context-range", [
["punctuation", "["],
["range-duration", "10m"],
["punctuation", ":"],
["punctuation", "]"]
]],
["punctuation", ")"]
]

----------------------------------------------------

Checks subquery.
38 changes: 38 additions & 0 deletions tests/languages/promql/time_series_selection.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
http_requests_total{job="apiserver", handler="/api/comments"}[5m]

http_requests_total{job=~".*server"}

----------------------------------------------------

[
"http_requests_total",
["context-labels", [
["punctuation", "{"],
["label-key", "job"],
["punctuation", "="],
["label-value", "\"apiserver\""],
["punctuation", ","],
["label-key", "handler"],
["punctuation", "="],
["label-value", "\"/api/comments\""],
["punctuation", "}"]
]],
["context-range", [
["punctuation", "["],
["range-duration", "5m"],
["punctuation", "]"]
]],

"\r\n\r\nhttp_requests_total",
["context-labels", [
["punctuation", "{"],
["label-key", "job"],
["punctuation", "=~"],
["label-value", "\".*server\""],
["punctuation", "}"]
]]
]

----------------------------------------------------

Checks simple time series queries.