[d24f17c] | 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-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|or|unless)\b/i,
|
---|
| 97 | 'punctuation': /[{};()`,.[\]]/,
|
---|
| 98 | };
|
---|
| 99 | }(Prism));
|
---|