metric_find_query.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import _ from "lodash";
  2. export default class PrometheusMetricFindQuery {
  3. datasource: any;
  4. query: any;
  5. range: any;
  6. constructor(datasource, query, timeSrv) {
  7. this.datasource = datasource;
  8. this.query = query;
  9. this.range = timeSrv.timeRange();
  10. }
  11. process() {
  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(
  19. label_values_query[2],
  20. label_values_query[1]
  21. );
  22. } else {
  23. return this.labelValuesQuery(label_values_query[2], null);
  24. }
  25. }
  26. var metric_names_query = this.query.match(metric_names_regex);
  27. if (metric_names_query) {
  28. return this.metricNameQuery(metric_names_query[1]);
  29. }
  30. var query_result_query = this.query.match(query_result_regex);
  31. if (query_result_query) {
  32. return this.queryResultQuery(query_result_query[1]);
  33. }
  34. // if query contains full metric name, return metric name and label list
  35. return this.metricNameAndLabelsQuery(this.query);
  36. }
  37. labelValuesQuery(label, metric) {
  38. var url;
  39. if (!metric) {
  40. // return label values globally
  41. url = "/api/v1/label/" + label + "/values";
  42. return this.datasource._request("GET", url).then(function(result) {
  43. return _.map(result.data.data, function(value) {
  44. return { text: value };
  45. });
  46. });
  47. } else {
  48. var start = this.datasource.getPrometheusTime(this.range.from, false);
  49. var end = this.datasource.getPrometheusTime(this.range.to, true);
  50. url =
  51. "/api/v1/series?match[]=" +
  52. encodeURIComponent(metric) +
  53. "&start=" +
  54. start +
  55. "&end=" +
  56. end;
  57. return this.datasource._request("GET", url).then(function(result) {
  58. var _labels = _.map(result.data.data, function(metric) {
  59. return metric[label] || '';
  60. }).filter(function(label) { return label !== ''; });
  61. return _.uniq(_labels).map(function(metric) {
  62. return {
  63. text: metric,
  64. expandable: true
  65. };
  66. });
  67. });
  68. }
  69. }
  70. metricNameQuery(metricFilterPattern) {
  71. var url = "/api/v1/label/__name__/values";
  72. return this.datasource._request("GET", url).then(function(result) {
  73. return _.chain(result.data.data)
  74. .filter(function(metricName) {
  75. var r = new RegExp(metricFilterPattern);
  76. return r.test(metricName);
  77. })
  78. .map(function(matchedMetricName) {
  79. return {
  80. text: matchedMetricName,
  81. expandable: true
  82. };
  83. })
  84. .value();
  85. });
  86. }
  87. queryResultQuery(query) {
  88. var end = this.datasource.getPrometheusTime(this.range.to, true);
  89. return this.datasource
  90. .performInstantQuery({ expr: query }, end)
  91. .then(function(result) {
  92. return _.map(result.data.data.result, function(metricData) {
  93. var text = metricData.metric.__name__ || "";
  94. delete metricData.metric.__name__;
  95. text +=
  96. "{" +
  97. _.map(metricData.metric, function(v, k) {
  98. return k + '="' + v + '"';
  99. }).join(",") +
  100. "}";
  101. text += " " + metricData.value[1] + " " + metricData.value[0] * 1000;
  102. return {
  103. text: text,
  104. expandable: true
  105. };
  106. });
  107. });
  108. }
  109. metricNameAndLabelsQuery(query) {
  110. var start = this.datasource.getPrometheusTime(this.range.from, false);
  111. var end = this.datasource.getPrometheusTime(this.range.to, true);
  112. var url =
  113. "/api/v1/series?match[]=" +
  114. encodeURIComponent(query) +
  115. "&start=" +
  116. start +
  117. "&end=" +
  118. end;
  119. var self = this;
  120. return this.datasource._request("GET", url).then(function(result) {
  121. return _.map(result.data.data, function(metric) {
  122. return {
  123. text: self.datasource.getOriginalMetricName(metric),
  124. expandable: true
  125. };
  126. });
  127. });
  128. }
  129. }