datasource.ts 9.3 KB

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