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