datasource.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { stackdriverUnitMappings } from './constants';
  2. import appEvents from 'app/core/app_events';
  3. import _ from 'lodash';
  4. import StackdriverMetricFindQuery from './StackdriverMetricFindQuery';
  5. export default class StackdriverDatasource {
  6. id: number;
  7. url: string;
  8. baseUrl: string;
  9. projectName: string;
  10. authenticationType: string;
  11. queryPromise: Promise<any>;
  12. metricTypes: any[];
  13. /** @ngInject */
  14. constructor(instanceSettings, private backendSrv, private templateSrv, private timeSrv) {
  15. this.baseUrl = `/stackdriver/`;
  16. this.url = instanceSettings.url;
  17. this.doRequest = this.doRequest;
  18. this.id = instanceSettings.id;
  19. this.projectName = instanceSettings.jsonData.defaultProject || '';
  20. this.authenticationType = instanceSettings.jsonData.authenticationType || 'jwt';
  21. this.metricTypes = [];
  22. }
  23. async getTimeSeries(options) {
  24. const queries = options.targets
  25. .filter(target => {
  26. return !target.hide && target.metricType;
  27. })
  28. .map(t => {
  29. if (!t.hasOwnProperty('aggregation')) {
  30. t.aggregation = {
  31. crossSeriesReducer: 'REDUCE_MEAN',
  32. groupBys: [],
  33. };
  34. }
  35. return {
  36. refId: t.refId,
  37. intervalMs: options.intervalMs,
  38. datasourceId: this.id,
  39. metricType: this.templateSrv.replace(t.metricType, options.scopedVars || {}),
  40. primaryAggregation: this.templateSrv.replace(t.aggregation.crossSeriesReducer, options.scopedVars || {}),
  41. perSeriesAligner: this.templateSrv.replace(t.aggregation.perSeriesAligner, options.scopedVars || {}),
  42. alignmentPeriod: this.templateSrv.replace(t.aggregation.alignmentPeriod, options.scopedVars || {}),
  43. groupBys: this.interpolateGroupBys(t.aggregation.groupBys, options.scopedVars),
  44. view: t.view || 'FULL',
  45. filters: (t.filters || []).map(f => {
  46. return this.templateSrv.replace(f, options.scopedVars || {});
  47. }),
  48. aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}),
  49. type: 'timeSeriesQuery',
  50. };
  51. });
  52. if (queries.length > 0) {
  53. const { data } = await this.backendSrv.datasourceRequest({
  54. url: '/api/tsdb/query',
  55. method: 'POST',
  56. data: {
  57. from: options.range.from.valueOf().toString(),
  58. to: options.range.to.valueOf().toString(),
  59. queries,
  60. },
  61. });
  62. return data;
  63. } else {
  64. return { results: [] };
  65. }
  66. }
  67. async getLabels(metricType, refId) {
  68. const response = await this.getTimeSeries({
  69. targets: [
  70. {
  71. refId: refId,
  72. datasourceId: this.id,
  73. metricType: this.templateSrv.replace(metricType),
  74. aggregation: {
  75. crossSeriesReducer: 'REDUCE_NONE',
  76. },
  77. view: 'HEADERS',
  78. },
  79. ],
  80. range: this.timeSrv.timeRange(),
  81. });
  82. return response.results[refId];
  83. }
  84. interpolateGroupBys(groupBys: string[], scopedVars): string[] {
  85. let interpolatedGroupBys = [];
  86. (groupBys || []).forEach(gb => {
  87. const interpolated = this.templateSrv.replace(gb, scopedVars || {}, 'csv').split(',');
  88. if (Array.isArray(interpolated)) {
  89. interpolatedGroupBys = interpolatedGroupBys.concat(interpolated);
  90. } else {
  91. interpolatedGroupBys.push(interpolated);
  92. }
  93. });
  94. return interpolatedGroupBys;
  95. }
  96. resolvePanelUnitFromTargets(targets: any[]) {
  97. let unit;
  98. if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) {
  99. if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) {
  100. unit = stackdriverUnitMappings[targets[0].unit];
  101. }
  102. }
  103. return unit;
  104. }
  105. async getProjects() {
  106. const response = await this.doRequest(`/cloudresourcemanager/v1/projects`);
  107. return response.data.projects.map(p => ({ value: p.projectId, name: p.name }));
  108. }
  109. async query(options) {
  110. const result = [];
  111. const data = await this.getTimeSeries(options);
  112. if (data.results) {
  113. Object['values'](data.results).forEach(queryRes => {
  114. if (!queryRes.series) {
  115. return;
  116. }
  117. const unit = this.resolvePanelUnitFromTargets(options.targets);
  118. queryRes.series.forEach(series => {
  119. let timeSerie: any = {
  120. target: series.name,
  121. datapoints: series.points,
  122. refId: queryRes.refId,
  123. meta: queryRes.meta,
  124. };
  125. if (unit) {
  126. timeSerie = { ...timeSerie, unit };
  127. }
  128. result.push(timeSerie);
  129. });
  130. });
  131. return { data: result };
  132. } else {
  133. return { data: [] };
  134. }
  135. }
  136. async annotationQuery(options) {
  137. const annotation = options.annotation;
  138. const queries = [
  139. {
  140. refId: 'annotationQuery',
  141. datasourceId: this.id,
  142. metricType: this.templateSrv.replace(annotation.target.metricType, options.scopedVars || {}),
  143. primaryAggregation: 'REDUCE_NONE',
  144. perSeriesAligner: 'ALIGN_NONE',
  145. title: this.templateSrv.replace(annotation.target.title, options.scopedVars || {}),
  146. text: this.templateSrv.replace(annotation.target.text, options.scopedVars || {}),
  147. tags: this.templateSrv.replace(annotation.target.tags, options.scopedVars || {}),
  148. view: 'FULL',
  149. filters: (annotation.target.filters || []).map(f => {
  150. return this.templateSrv.replace(f, options.scopedVars || {});
  151. }),
  152. type: 'annotationQuery',
  153. },
  154. ];
  155. const { data } = await this.backendSrv.datasourceRequest({
  156. url: '/api/tsdb/query',
  157. method: 'POST',
  158. data: {
  159. from: options.range.from.valueOf().toString(),
  160. to: options.range.to.valueOf().toString(),
  161. queries,
  162. },
  163. });
  164. const results = data.results['annotationQuery'].tables[0].rows.map(v => {
  165. return {
  166. annotation: annotation,
  167. time: Date.parse(v[0]),
  168. title: v[1],
  169. tags: [],
  170. text: v[3],
  171. };
  172. });
  173. return results;
  174. }
  175. async metricFindQuery(query) {
  176. const stackdriverMetricFindQuery = new StackdriverMetricFindQuery(this);
  177. return stackdriverMetricFindQuery.execute(query);
  178. }
  179. async testDatasource() {
  180. let status, message;
  181. const defaultErrorMessage = 'Cannot connect to Stackdriver API';
  182. try {
  183. const projectName = await this.getDefaultProject();
  184. const path = `v3/projects/${projectName}/metricDescriptors`;
  185. const response = await this.doRequest(`${this.baseUrl}${path}`);
  186. if (response.status === 200) {
  187. status = 'success';
  188. message = 'Successfully queried the Stackdriver API.';
  189. } else {
  190. status = 'error';
  191. message = response.statusText ? response.statusText : defaultErrorMessage;
  192. }
  193. } catch (error) {
  194. status = 'error';
  195. if (_.isString(error)) {
  196. message = error;
  197. } else {
  198. message = 'Stackdriver: ';
  199. message += error.statusText ? error.statusText : defaultErrorMessage;
  200. if (error.data && error.data.error && error.data.error.code) {
  201. message += ': ' + error.data.error.code + '. ' + error.data.error.message;
  202. }
  203. }
  204. } finally {
  205. return {
  206. status,
  207. message,
  208. };
  209. }
  210. }
  211. formatStackdriverError(error) {
  212. let message = 'Stackdriver: ';
  213. message += error.statusText ? error.statusText + ': ' : '';
  214. if (error.data && error.data.error) {
  215. try {
  216. const res = JSON.parse(error.data.error);
  217. message += res.error.code + '. ' + res.error.message;
  218. } catch (err) {
  219. message += error.data.error;
  220. }
  221. } else {
  222. message += 'Cannot connect to Stackdriver API';
  223. }
  224. return message;
  225. }
  226. async getDefaultProject() {
  227. try {
  228. if (this.authenticationType === 'gce' || !this.projectName) {
  229. const { data } = await this.backendSrv.datasourceRequest({
  230. url: '/api/tsdb/query',
  231. method: 'POST',
  232. data: {
  233. queries: [
  234. {
  235. refId: 'ensureDefaultProjectQuery',
  236. type: 'ensureDefaultProjectQuery',
  237. datasourceId: this.id,
  238. },
  239. ],
  240. },
  241. });
  242. this.projectName = data.results.ensureDefaultProjectQuery.meta.defaultProject;
  243. return this.projectName;
  244. } else {
  245. return this.projectName;
  246. }
  247. } catch (error) {
  248. throw this.formatStackdriverError(error);
  249. }
  250. }
  251. async getMetricTypes(projectName: string) {
  252. try {
  253. if (this.metricTypes.length === 0) {
  254. const metricsApiPath = `v3/projects/${projectName}/metricDescriptors`;
  255. const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`);
  256. this.metricTypes = data.metricDescriptors.map(m => {
  257. const [service] = m.type.split('/');
  258. const [serviceShortName] = service.split('.');
  259. m.service = service;
  260. m.serviceShortName = serviceShortName;
  261. m.displayName = m.displayName || m.type;
  262. return m;
  263. });
  264. }
  265. return this.metricTypes;
  266. } catch (error) {
  267. appEvents.emit('ds-request-error', this.formatStackdriverError(error));
  268. return [];
  269. }
  270. }
  271. async doRequest(url, maxRetries = 1) {
  272. return this.backendSrv
  273. .datasourceRequest({
  274. url: this.url + url,
  275. method: 'GET',
  276. })
  277. .catch(error => {
  278. if (maxRetries > 0) {
  279. return this.doRequest(url, maxRetries - 1);
  280. }
  281. throw error;
  282. });
  283. }
  284. }