stackdriver: improve error handling

This commit is contained in:
Erik Sundell 2018-10-11 13:46:51 +02:00
parent 5b04a8b6c2
commit ae8a765190
2 changed files with 44 additions and 30 deletions

View File

@ -1,11 +1,13 @@
import { stackdriverUnitMappings } from './constants'; import { stackdriverUnitMappings } from './constants';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import _ from 'lodash';
export default class StackdriverDatasource { export default class StackdriverDatasource {
id: number; id: number;
url: string; url: string;
baseUrl: string; baseUrl: string;
projectName: string; projectName: string;
authenticationType: string;
queryPromise: Promise<any>; queryPromise: Promise<any>;
/** @ngInject */ /** @ngInject */
@ -15,6 +17,7 @@ export default class StackdriverDatasource {
this.doRequest = this.doRequest; this.doRequest = this.doRequest;
this.id = instanceSettings.id; this.id = instanceSettings.id;
this.projectName = instanceSettings.jsonData.defaultProject || ''; this.projectName = instanceSettings.jsonData.defaultProject || '';
this.authenticationType = instanceSettings.jsonData.authenticationType || 'jwt';
} }
async getTimeSeries(options) { async getTimeSeries(options) {
@ -178,29 +181,36 @@ export default class StackdriverDatasource {
} }
async testDatasource() { async testDatasource() {
let status, message;
const defaultErrorMessage = 'Cannot connect to Stackdriver API';
try { try {
await this.backendSrv.datasourceRequest({ const projectName = await this.getDefaultProject();
url: '/api/tsdb/query', const path = `v3/projects/${projectName}/metricDescriptors`;
method: 'POST', const response = await this.doRequest(`${this.baseUrl}${path}`);
data: { if (response.status === 200) {
queries: [ status = 'success';
{ message = 'Successfully queried the Stackdriver API.';
refId: 'testDatasource', } else {
datasourceId: this.id, status = 'error';
type: 'testDatasource', message = response.statusText ? response.statusText : defaultErrorMessage;
}, }
],
},
});
return {
status: 'success',
message: 'Successfully queried the Stackdriver API.',
title: 'Success',
};
} catch (error) { } catch (error) {
status = 'error';
if (_.isString(error)) {
message = error;
} else {
message = 'Stackdriver: ';
message += error.statusText ? error.statusText + ': ' : '';
if (error.data && error.data.error && error.data.error.code) {
message += error.data.error.code + '. ' + error.data.error.message;
} else {
message = defaultErrorMessage;
}
}
} finally {
return { return {
status: 'error', status,
message: this.formatStackdriverError(error), message,
}; };
} }
} }
@ -223,28 +233,27 @@ export default class StackdriverDatasource {
async getDefaultProject() { async getDefaultProject() {
try { try {
if (!this.projectName) { if (this.authenticationType === 'gce' || !this.projectName) {
const { data } = await this.backendSrv.datasourceRequest({ const { data } = await this.backendSrv.datasourceRequest({
url: '/api/tsdb/query', url: '/api/tsdb/query',
method: 'POST', method: 'POST',
data: { data: {
queries: [ queries: [
{ {
refId: 'defaultProject', refId: 'ensureDefaultProjectQuery',
type: 'defaultProject', type: 'ensureDefaultProjectQuery',
datasourceId: this.id, datasourceId: this.id,
}, },
], ],
}, },
}); });
this.projectName = data.results.defaultProject.meta.defaultProject; this.projectName = data.results.ensureDefaultProjectQuery.meta.defaultProject;
return this.projectName; return this.projectName;
} else { } else {
return this.projectName; return this.projectName;
} }
} catch (error) { } catch (error) {
appEvents.emit('ds-request-error', this.formatStackdriverError(error)); throw this.formatStackdriverError(error);
return '';
} }
} }

View File

@ -79,11 +79,16 @@ export class StackdriverFilterCtrl {
} }
async getCurrentProject() { async getCurrentProject() {
return new Promise(async resolve => { return new Promise(async (resolve, reject) => {
if (!this.target.defaultProject || this.target.defaultProject === 'loading project...') { try {
this.target.defaultProject = await this.datasource.getDefaultProject(); if (!this.target.defaultProject || this.target.defaultProject === 'loading project...') {
this.target.defaultProject = await this.datasource.getDefaultProject();
}
resolve(this.target.defaultProject);
} catch (error) {
appEvents.emit('ds-request-error', error);
reject();
} }
resolve(this.target.defaultProject);
}); });
} }