metric_find_query.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. define([
  2. 'lodash',
  3. 'moment',
  4. ],
  5. function (_, moment) {
  6. 'use strict';
  7. function PrometheusMetricFindQuery(datasource, query) {
  8. this.datasource = datasource;
  9. this.query = query;
  10. }
  11. PrometheusMetricFindQuery.prototype.process = function() {
  12. var label_values_regex = /^label_values\(([^,]+)(?:,\s*(.+))?\)$/;
  13. var metric_names_regex = /^metrics\((.+)\)$/;
  14. var query_result_regex = /^query_result\((.+)\)$/;
  15. var label_values_query = this.query.match(label_values_regex);
  16. if (label_values_query) {
  17. if (label_values_query[2]) {
  18. return this.labelValuesQuery(label_values_query[2], label_values_query[1]);
  19. } else {
  20. return this.labelValuesQuery(label_values_query[1], null);
  21. }
  22. }
  23. var metric_names_query = this.query.match(metric_names_regex);
  24. if (metric_names_query) {
  25. return this.metricNameQuery(metric_names_query[1]);
  26. }
  27. var query_result_query = this.query.match(query_result_regex);
  28. if (query_result_query) {
  29. return this.queryResultQuery(query_result_query[1]);
  30. }
  31. // if query contains full metric name, return metric name and label list
  32. return this.metricNameAndLabelsQuery(this.query);
  33. };
  34. PrometheusMetricFindQuery.prototype.labelValuesQuery = function(label, metric) {
  35. var url;
  36. if (!metric) {
  37. // return label values globally
  38. url = '/api/v1/label/' + label + '/values';
  39. return this.datasource._request('GET', url).then(function(result) {
  40. return _.map(result.data.data, function(value) {
  41. return {text: value};
  42. });
  43. });
  44. } else {
  45. url = '/api/v1/series?match[]=' + encodeURIComponent(metric);
  46. return this.datasource._request('GET', url)
  47. .then(function(result) {
  48. return _.map(result.data.data, function(metric) {
  49. return {
  50. text: metric[label],
  51. expandable: true
  52. };
  53. });
  54. });
  55. }
  56. };
  57. PrometheusMetricFindQuery.prototype.metricNameQuery = function(metricFilterPattern) {
  58. var url = '/api/v1/label/__name__/values';
  59. return this.datasource._request('GET', url)
  60. .then(function(result) {
  61. return _.chain(result.data.data)
  62. .filter(function(metricName) {
  63. var r = new RegExp(metricFilterPattern);
  64. return r.test(metricName);
  65. })
  66. .map(function(matchedMetricName) {
  67. return {
  68. text: matchedMetricName,
  69. expandable: true
  70. };
  71. })
  72. .value();
  73. });
  74. };
  75. PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) {
  76. var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (moment().valueOf() / 1000);
  77. return this.datasource._request('GET', url)
  78. .then(function(result) {
  79. return _.map(result.data.data.result, function(metricData) {
  80. var text = metricData.metric.__name__ || '';
  81. delete metricData.metric.__name__;
  82. text += '{' +
  83. _.map(metricData.metric, function(v, k) { return k + '="' + v + '"'; }).join(',') +
  84. '}';
  85. text += ' ' + metricData.value[1] + ' ' + metricData.value[0] * 1000;
  86. return {
  87. text: text,
  88. expandable: true
  89. };
  90. });
  91. });
  92. };
  93. PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) {
  94. var url = '/api/v1/series?match[]=' + encodeURIComponent(query);
  95. var self = this;
  96. return this.datasource._request('GET', url)
  97. .then(function(result) {
  98. return _.map(result.data.data, function(metric) {
  99. return {
  100. text: self.datasource.getOriginalMetricName(metric),
  101. expandable: true
  102. };
  103. });
  104. });
  105. };
  106. return PrometheusMetricFindQuery;
  107. });