datasource.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import kbn from 'app/core/utils/kbn';
  4. import * as dateMath from 'app/core/utils/datemath';
  5. import PrometheusMetricFindQuery from './metric_find_query';
  6. import { ResultTransformer } from './result_transformer';
  7. export function prometheusRegularEscape(value) {
  8. return value.replace(/'/g, "\\\\'");
  9. }
  10. export function prometheusSpecialRegexEscape(value) {
  11. return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
  12. }
  13. export class PrometheusDatasource {
  14. type: string;
  15. editorSrc: string;
  16. name: string;
  17. supportMetrics: boolean;
  18. url: string;
  19. directUrl: string;
  20. basicAuth: any;
  21. withCredentials: any;
  22. metricsNameCache: any;
  23. interval: string;
  24. httpMethod: string;
  25. resultTransformer: ResultTransformer;
  26. /** @ngInject */
  27. constructor(instanceSettings, private $q, private backendSrv, private templateSrv, private timeSrv) {
  28. this.type = 'prometheus';
  29. this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
  30. this.name = instanceSettings.name;
  31. this.supportMetrics = true;
  32. this.url = instanceSettings.url;
  33. this.directUrl = instanceSettings.directUrl;
  34. this.basicAuth = instanceSettings.basicAuth;
  35. this.withCredentials = instanceSettings.withCredentials;
  36. this.interval = instanceSettings.jsonData.timeInterval || '15s';
  37. this.httpMethod = instanceSettings.jsonData.httpMethod || 'GET';
  38. this.resultTransformer = new ResultTransformer(templateSrv);
  39. }
  40. _request(method, url, data?, requestId?) {
  41. var options: any = {
  42. url: this.url + url,
  43. method: method,
  44. requestId: requestId,
  45. };
  46. if (method === 'GET') {
  47. if (!_.isEmpty(data)) {
  48. options.url =
  49. options.url +
  50. '?' +
  51. _.map(data, (v, k) => {
  52. return encodeURIComponent(k) + '=' + encodeURIComponent(v);
  53. }).join('&');
  54. }
  55. } else {
  56. options.headers = {
  57. 'Content-Type': 'application/x-www-form-urlencoded',
  58. };
  59. options.transformRequest = data => {
  60. return $.param(data);
  61. };
  62. options.data = data;
  63. }
  64. if (this.basicAuth || this.withCredentials) {
  65. options.withCredentials = true;
  66. }
  67. if (this.basicAuth) {
  68. options.headers = {
  69. Authorization: this.basicAuth,
  70. };
  71. }
  72. return this.backendSrv.datasourceRequest(options);
  73. }
  74. // Use this for tab completion features, wont publish response to other components
  75. helperRequest(url) {
  76. const options: any = {
  77. url: this.url + url,
  78. silent: true,
  79. };
  80. if (this.basicAuth || this.withCredentials) {
  81. options.withCredentials = true;
  82. }
  83. return this.backendSrv.datasourceRequest(options);
  84. }
  85. interpolateQueryExpr(value, variable, defaultFormatFn) {
  86. // if no multi or include all do not regexEscape
  87. if (!variable.multi && !variable.includeAll) {
  88. return prometheusRegularEscape(value);
  89. }
  90. if (typeof value === 'string') {
  91. return prometheusSpecialRegexEscape(value);
  92. }
  93. var escapedValues = _.map(value, prometheusSpecialRegexEscape);
  94. return escapedValues.join('|');
  95. }
  96. targetContainsTemplate(target) {
  97. return this.templateSrv.variableExists(target.expr);
  98. }
  99. query(options) {
  100. var start = this.getPrometheusTime(options.range.from, false);
  101. var end = this.getPrometheusTime(options.range.to, true);
  102. var range = Math.ceil(end - start);
  103. var queries = [];
  104. var activeTargets = [];
  105. options = _.clone(options);
  106. for (let target of options.targets) {
  107. if (!target.expr || target.hide) {
  108. continue;
  109. }
  110. activeTargets.push(target);
  111. queries.push(this.createQuery(target, options, range));
  112. }
  113. // No valid targets, return the empty result to save a round trip.
  114. if (_.isEmpty(queries)) {
  115. return this.$q.when({ data: [] });
  116. }
  117. var allQueryPromise = _.map(queries, query => {
  118. if (!query.instant) {
  119. return this.performTimeSeriesQuery(query, start, end);
  120. } else {
  121. return this.performInstantQuery(query, end);
  122. }
  123. });
  124. return this.$q.all(allQueryPromise).then(responseList => {
  125. let result = [];
  126. _.each(responseList, (response, index) => {
  127. if (response.status === 'error') {
  128. throw response.error;
  129. }
  130. let transformerOptions = {
  131. format: activeTargets[index].format,
  132. step: queries[index].step,
  133. legendFormat: activeTargets[index].legendFormat,
  134. start: start,
  135. end: end,
  136. responseListLength: responseList.length,
  137. responseIndex: index,
  138. };
  139. this.resultTransformer.transform(result, response, transformerOptions);
  140. });
  141. return { data: result };
  142. });
  143. }
  144. createQuery(target, options, range) {
  145. var query: any = {};
  146. query.instant = target.instant;
  147. var interval = kbn.interval_to_seconds(options.interval);
  148. // Minimum interval ("Min step"), if specified for the query. or same as interval otherwise
  149. var minInterval = kbn.interval_to_seconds(
  150. this.templateSrv.replace(target.interval, options.scopedVars) || options.interval
  151. );
  152. var intervalFactor = target.intervalFactor || 1;
  153. // Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits
  154. var adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor);
  155. var scopedVars = options.scopedVars;
  156. // If the interval was adjusted, make a shallow copy of scopedVars with updated interval vars
  157. if (interval !== adjustedInterval) {
  158. interval = adjustedInterval;
  159. scopedVars = Object.assign({}, options.scopedVars, {
  160. __interval: { text: interval + 's', value: interval + 's' },
  161. __interval_ms: { text: interval * 1000, value: interval * 1000 },
  162. });
  163. }
  164. query.step = interval;
  165. // Only replace vars in expression after having (possibly) updated interval vars
  166. query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr);
  167. query.requestId = options.panelId + target.refId;
  168. return query;
  169. }
  170. adjustInterval(interval, minInterval, range, intervalFactor) {
  171. // Prometheus will drop queries that might return more than 11000 data points.
  172. // Calibrate interval if it is too small.
  173. if (interval !== 0 && range / intervalFactor / interval > 11000) {
  174. interval = Math.ceil(range / intervalFactor / 11000);
  175. }
  176. return Math.max(interval * intervalFactor, minInterval, 1);
  177. }
  178. performTimeSeriesQuery(query, start, end) {
  179. if (start > end) {
  180. throw { message: 'Invalid time range' };
  181. }
  182. var url = '/api/v1/query_range';
  183. var data = {
  184. query: query.expr,
  185. start: start,
  186. end: end,
  187. step: query.step,
  188. };
  189. return this._request(this.httpMethod, url, data, query.requestId);
  190. }
  191. performInstantQuery(query, time) {
  192. var url = '/api/v1/query';
  193. var data = {
  194. query: query.expr,
  195. time: time,
  196. };
  197. return this._request(this.httpMethod, url, data, query.requestId);
  198. }
  199. performSuggestQuery(query, cache = false) {
  200. var url = '/api/v1/label/__name__/values';
  201. if (cache && this.metricsNameCache && this.metricsNameCache.expire > Date.now()) {
  202. return this.$q.when(
  203. _.filter(this.metricsNameCache.data, metricName => {
  204. return metricName.indexOf(query) !== 1;
  205. })
  206. );
  207. }
  208. return this.helperRequest(url).then(result => {
  209. this.metricsNameCache = {
  210. data: result.data.data,
  211. expire: Date.now() + 60 * 1000,
  212. };
  213. return _.filter(result.data.data, metricName => {
  214. return metricName.indexOf(query) !== 1;
  215. });
  216. });
  217. }
  218. metricFindQuery(query) {
  219. if (!query) {
  220. return this.$q.when([]);
  221. }
  222. let interpolated = this.templateSrv.replace(query, {}, this.interpolateQueryExpr);
  223. var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, this.timeSrv);
  224. return metricFindQuery.process();
  225. }
  226. annotationQuery(options) {
  227. var annotation = options.annotation;
  228. var expr = annotation.expr || '';
  229. var tagKeys = annotation.tagKeys || '';
  230. var titleFormat = annotation.titleFormat || '';
  231. var textFormat = annotation.textFormat || '';
  232. if (!expr) {
  233. return this.$q.when([]);
  234. }
  235. var interpolated = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr);
  236. var step = '60s';
  237. if (annotation.step) {
  238. step = this.templateSrv.replace(annotation.step);
  239. }
  240. var start = this.getPrometheusTime(options.range.from, false);
  241. var end = this.getPrometheusTime(options.range.to, true);
  242. var query = {
  243. expr: interpolated,
  244. step: this.adjustInterval(kbn.interval_to_seconds(step), 0, Math.ceil(end - start), 1) + 's',
  245. };
  246. var self = this;
  247. return this.performTimeSeriesQuery(query, start, end).then(function(results) {
  248. var eventList = [];
  249. tagKeys = tagKeys.split(',');
  250. _.each(results.data.data.result, function(series) {
  251. var tags = _.chain(series.metric)
  252. .filter(function(v, k) {
  253. return _.includes(tagKeys, k);
  254. })
  255. .value();
  256. for (let value of series.values) {
  257. if (value[1] === '1') {
  258. var event = {
  259. annotation: annotation,
  260. time: Math.floor(parseFloat(value[0])) * 1000,
  261. title: self.resultTransformer.renderTemplate(titleFormat, series.metric),
  262. tags: tags,
  263. text: self.resultTransformer.renderTemplate(textFormat, series.metric),
  264. };
  265. eventList.push(event);
  266. }
  267. }
  268. });
  269. return eventList;
  270. });
  271. }
  272. testDatasource() {
  273. let now = new Date().getTime();
  274. return this.performInstantQuery({ expr: '1+1' }, now / 1000).then(response => {
  275. if (response.data.status === 'success') {
  276. return { status: 'success', message: 'Data source is working' };
  277. } else {
  278. return { status: 'error', message: response.error };
  279. }
  280. });
  281. }
  282. getPrometheusTime(date, roundUp) {
  283. if (_.isString(date)) {
  284. date = dateMath.parse(date, roundUp);
  285. }
  286. return Math.ceil(date.valueOf() / 1000);
  287. }
  288. }