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