metric_find_query.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var start = this.datasource.getPrometheusTime(this.range.from, false);
  46. var end = this.datasource.getPrometheusTime(this.range.to, true);
  47. url = '/api/v1/series?match[]=' + encodeURIComponent(metric)
  48. + '&start=' + start
  49. + '&end=' + end;
  50. return this.datasource._request('GET', url)
  51. .then(function(result) {
  52. return _.map(result.data.data, function(metric) {
  53. return {
  54. text: metric[label],
  55. expandable: true
  56. };
  57. });
  58. });
  59. }
  60. };
  61. PrometheusMetricFindQuery.prototype.metricNameQuery = function(metricFilterPattern) {
  62. var url = '/api/v1/label/__name__/values';
  63. return this.datasource._request('GET', url)
  64. .then(function(result) {
  65. return _.chain(result.data.data)
  66. .filter(function(metricName) {
  67. var r = new RegExp(metricFilterPattern);
  68. return r.test(metricName);
  69. })
  70. .map(function(matchedMetricName) {
  71. return {
  72. text: matchedMetricName,
  73. expandable: true
  74. };
  75. })
  76. .value();
  77. });
  78. };
  79. PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) {
  80. var end = this.datasource.getPrometheusTime(this.range.to, true);
  81. var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + end;
  82. return this.datasource._request('GET', url)
  83. .then(function(result) {
  84. return _.map(result.data.data.result, function(metricData) {
  85. var text = metricData.metric.__name__ || '';
  86. delete metricData.metric.__name__;
  87. text += '{' +
  88. _.map(metricData.metric, function(v, k) { return k + '="' + v + '"'; }).join(',') +
  89. '}';
  90. text += ' ' + metricData.value[1] + ' ' + metricData.value[0] * 1000;
  91. return {
  92. text: text,
  93. expandable: true
  94. };
  95. });
  96. });
  97. };
  98. PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) {
  99. var start = this.datasource.getPrometheusTime(this.range.from, false);
  100. var end = this.datasource.getPrometheusTime(this.range.to, true);
  101. var url = '/api/v1/series?match[]=' + encodeURIComponent(query)
  102. + '&start=' + start
  103. + '&end=' + end;
  104. var self = this;
  105. return this.datasource._request('GET', url)
  106. .then(function(result) {
  107. return _.map(result.data.data, function(metric) {
  108. return {
  109. text: self.datasource.getOriginalMetricName(metric),
  110. expandable: true
  111. };
  112. });
  113. });
  114. };
  115. return PrometheusMetricFindQuery;
  116. });