datasource.ts 11 KB

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