datasource.ts 17 KB

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