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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Expand Up @@ -875,6 +875,10 @@
"title": "Prolog",
"owner": "Golmote"
},
"promql": {
"title": "PromQL",
"owner": "arendjr"
},
"properties": {
"title": ".properties",
"owner": "Golmote"
Expand Down
173 changes: 173 additions & 0 deletions components/prism-promql.js
@@ -0,0 +1,173 @@
// 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/

// 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 functions
// (https://prometheus.io/docs/prometheus/latest/querying/functions/)
var functions = [
"abs",
"absent",
"ceil",
"changes",
"clamp_max",
"clamp_min",
"day_of_month",
"day_of_week",
"days_in_month",
"delta",
"deriv",
"exp",
"floor",
"histogram_quantile",
"holt_winters",
"hour",
"idelta",
"increase",
"irate",
"label_join",
"label_replace",
"ln",
"log2",
"log10",
"minute",
"month",
"predict_linear",
"rate",
"resets",
"round",
"scalar",
"sort",
"sort_desc",
"sqrt",
"time",
"timestamp",
"vector",
"year",
];

// PromQL specific functions: Aggregations over time
// (https://prometheus.io/docs/prometheus/latest/querying/functions/#aggregation_over_time)
var aggregationsOverTime = [];
aggregations.forEach(function(agg) {
aggregationsOverTime.push(agg + "_over_time");
});

// 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 Operators
// (https://prometheus.io/docs/prometheus/latest/querying/operators/)
var operators = [
"\\+",
"-",
"\\*",
"/",
"%",
"\\^",
"==",
"!=",
">",
"<",
">=",
"<=",
"and",
"or",
"unless",
];

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

// Merging all the keywords in one list
var keywords = aggregations
.concat(functions)
.concat(aggregationsOverTime)
.concat(vectorMatching)
.concat(offsetModifier);

Prism.languages.promql = {
comment: {
pattern: /^\s*#.*/m,
},
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
"context-aggregation": {
pattern: new RegExp("((?:" + vectorMatching.join("|") + ")\\s*)\\([^)]*\\)"),
lookbehind: true,
inside: {
"label-key": {
pattern: /\b[^,]*\b/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the idea here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to highlight the comma-separated labels.

alias: "attr-name",
},
},
},
"context-labels": {
pattern: /\{[^}]*\}/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
inside: {
"label-key": {
pattern: /[a-z_]\w*(?=\s*(?:=~?|![=~]))/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: "attr-name",
},
"label-value": {
pattern: /"(?:\\.|[^\\"])*"/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: "attr-value",
},
punctuation: /\{|\}|=~?|![=~]/,
},
},
function: new RegExp("\\b(?:" + keywords.join("|") + ")(?=\\s*\\()", "i"),
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
"context-range": [
{
pattern: /\[[^\]]*\]/, // [1m]
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
inside: {
punctuation: /\[|\]/,
"range-duration": {
pattern: /\b\d+[smhdwy]\b/i,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: "number",
},
},
},
{
pattern: /(offset\s+)\w+/, // offset 1m
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
lookbehind: true,
inside: {
"range-duration": {
pattern: /\b\d+[smhdwy]\b/i,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
alias: "number",
},
},
},
],
number: /\b-?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
operator: new RegExp(
"/[-+/%^*~]|=~?|![=~]|&&?|(?:\\|\\|?)|!=?|<(?:=>?|<|>)?|>[>=]?|\\b(?:" +
operators.join("|") +
")\\b",
"i"
),
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
punctuation: /[{};()`,.[\]]/,
};

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.

18 changes: 18 additions & 0 deletions examples/prism-promql.html
@@ -0,0 +1,18 @@
<h2>Examples</h2>
<pre><code>
# These examples are taken from: https://prometheus.io/docs/prometheus/latest/querying/examples/
arendjr marked this conversation as resolved.
Show resolved Hide resolved

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
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.

29 changes: 29 additions & 0 deletions tests/languages/promql/aggregate_selection.test
@@ -0,0 +1,29 @@
sum by (job) (
rate(http_requests_total[5m])
)

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

[
"sum by ",
["context-aggregation", [
"(",
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
["label-key", "job"],
")"
]],
["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
@@ -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
@@ -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"],
":",
["range-duration", "5s"],
["punctuation", "]"]
]],
["punctuation", ")"],
["context-range", [
["punctuation", "["],
["range-duration", "10m"],
":",
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
["punctuation", "]"]
]],
["punctuation", ")"]
]

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

Checks subquery.
38 changes: 38 additions & 0 deletions tests/languages/promql/time_series_selection.test
@@ -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\""],
", ",
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
["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.