datasource.ts 16 KB

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