datasource.ts 17 KB

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