metric_find_query.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. define([
  2. 'lodash'
  3. ],
  4. function (_) {
  5. 'use strict';
  6. function PrometheusMetricFindQuery(datasource, query, timeSrv) {
  7. this.datasource = datasource;
  8. this.query = query;
  9. this.range = timeSrv.timeRange();
  10. }
  11. PrometheusMetricFindQuery.prototype.process = function() {
  12. var label_values_regex = /^label_values\((?:(.+),\s*)?([a-zA-Z_][a-zA-Z0-9_]+)\)$/;
  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[1]) {
  18. return this.labelValuesQuery(label_values_query[2], label_values_query[1]);
  19. } else {
  20. return this.labelValuesQuery(label_values_query[2], 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. + '&start=' + (this.range.from.valueOf() / 1000)
  47. + '&end=' + (this.range.to.valueOf() / 1000);
  48. return this.datasource._request('GET', url)
  49. .then(function(result) {
  50. return _.map(result.data.data, function(metric) {
  51. return {
  52. text: metric[label],
  53. expandable: true
  54. };
  55. });
  56. });
  57. }
  58. };
  59. PrometheusMetricFindQuery.prototype.metricNameQuery = function(metricFilterPattern) {
  60. var url = '/api/v1/label/__name__/values';
  61. return this.datasource._request('GET', url)
  62. .then(function(result) {
  63. return _.chain(result.data.data)
  64. .filter(function(metricName) {
  65. var r = new RegExp(metricFilterPattern);
  66. return r.test(metricName);
  67. })
  68. .map(function(matchedMetricName) {
  69. return {
  70. text: matchedMetricName,
  71. expandable: true
  72. };
  73. })
  74. .value();
  75. });
  76. };
  77. PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) {
  78. var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (this.range.to.valueOf() / 1000);
  79. return this.datasource._request('GET', url)
  80. .then(function(result) {
  81. return _.map(result.data.data.result, function(metricData) {
  82. var text = metricData.metric.__name__ || '';
  83. delete metricData.metric.__name__;
  84. text += '{' +
  85. _.map(metricData.metric, function(v, k) { return k + '="' + v + '"'; }).join(',') +
  86. '}';
  87. text += ' ' + metricData.value[1] + ' ' + metricData.value[0] * 1000;
  88. return {
  89. text: text,
  90. expandable: true
  91. };
  92. });
  93. });
  94. };
  95. PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) {
  96. var url = '/api/v1/series?match[]=' + encodeURIComponent(query)
  97. + '&start=' + (this.range.from.valueOf() / 1000)
  98. + '&end=' + (this.range.to.valueOf() / 1000);
  99. var self = this;
  100. return this.datasource._request('GET', url)
  101. .then(function(result) {
  102. return _.map(result.data.data, function(metric) {
  103. return {
  104. text: self.datasource.getOriginalMetricName(metric),
  105. expandable: true
  106. };
  107. });
  108. });
  109. };
  110. return PrometheusMetricFindQuery;
  111. });