prism-promql.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. (function (Prism) {
  4. // PromQL Aggregation Operators
  5. // (https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators)
  6. var aggregations = [
  7. 'sum',
  8. 'min',
  9. 'max',
  10. 'avg',
  11. 'group',
  12. 'stddev',
  13. 'stdvar',
  14. 'count',
  15. 'count_values',
  16. 'bottomk',
  17. 'topk',
  18. 'quantile'
  19. ];
  20. // PromQL vector matching + the by and without clauses
  21. // (https://prometheus.io/docs/prometheus/latest/querying/operators/#vector-matching)
  22. var vectorMatching = [
  23. 'on',
  24. 'ignoring',
  25. 'group_right',
  26. 'group_left',
  27. 'by',
  28. 'without',
  29. ];
  30. // PromQL offset modifier
  31. // (https://prometheus.io/docs/prometheus/latest/querying/basics/#offset-modifier)
  32. var offsetModifier = ['offset'];
  33. var keywords = aggregations.concat(vectorMatching, offsetModifier);
  34. Prism.languages.promql = {
  35. 'comment': {
  36. pattern: /(^[ \t]*)#.*/m,
  37. lookbehind: true
  38. },
  39. 'vector-match': {
  40. // Match the comma-separated label lists inside vector matching:
  41. pattern: new RegExp('((?:' + vectorMatching.join('|') + ')\\s*)\\([^)]*\\)'),
  42. lookbehind: true,
  43. inside: {
  44. 'label-key': {
  45. pattern: /\b[^,]+\b/,
  46. alias: 'attr-name',
  47. },
  48. 'punctuation': /[(),]/
  49. },
  50. },
  51. 'context-labels': {
  52. pattern: /\{[^{}]*\}/,
  53. inside: {
  54. 'label-key': {
  55. pattern: /\b[a-z_]\w*(?=\s*(?:=|![=~]))/,
  56. alias: 'attr-name',
  57. },
  58. 'label-value': {
  59. pattern: /(["'`])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
  60. greedy: true,
  61. alias: 'attr-value',
  62. },
  63. 'punctuation': /\{|\}|=~?|![=~]|,/,
  64. },
  65. },
  66. 'context-range': [
  67. {
  68. pattern: /\[[\w\s:]+\]/, // [1m]
  69. inside: {
  70. 'punctuation': /\[|\]|:/,
  71. 'range-duration': {
  72. pattern: /\b(?:\d+(?:[smhdwy]|ms))+\b/i,
  73. alias: 'number',
  74. },
  75. },
  76. },
  77. {
  78. pattern: /(\boffset\s+)\w+/, // offset 1m
  79. lookbehind: true,
  80. inside: {
  81. 'range-duration': {
  82. pattern: /\b(?:\d+(?:[smhdwy]|ms))+\b/i,
  83. alias: 'number',
  84. },
  85. },
  86. },
  87. ],
  88. 'keyword': new RegExp('\\b(?:' + keywords.join('|') + ')\\b', 'i'),
  89. 'function': /\b[a-z_]\w*(?=\s*\()/i,
  90. 'number': /[-+]?(?:(?:\b\d+(?:\.\d+)?|\B\.\d+)(?:e[-+]?\d+)?\b|\b(?:0x[0-9a-f]+|nan|inf)\b)/i,
  91. 'operator': /[\^*/%+-]|==|!=|<=|<|>=|>|\b(?:and|or|unless)\b/i,
  92. 'punctuation': /[{};()`,.[\]]/,
  93. };
  94. }(Prism));