-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add PromQL language support #2628
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
Changes from 4 commits
482828e
d66f50e
7e02aaa
c4af08a
7486333
ab12d10
b575d4a
ebded9f
2fb4756
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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/, | ||
|
||
| alias: "attr-name", | ||
| }, | ||
| }, | ||
| }, | ||
| "context-labels": { | ||
| pattern: /\{[^}]*\}/, | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| inside: { | ||
| "label-key": { | ||
| pattern: /[a-z_]\w*(?=\s*(?:=~?|![=~]))/, | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| alias: "attr-name", | ||
| }, | ||
| "label-value": { | ||
| pattern: /"(?:\\.|[^\\"])*"/, | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| alias: "attr-value", | ||
| }, | ||
| punctuation: /\{|\}|=~?|![=~]/, | ||
| }, | ||
| }, | ||
| function: new RegExp("\\b(?:" + keywords.join("|") + ")(?=\\s*\\()", "i"), | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "context-range": [ | ||
| { | ||
| pattern: /\[[^\]]*\]/, // [1m] | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| inside: { | ||
| punctuation: /\[|\]/, | ||
| "range-duration": { | ||
| pattern: /\b\d+[smhdwy]\b/i, | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| alias: "number", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| pattern: /(offset\s+)\w+/, // offset 1m | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| lookbehind: true, | ||
| inside: { | ||
| "range-duration": { | ||
| pattern: /\b\d+[smhdwy]\b/i, | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| alias: "number", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| number: /\b-?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/, | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| operator: new RegExp( | ||
| "/[-+/%^*~]|=~?|![=~]|&&?|(?:\\|\\|?)|!=?|<(?:=>?|<|>)?|>[>=]?|\\b(?:" + | ||
| operators.join("|") + | ||
| ")\\b", | ||
| "i" | ||
| ), | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| punctuation: /[{};()`,.[\]]/, | ||
| }; | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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/ | ||
arendjr marked this conversation as resolved.
Outdated
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> | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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", [ | ||
| "(", | ||
RunDevelopment marked this conversation as resolved.
Outdated
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. | ||
| 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. |
| 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"], | ||
| ":", | ||
RunDevelopment marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ["punctuation", "]"] | ||
| ]], | ||
| ["punctuation", ")"] | ||
| ] | ||
|
|
||
| ---------------------------------------------------- | ||
|
|
||
| Checks subquery. | ||
| 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\""], | ||
| ", ", | ||
RunDevelopment marked this conversation as resolved.
Outdated
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. | ||
Uh oh!
There was an error while loading. Please reload this page.