datasource.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import _ from 'lodash';
  2. import * as dateMath from 'app/core/utils/datemath';
  3. import { isVersionGtOrEq, SemVersion } from 'app/core/utils/version';
  4. import gfunc from './gfunc';
  5. /** @ngInject */
  6. export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv) {
  7. this.basicAuth = instanceSettings.basicAuth;
  8. this.url = instanceSettings.url;
  9. this.name = instanceSettings.name;
  10. this.graphiteVersion = instanceSettings.jsonData.graphiteVersion || '0.9';
  11. this.supportsTags = supportsTags(this.graphiteVersion);
  12. this.cacheTimeout = instanceSettings.cacheTimeout;
  13. this.withCredentials = instanceSettings.withCredentials;
  14. this.render_method = instanceSettings.render_method || 'POST';
  15. this.funcDefs = null;
  16. this.funcDefsPromise = null;
  17. this.getQueryOptionsInfo = function() {
  18. return {
  19. maxDataPoints: true,
  20. cacheTimeout: true,
  21. links: [
  22. {
  23. text: 'Help',
  24. url: 'http://docs.grafana.org/features/datasources/graphite/#using-graphite-in-grafana',
  25. },
  26. ],
  27. };
  28. };
  29. this.query = function(options) {
  30. var graphOptions = {
  31. from: this.translateTime(options.rangeRaw.from, false),
  32. until: this.translateTime(options.rangeRaw.to, true),
  33. targets: options.targets,
  34. format: options.format,
  35. cacheTimeout: options.cacheTimeout || this.cacheTimeout,
  36. maxDataPoints: options.maxDataPoints,
  37. };
  38. var params = this.buildGraphiteParams(graphOptions, options.scopedVars);
  39. if (params.length === 0) {
  40. return $q.when({ data: [] });
  41. }
  42. var httpOptions: any = {
  43. method: 'POST',
  44. url: '/render',
  45. data: params.join('&'),
  46. headers: {
  47. 'Content-Type': 'application/x-www-form-urlencoded',
  48. 'X-Dashboard-Id': options.dashboardId, // enables distributed tracing in ds_proxy
  49. 'X-Panel-Id': options.panelId, // enables distributed tracing in ds_proxy
  50. },
  51. };
  52. if (options.panelId) {
  53. httpOptions.requestId = this.name + '.panelId.' + options.panelId;
  54. }
  55. return this.doGraphiteRequest(httpOptions).then(this.convertDataPointsToMs);
  56. };
  57. this.convertDataPointsToMs = function(result) {
  58. if (!result || !result.data) {
  59. return [];
  60. }
  61. for (var i = 0; i < result.data.length; i++) {
  62. var series = result.data[i];
  63. for (var y = 0; y < series.datapoints.length; y++) {
  64. series.datapoints[y][1] *= 1000;
  65. }
  66. }
  67. return result;
  68. };
  69. this.parseTags = function(tagString) {
  70. let tags = [];
  71. tags = tagString.split(',');
  72. if (tags.length === 1) {
  73. tags = tagString.split(' ');
  74. if (tags[0] === '') {
  75. tags = [];
  76. }
  77. }
  78. return tags;
  79. };
  80. this.annotationQuery = function(options) {
  81. // Graphite metric as annotation
  82. if (options.annotation.target) {
  83. var target = templateSrv.replace(options.annotation.target, {}, 'glob');
  84. var graphiteQuery = {
  85. rangeRaw: options.rangeRaw,
  86. targets: [{ target: target }],
  87. format: 'json',
  88. maxDataPoints: 100,
  89. };
  90. return this.query(graphiteQuery).then(function(result) {
  91. var list = [];
  92. for (var i = 0; i < result.data.length; i++) {
  93. var target = result.data[i];
  94. for (var y = 0; y < target.datapoints.length; y++) {
  95. var datapoint = target.datapoints[y];
  96. if (!datapoint[0]) {
  97. continue;
  98. }
  99. list.push({
  100. annotation: options.annotation,
  101. time: datapoint[1],
  102. title: target.target,
  103. });
  104. }
  105. }
  106. return list;
  107. });
  108. } else {
  109. // Graphite event as annotation
  110. var tags = templateSrv.replace(options.annotation.tags);
  111. return this.events({ range: options.rangeRaw, tags: tags }).then(results => {
  112. var list = [];
  113. for (var i = 0; i < results.data.length; i++) {
  114. var e = results.data[i];
  115. var tags = e.tags;
  116. if (_.isString(e.tags)) {
  117. tags = this.parseTags(e.tags);
  118. }
  119. list.push({
  120. annotation: options.annotation,
  121. time: e.when * 1000,
  122. title: e.what,
  123. tags: tags,
  124. text: e.data,
  125. });
  126. }
  127. return list;
  128. });
  129. }
  130. };
  131. this.events = function(options) {
  132. try {
  133. var tags = '';
  134. if (options.tags) {
  135. tags = '&tags=' + options.tags;
  136. }
  137. return this.doGraphiteRequest({
  138. method: 'GET',
  139. url:
  140. '/events/get_data?from=' +
  141. this.translateTime(options.range.from, false) +
  142. '&until=' +
  143. this.translateTime(options.range.to, true) +
  144. tags,
  145. });
  146. } catch (err) {
  147. return $q.reject(err);
  148. }
  149. };
  150. this.targetContainsTemplate = function(target) {
  151. return templateSrv.variableExists(target.target);
  152. };
  153. this.translateTime = function(date, roundUp) {
  154. if (_.isString(date)) {
  155. if (date === 'now') {
  156. return 'now';
  157. } else if (date.indexOf('now-') >= 0 && date.indexOf('/') === -1) {
  158. date = date.substring(3);
  159. date = date.replace('m', 'min');
  160. date = date.replace('M', 'mon');
  161. return date;
  162. }
  163. date = dateMath.parse(date, roundUp);
  164. }
  165. // graphite' s from filter is exclusive
  166. // here we step back one minute in order
  167. // to guarantee that we get all the data that
  168. // exists for the specified range
  169. if (roundUp) {
  170. if (date.get('s')) {
  171. date.add(1, 'm');
  172. }
  173. } else if (roundUp === false) {
  174. if (date.get('s')) {
  175. date.subtract(1, 'm');
  176. }
  177. }
  178. return date.unix();
  179. };
  180. this.metricFindQuery = function(query, optionalOptions) {
  181. let options = optionalOptions || {};
  182. let interpolatedQuery = templateSrv.replace(query);
  183. // special handling for tag_values(<tag>[,<expression>]*), this is used for template variables
  184. let matches = interpolatedQuery.match(/^tag_values\(([^,]+)((, *[^,]+)*)\)$/);
  185. if (matches) {
  186. const expressions = [];
  187. const exprRegex = /, *([^,]+)/g;
  188. let match;
  189. while ((match = exprRegex.exec(matches[2])) !== null) {
  190. expressions.push(match[1]);
  191. }
  192. options.limit = 10000;
  193. return this.getTagValuesAutoComplete(expressions, matches[1], undefined, options);
  194. }
  195. // special handling for tags(<expression>[,<expression>]*), this is used for template variables
  196. matches = interpolatedQuery.match(/^tags\(([^,]*)((, *[^,]+)*)\)$/);
  197. if (matches) {
  198. const expressions = [];
  199. if (matches[1]) {
  200. expressions.push(matches[1]);
  201. const exprRegex = /, *([^,]+)/g;
  202. let match;
  203. while ((match = exprRegex.exec(matches[2])) !== null) {
  204. expressions.push(match[1]);
  205. }
  206. }
  207. options.limit = 10000;
  208. return this.getTagsAutoComplete(expressions, undefined, options);
  209. }
  210. let httpOptions: any = {
  211. method: 'GET',
  212. url: '/metrics/find',
  213. params: {
  214. query: interpolatedQuery,
  215. },
  216. // for cancellations
  217. requestId: options.requestId,
  218. };
  219. if (options.range) {
  220. httpOptions.params.from = this.translateTime(options.range.from, false);
  221. httpOptions.params.until = this.translateTime(options.range.to, true);
  222. }
  223. return this.doGraphiteRequest(httpOptions).then(results => {
  224. return _.map(results.data, metric => {
  225. return {
  226. text: metric.text,
  227. expandable: metric.expandable ? true : false,
  228. };
  229. });
  230. });
  231. };
  232. this.getTags = function(optionalOptions) {
  233. let options = optionalOptions || {};
  234. let httpOptions: any = {
  235. method: 'GET',
  236. url: '/tags',
  237. // for cancellations
  238. requestId: options.requestId,
  239. };
  240. if (options.range) {
  241. httpOptions.params.from = this.translateTime(options.range.from, false);
  242. httpOptions.params.until = this.translateTime(options.range.to, true);
  243. }
  244. return this.doGraphiteRequest(httpOptions).then(results => {
  245. return _.map(results.data, tag => {
  246. return {
  247. text: tag.tag,
  248. id: tag.id,
  249. };
  250. });
  251. });
  252. };
  253. this.getTagValues = function(tag, optionalOptions) {
  254. let options = optionalOptions || {};
  255. let httpOptions: any = {
  256. method: 'GET',
  257. url: '/tags/' + templateSrv.replace(tag),
  258. // for cancellations
  259. requestId: options.requestId,
  260. };
  261. if (options.range) {
  262. httpOptions.params.from = this.translateTime(options.range.from, false);
  263. httpOptions.params.until = this.translateTime(options.range.to, true);
  264. }
  265. return this.doGraphiteRequest(httpOptions).then(results => {
  266. if (results.data && results.data.values) {
  267. return _.map(results.data.values, value => {
  268. return {
  269. text: value.value,
  270. id: value.id,
  271. };
  272. });
  273. } else {
  274. return [];
  275. }
  276. });
  277. };
  278. this.getTagsAutoComplete = (expressions, tagPrefix, optionalOptions) => {
  279. let options = optionalOptions || {};
  280. let httpOptions: any = {
  281. method: 'GET',
  282. url: '/tags/autoComplete/tags',
  283. params: {
  284. expr: _.map(expressions, expression => templateSrv.replace((expression || '').trim())),
  285. },
  286. // for cancellations
  287. requestId: options.requestId,
  288. };
  289. if (tagPrefix) {
  290. httpOptions.params.tagPrefix = tagPrefix;
  291. }
  292. if (options.limit) {
  293. httpOptions.params.limit = options.limit;
  294. }
  295. if (options.range) {
  296. httpOptions.params.from = this.translateTime(options.range.from, false);
  297. httpOptions.params.until = this.translateTime(options.range.to, true);
  298. }
  299. return this.doGraphiteRequest(httpOptions).then(results => {
  300. if (results.data) {
  301. return _.map(results.data, tag => {
  302. return { text: tag };
  303. });
  304. } else {
  305. return [];
  306. }
  307. });
  308. };
  309. this.getTagValuesAutoComplete = (expressions, tag, valuePrefix, optionalOptions) => {
  310. let options = optionalOptions || {};
  311. let httpOptions: any = {
  312. method: 'GET',
  313. url: '/tags/autoComplete/values',
  314. params: {
  315. expr: _.map(expressions, expression => templateSrv.replace((expression || '').trim())),
  316. tag: templateSrv.replace((tag || '').trim()),
  317. },
  318. // for cancellations
  319. requestId: options.requestId,
  320. };
  321. if (valuePrefix) {
  322. httpOptions.params.valuePrefix = valuePrefix;
  323. }
  324. if (options.limit) {
  325. httpOptions.params.limit = options.limit;
  326. }
  327. if (options.range) {
  328. httpOptions.params.from = this.translateTime(options.range.from, false);
  329. httpOptions.params.until = this.translateTime(options.range.to, true);
  330. }
  331. return this.doGraphiteRequest(httpOptions).then(results => {
  332. if (results.data) {
  333. return _.map(results.data, value => {
  334. return { text: value };
  335. });
  336. } else {
  337. return [];
  338. }
  339. });
  340. };
  341. this.getVersion = function(optionalOptions) {
  342. let options = optionalOptions || {};
  343. let httpOptions = {
  344. method: 'GET',
  345. url: '/version',
  346. requestId: options.requestId,
  347. };
  348. return this.doGraphiteRequest(httpOptions)
  349. .then(results => {
  350. if (results.data) {
  351. let semver = new SemVersion(results.data);
  352. return semver.isValid() ? results.data : '';
  353. }
  354. return '';
  355. })
  356. .catch(() => {
  357. return '';
  358. });
  359. };
  360. this.createFuncInstance = function(funcDef, options?) {
  361. return gfunc.createFuncInstance(funcDef, options, this.funcDefs);
  362. };
  363. this.getFuncDef = function(name) {
  364. return gfunc.getFuncDef(name, this.funcDefs);
  365. };
  366. this.waitForFuncDefsLoaded = function() {
  367. return this.getFuncDefs();
  368. };
  369. this.getFuncDefs = function() {
  370. if (this.funcDefsPromise !== null) {
  371. return this.funcDefsPromise;
  372. }
  373. if (!supportsFunctionIndex(this.graphiteVersion)) {
  374. this.funcDefs = gfunc.getFuncDefs(this.graphiteVersion);
  375. this.funcDefsPromise = Promise.resolve(this.funcDefs);
  376. return this.funcDefsPromise;
  377. }
  378. let httpOptions = {
  379. method: 'GET',
  380. url: '/functions',
  381. };
  382. this.funcDefsPromise = this.doGraphiteRequest(httpOptions)
  383. .then(results => {
  384. if (results.status !== 200 || typeof results.data !== 'object') {
  385. this.funcDefs = gfunc.getFuncDefs(this.graphiteVersion);
  386. } else {
  387. this.funcDefs = gfunc.parseFuncDefs(results.data);
  388. }
  389. return this.funcDefs;
  390. })
  391. .catch(err => {
  392. console.log('Fetching graphite functions error', err);
  393. this.funcDefs = gfunc.getFuncDefs(this.graphiteVersion);
  394. return this.funcDefs;
  395. });
  396. return this.funcDefsPromise;
  397. };
  398. this.testDatasource = function() {
  399. return this.metricFindQuery('*').then(function() {
  400. return { status: 'success', message: 'Data source is working' };
  401. });
  402. };
  403. this.doGraphiteRequest = function(options) {
  404. if (this.basicAuth || this.withCredentials) {
  405. options.withCredentials = true;
  406. }
  407. if (this.basicAuth) {
  408. options.headers = options.headers || {};
  409. options.headers.Authorization = this.basicAuth;
  410. }
  411. options.url = this.url + options.url;
  412. options.inspect = { type: 'graphite' };
  413. return backendSrv.datasourceRequest(options);
  414. };
  415. this._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  416. this.buildGraphiteParams = function(options, scopedVars) {
  417. var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
  418. var clean_options = [],
  419. targets = {};
  420. var target, targetValue, i;
  421. var regex = /\#([A-Z])/g;
  422. var intervalFormatFixRegex = /'(\d+)m'/gi;
  423. var hasTargets = false;
  424. options['format'] = 'json';
  425. function fixIntervalFormat(match) {
  426. return match.replace('m', 'min').replace('M', 'mon');
  427. }
  428. for (i = 0; i < options.targets.length; i++) {
  429. target = options.targets[i];
  430. if (!target.target) {
  431. continue;
  432. }
  433. if (!target.refId) {
  434. target.refId = this._seriesRefLetters[i];
  435. }
  436. targetValue = templateSrv.replace(target.target, scopedVars);
  437. targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat);
  438. targets[target.refId] = targetValue;
  439. }
  440. function nestedSeriesRegexReplacer(match, g1) {
  441. return targets[g1] || match;
  442. }
  443. for (i = 0; i < options.targets.length; i++) {
  444. target = options.targets[i];
  445. if (!target.target) {
  446. continue;
  447. }
  448. targetValue = targets[target.refId];
  449. targetValue = targetValue.replace(regex, nestedSeriesRegexReplacer);
  450. targets[target.refId] = targetValue;
  451. if (!target.hide) {
  452. hasTargets = true;
  453. clean_options.push('target=' + encodeURIComponent(targetValue));
  454. }
  455. }
  456. _.each(options, function(value, key) {
  457. if (_.indexOf(graphite_options, key) === -1) {
  458. return;
  459. }
  460. if (value) {
  461. clean_options.push(key + '=' + encodeURIComponent(value));
  462. }
  463. });
  464. if (!hasTargets) {
  465. return [];
  466. }
  467. return clean_options;
  468. };
  469. }
  470. function supportsTags(version: string): boolean {
  471. return isVersionGtOrEq(version, '1.1');
  472. }
  473. function supportsFunctionIndex(version: string): boolean {
  474. return isVersionGtOrEq(version, '1.1');
  475. }