datasource.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import * as dateMath from 'app/core/utils/datemath';
  4. import {isVersionGtOrEq, SemVersion} from 'app/core/utils/version';
  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.getQueryOptionsInfo = function() {
  16. return {
  17. "maxDataPoints": true,
  18. "cacheTimeout": true,
  19. "links": [
  20. {
  21. text: "Help",
  22. url: "http://docs.grafana.org/features/datasources/graphite/#using-graphite-in-grafana"
  23. }
  24. ]
  25. };
  26. };
  27. this.query = function(options) {
  28. var graphOptions = {
  29. from: this.translateTime(options.rangeRaw.from, false),
  30. until: this.translateTime(options.rangeRaw.to, true),
  31. targets: options.targets,
  32. format: options.format,
  33. cacheTimeout: options.cacheTimeout || this.cacheTimeout,
  34. maxDataPoints: options.maxDataPoints,
  35. };
  36. var params = this.buildGraphiteParams(graphOptions, options.scopedVars);
  37. if (params.length === 0) {
  38. return $q.when({data: []});
  39. }
  40. var httpOptions: any = {
  41. method: 'POST',
  42. url: '/render',
  43. data: params.join('&'),
  44. headers: {
  45. 'Content-Type': 'application/x-www-form-urlencoded'
  46. },
  47. };
  48. if (options.panelId) {
  49. httpOptions.requestId = this.name + '.panelId.' + options.panelId;
  50. }
  51. return this.doGraphiteRequest(httpOptions).then(this.convertDataPointsToMs);
  52. };
  53. this.convertDataPointsToMs = function(result) {
  54. if (!result || !result.data) { return []; }
  55. for (var i = 0; i < result.data.length; i++) {
  56. var series = result.data[i];
  57. for (var y = 0; y < series.datapoints.length; y++) {
  58. series.datapoints[y][1] *= 1000;
  59. }
  60. }
  61. return result;
  62. };
  63. this.parseTags = function(tagString) {
  64. let tags = [];
  65. tags = tagString.split(',');
  66. if (tags.length === 1) {
  67. tags = tagString.split(' ');
  68. if (tags[0] === '') {
  69. tags = [];
  70. }
  71. }
  72. return tags;
  73. };
  74. this.annotationQuery = function(options) {
  75. // Graphite metric as annotation
  76. if (options.annotation.target) {
  77. var target = templateSrv.replace(options.annotation.target, {}, 'glob');
  78. var graphiteQuery = {
  79. rangeRaw: options.rangeRaw,
  80. targets: [{ target: target }],
  81. format: 'json',
  82. maxDataPoints: 100
  83. };
  84. return this.query(graphiteQuery).then(function(result) {
  85. var list = [];
  86. for (var i = 0; i < result.data.length; i++) {
  87. var target = result.data[i];
  88. for (var y = 0; y < target.datapoints.length; y++) {
  89. var datapoint = target.datapoints[y];
  90. if (!datapoint[0]) { continue; }
  91. list.push({
  92. annotation: options.annotation,
  93. time: datapoint[1],
  94. title: target.target
  95. });
  96. }
  97. }
  98. return list;
  99. });
  100. } else {
  101. // Graphite event as annotation
  102. var tags = templateSrv.replace(options.annotation.tags);
  103. return this.events({range: options.rangeRaw, tags: tags}).then(results => {
  104. var list = [];
  105. for (var i = 0; i < results.data.length; i++) {
  106. var e = results.data[i];
  107. var tags = e.tags;
  108. if (_.isString(e.tags)) {
  109. tags = this.parseTags(e.tags);
  110. }
  111. list.push({
  112. annotation: options.annotation,
  113. time: e.when * 1000,
  114. title: e.what,
  115. tags: tags,
  116. text: e.data
  117. });
  118. }
  119. return list;
  120. });
  121. }
  122. };
  123. this.events = function(options) {
  124. try {
  125. var tags = '';
  126. if (options.tags) {
  127. tags = '&tags=' + options.tags;
  128. }
  129. return this.doGraphiteRequest({
  130. method: 'GET',
  131. url: '/events/get_data?from=' + this.translateTime(options.range.from, false) +
  132. '&until=' + this.translateTime(options.range.to, true) + tags,
  133. });
  134. } catch (err) {
  135. return $q.reject(err);
  136. }
  137. };
  138. this.targetContainsTemplate = function(target) {
  139. return templateSrv.variableExists(target.target);
  140. };
  141. this.translateTime = function(date, roundUp) {
  142. if (_.isString(date)) {
  143. if (date === 'now') {
  144. return 'now';
  145. } else if (date.indexOf('now-') >= 0 && date.indexOf('/') === -1) {
  146. date = date.substring(3);
  147. date = date.replace('m', 'min');
  148. date = date.replace('M', 'mon');
  149. return date;
  150. }
  151. date = dateMath.parse(date, roundUp);
  152. }
  153. // graphite' s from filter is exclusive
  154. // here we step back one minute in order
  155. // to guarantee that we get all the data that
  156. // exists for the specified range
  157. if (roundUp) {
  158. if (date.get('s')) {
  159. date.add(1, 'm');
  160. }
  161. } else if (roundUp === false) {
  162. if (date.get('s')) {
  163. date.subtract(1, 'm');
  164. }
  165. }
  166. return date.unix();
  167. };
  168. this.metricFindQuery = function(query, optionalOptions) {
  169. let options = optionalOptions || {};
  170. let interpolatedQuery = templateSrv.replace(query);
  171. let httpOptions: any = {
  172. method: 'GET',
  173. url: '/metrics/find',
  174. params: {
  175. query: interpolatedQuery
  176. },
  177. // for cancellations
  178. requestId: options.requestId,
  179. };
  180. if (options && options.range) {
  181. httpOptions.params.from = this.translateTime(options.range.from, false);
  182. httpOptions.params.until = this.translateTime(options.range.to, true);
  183. }
  184. return this.doGraphiteRequest(httpOptions).then(results => {
  185. return _.map(results.data, metric => {
  186. return {
  187. text: metric.text,
  188. expandable: metric.expandable ? true : false
  189. };
  190. });
  191. });
  192. };
  193. this.getTags = function(optionalOptions) {
  194. let options = optionalOptions || {};
  195. let httpOptions: any = {
  196. method: 'GET',
  197. url: '/tags',
  198. // for cancellations
  199. requestId: options.requestId,
  200. };
  201. if (options && options.range) {
  202. httpOptions.params.from = this.translateTime(options.range.from, false);
  203. httpOptions.params.until = this.translateTime(options.range.to, true);
  204. }
  205. return this.doGraphiteRequest(httpOptions).then(results => {
  206. return _.map(results.data, tag => {
  207. return {
  208. text: tag.tag,
  209. id: tag.id
  210. };
  211. });
  212. });
  213. };
  214. this.getTagValues = function(tag, optionalOptions) {
  215. let options = optionalOptions || {};
  216. let httpOptions: any = {
  217. method: 'GET',
  218. url: '/tags/' + tag,
  219. // for cancellations
  220. requestId: options.requestId,
  221. };
  222. if (options && options.range) {
  223. httpOptions.params.from = this.translateTime(options.range.from, false);
  224. httpOptions.params.until = this.translateTime(options.range.to, true);
  225. }
  226. return this.doGraphiteRequest(httpOptions).then(results => {
  227. if (results.data && results.data.values) {
  228. return _.map(results.data.values, value => {
  229. return {
  230. text: value.value,
  231. id: value.id
  232. };
  233. });
  234. } else {
  235. return [];
  236. }
  237. });
  238. };
  239. this.getTagsAutoComplete = (expression, tagPrefix) => {
  240. let httpOptions: any = {
  241. method: 'GET',
  242. url: '/tags/autoComplete/tags',
  243. params: {
  244. expr: expression
  245. }
  246. };
  247. if (tagPrefix) {
  248. httpOptions.params.tagPrefix = tagPrefix;
  249. }
  250. return this.doGraphiteRequest(httpOptions).then(results => {
  251. if (results.data) {
  252. return _.map(results.data, (tag) => {
  253. return { text: tag };
  254. });
  255. } else {
  256. return [];
  257. }
  258. });
  259. };
  260. this.getTagValuesAutoComplete = (expression, tag, valuePrefix) => {
  261. let httpOptions: any = {
  262. method: 'GET',
  263. url: '/tags/autoComplete/values',
  264. params: {
  265. expr: expression,
  266. tag: tag
  267. }
  268. };
  269. if (valuePrefix) {
  270. httpOptions.params.valuePrefix = valuePrefix;
  271. }
  272. return this.doGraphiteRequest(httpOptions).then(results => {
  273. if (results.data) {
  274. return _.map(results.data, (value) => {
  275. return { text: value };
  276. });
  277. } else {
  278. return [];
  279. }
  280. });
  281. };
  282. this.getVersion = function() {
  283. let httpOptions = {
  284. method: 'GET',
  285. url: '/version/_', // Prevent last / trimming
  286. };
  287. return this.doGraphiteRequest(httpOptions).then(results => {
  288. if (results.data) {
  289. let semver = new SemVersion(results.data);
  290. return semver.isValid() ? results.data : '';
  291. }
  292. return '';
  293. }).catch(() => {
  294. return '';
  295. });
  296. };
  297. this.testDatasource = function() {
  298. return this.metricFindQuery('*').then(function () {
  299. return { status: "success", message: "Data source is working"};
  300. });
  301. };
  302. this.doGraphiteRequest = function(options) {
  303. if (this.basicAuth || this.withCredentials) {
  304. options.withCredentials = true;
  305. }
  306. if (this.basicAuth) {
  307. options.headers = options.headers || {};
  308. options.headers.Authorization = this.basicAuth;
  309. }
  310. options.url = this.url + options.url;
  311. options.inspect = {type: 'graphite'};
  312. return backendSrv.datasourceRequest(options);
  313. };
  314. this._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  315. this.buildGraphiteParams = function(options, scopedVars) {
  316. var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
  317. var clean_options = [], targets = {};
  318. var target, targetValue, i;
  319. var regex = /\#([A-Z])/g;
  320. var intervalFormatFixRegex = /'(\d+)m'/gi;
  321. var hasTargets = false;
  322. options['format'] = 'json';
  323. function fixIntervalFormat(match) {
  324. return match.replace('m', 'min').replace('M', 'mon');
  325. }
  326. for (i = 0; i < options.targets.length; i++) {
  327. target = options.targets[i];
  328. if (!target.target) {
  329. continue;
  330. }
  331. if (!target.refId) {
  332. target.refId = this._seriesRefLetters[i];
  333. }
  334. targetValue = templateSrv.replace(target.target, scopedVars);
  335. targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat);
  336. targets[target.refId] = targetValue;
  337. }
  338. function nestedSeriesRegexReplacer(match, g1) {
  339. return targets[g1] || match;
  340. }
  341. for (i = 0; i < options.targets.length; i++) {
  342. target = options.targets[i];
  343. if (!target.target) {
  344. continue;
  345. }
  346. targetValue = targets[target.refId];
  347. targetValue = targetValue.replace(regex, nestedSeriesRegexReplacer);
  348. targets[target.refId] = targetValue;
  349. if (!target.hide) {
  350. hasTargets = true;
  351. clean_options.push("target=" + encodeURIComponent(targetValue));
  352. }
  353. }
  354. _.each(options, function (value, key) {
  355. if (_.indexOf(graphite_options, key) === -1) { return; }
  356. if (value) {
  357. clean_options.push(key + "=" + encodeURIComponent(value));
  358. }
  359. });
  360. if (!hasTargets) {
  361. return [];
  362. }
  363. return clean_options;
  364. };
  365. }
  366. function supportsTags(version: string): boolean {
  367. return isVersionGtOrEq(version, '1.1');
  368. }