metric_find_query.js 4.2 KB

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