Skip to content
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.
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
173 changes: 173 additions & 0 deletions components/prism-promql.js
Original file line number Diff line number Diff line change
@@ -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,
},
"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: /\{[^}]*\}/,
inside: {
"label-key": {
pattern: /[a-z_]\w*(?=\s*(?:=~?|![=~]))/,
alias: "attr-name",
},
"label-value": {
pattern: /"(?:\\.|[^\\"])*"/,
alias: "attr-value",
},
punctuation: /\{|\}|=~?|![=~]/,
},
},
function: new RegExp("\\b(?:" + keywords.join("|") + ")(?=\\s*\\()", "i"),
"context-range": [
{
pattern: /\[[^\]]*\]/, // [1m]
inside: {
punctuation: /\[|\]/,
"range-duration": {
pattern: /\b\d+[smhdwy]\b/i,
alias: "number",
},
},
},
{
pattern: /(offset\s+)\w+/, // offset 1m
lookbehind: true,
inside: {
"range-duration": {
pattern: /\b\d+[smhdwy]\b/i,
alias: "number",
},
},
},
],
number: /\b-?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/,
operator: new RegExp(
"/[-+/%^*~]|=~?|![=~]|&&?|(?:\\|\\|?)|!=?|<(?:=>?|<|>)?|>[>=]?|\\b(?:" +
operators.join("|") +
")\\b",
"i"
),
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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.

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

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

[
"sum by ",
["context-aggregation", [
"(",
["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
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"],
":",
["range-duration", "5s"],
["punctuation", "]"]
]],
["punctuation", ")"],
["context-range", [
["punctuation", "["],
["range-duration", "10m"],
":",
["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\""],
", ",
["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.