datasource.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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: any) {
  32. const queries = options.targets
  33. .filter((target: any) => {
  34. return !target.hide && target.metricType;
  35. })
  36. .map((t: any) => {
  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: any[] = [];
  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. // @ts-ignore
  104. unit = stackdriverUnitMappings[targets[0].unit];
  105. }
  106. }
  107. return unit;
  108. }
  109. async query(options: DataQueryRequest<StackdriverQuery>) {
  110. const result: any[] = [];
  111. const data = await this.getTimeSeries(options);
  112. if (data.results) {
  113. Object['values'](data.results).forEach((queryRes: any) => {
  114. if (!queryRes.series) {
  115. return;
  116. }
  117. const unit = this.resolvePanelUnitFromTargets(options.targets);
  118. queryRes.series.forEach((series: any) => {
  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: any) {
  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. crossSeriesReducer: '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: any) => {
  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: any) => {
  165. return {
  166. annotation: annotation,
  167. time: Date.parse(v[0]),
  168. title: v[1],
  169. tags: [],
  170. text: v[3],
  171. } as any;
  172. });
  173. return results;
  174. }
  175. async metricFindQuery(query: string) {
  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: any) {
  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): Promise<MetricDescriptor[]> {
  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: any) => {
  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: string, maxRetries = 1) {
  272. return this.backendSrv
  273. .datasourceRequest({
  274. url: this.url + url,
  275. method: 'GET',
  276. })
  277. .catch((error: any) => {
  278. if (maxRetries > 0) {
  279. return this.doRequest(url, maxRetries - 1);
  280. }
  281. throw error;
  282. });
  283. }
  284. }