2018-09-27 07:03:52 -05:00
|
|
|
import { stackdriverUnitMappings } from './constants';
|
2018-09-27 07:22:20 -05:00
|
|
|
import appEvents from 'app/core/app_events';
|
|
|
|
|
2018-09-05 07:14:34 -05:00
|
|
|
export default class StackdriverDatasource {
|
2018-09-09 15:55:12 -05:00
|
|
|
id: number;
|
2018-09-05 07:14:34 -05:00
|
|
|
url: string;
|
|
|
|
baseUrl: string;
|
2018-09-18 09:02:38 -05:00
|
|
|
projectName: string;
|
2018-09-05 07:14:34 -05:00
|
|
|
|
2018-09-27 07:22:20 -05:00
|
|
|
/** @ngInject */
|
2018-09-24 08:26:49 -05:00
|
|
|
constructor(instanceSettings, private backendSrv, private templateSrv, private timeSrv) {
|
2018-09-05 09:18:03 -05:00
|
|
|
this.baseUrl = `/stackdriver/`;
|
2018-09-05 07:14:34 -05:00
|
|
|
this.url = instanceSettings.url;
|
2018-09-05 11:04:51 -05:00
|
|
|
this.doRequest = this.doRequest;
|
2018-09-09 15:55:12 -05:00
|
|
|
this.id = instanceSettings.id;
|
2018-09-18 09:02:38 -05:00
|
|
|
this.projectName = instanceSettings.jsonData.defaultProject || '';
|
2018-09-05 07:14:34 -05:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:22:48 -05:00
|
|
|
async getTimeSeries(options) {
|
2018-09-14 12:28:48 -05:00
|
|
|
const queries = options.targets
|
|
|
|
.filter(target => {
|
|
|
|
return !target.hide && target.metricType;
|
|
|
|
})
|
|
|
|
.map(t => {
|
|
|
|
if (!t.hasOwnProperty('aggregation')) {
|
|
|
|
t.aggregation = {
|
|
|
|
crossSeriesReducer: 'REDUCE_MEAN',
|
|
|
|
groupBys: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
refId: t.refId,
|
2018-09-27 03:42:28 -05:00
|
|
|
intervalMs: options.intervalMs,
|
2018-09-14 12:28:48 -05:00
|
|
|
datasourceId: this.id,
|
2018-09-20 04:44:17 -05:00
|
|
|
metricType: this.templateSrv.replace(t.metricType, options.scopedVars || {}),
|
|
|
|
primaryAggregation: this.templateSrv.replace(t.aggregation.crossSeriesReducer, options.scopedVars || {}),
|
|
|
|
perSeriesAligner: this.templateSrv.replace(t.aggregation.perSeriesAligner, options.scopedVars || {}),
|
|
|
|
alignmentPeriod: this.templateSrv.replace(t.aggregation.alignmentPeriod, options.scopedVars || {}),
|
|
|
|
groupBys: this.interpolateGroupBys(t.aggregation.groupBys, options.scopedVars),
|
2018-09-14 12:28:48 -05:00
|
|
|
view: t.view || 'FULL',
|
2018-09-20 04:44:17 -05:00
|
|
|
filters: (t.filters || []).map(f => {
|
|
|
|
return this.templateSrv.replace(f, options.scopedVars || {});
|
|
|
|
}),
|
|
|
|
aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}),
|
2018-09-24 03:17:06 -05:00
|
|
|
type: 'timeSeriesQuery',
|
2018-09-14 10:01:27 -05:00
|
|
|
};
|
2018-09-14 12:28:48 -05:00
|
|
|
});
|
2018-09-11 08:52:37 -05:00
|
|
|
|
2018-09-11 15:41:24 -05:00
|
|
|
const { data } = await this.backendSrv.datasourceRequest({
|
|
|
|
url: '/api/tsdb/query',
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
from: options.range.from.valueOf().toString(),
|
|
|
|
to: options.range.to.valueOf().toString(),
|
|
|
|
queries,
|
|
|
|
},
|
|
|
|
});
|
2018-09-13 11:22:48 -05:00
|
|
|
return data;
|
|
|
|
}
|
2018-09-10 12:09:43 -05:00
|
|
|
|
2018-09-24 08:26:49 -05:00
|
|
|
async getLabels(metricType, refId) {
|
|
|
|
return await this.getTimeSeries({
|
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
refId: refId,
|
|
|
|
datasourceId: this.id,
|
|
|
|
metricType: this.templateSrv.replace(metricType),
|
|
|
|
aggregation: {
|
|
|
|
crossSeriesReducer: 'REDUCE_NONE',
|
|
|
|
},
|
|
|
|
view: 'HEADERS',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
range: this.timeSrv.timeRange(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-20 04:44:17 -05:00
|
|
|
interpolateGroupBys(groupBys: string[], scopedVars): string[] {
|
|
|
|
let interpolatedGroupBys = [];
|
|
|
|
(groupBys || []).forEach(gb => {
|
|
|
|
const interpolated = this.templateSrv.replace(gb, scopedVars || {}, 'csv').split(',');
|
|
|
|
if (Array.isArray(interpolated)) {
|
|
|
|
interpolatedGroupBys = interpolatedGroupBys.concat(interpolated);
|
|
|
|
} else {
|
|
|
|
interpolatedGroupBys.push(interpolated);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return interpolatedGroupBys;
|
|
|
|
}
|
|
|
|
|
2018-09-27 07:24:28 -05:00
|
|
|
resolvePanelUnitFromTargets(targets: any[]) {
|
2018-09-27 07:03:52 -05:00
|
|
|
let unit = 'none';
|
|
|
|
if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) {
|
|
|
|
if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) {
|
|
|
|
unit = stackdriverUnitMappings[targets[0].unit];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
2018-09-13 11:22:48 -05:00
|
|
|
async query(options) {
|
|
|
|
const result = [];
|
|
|
|
const data = await this.getTimeSeries(options);
|
2018-09-11 15:41:24 -05:00
|
|
|
if (data.results) {
|
|
|
|
Object['values'](data.results).forEach(queryRes => {
|
2018-09-11 17:24:59 -05:00
|
|
|
if (!queryRes.series) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-27 07:03:52 -05:00
|
|
|
|
2018-09-27 07:24:28 -05:00
|
|
|
const unit = this.resolvePanelUnitFromTargets(options.targets);
|
2018-09-11 15:41:24 -05:00
|
|
|
queryRes.series.forEach(series => {
|
|
|
|
result.push({
|
|
|
|
target: series.name,
|
|
|
|
datapoints: series.points,
|
|
|
|
refId: queryRes.refId,
|
|
|
|
meta: queryRes.meta,
|
2018-09-27 07:03:52 -05:00
|
|
|
unit,
|
2018-09-10 12:09:43 -05:00
|
|
|
});
|
|
|
|
});
|
2018-09-11 15:41:24 -05:00
|
|
|
});
|
2018-09-09 15:55:12 -05:00
|
|
|
}
|
2018-09-11 08:52:37 -05:00
|
|
|
|
|
|
|
return { data: result };
|
2018-09-07 10:18:15 -05:00
|
|
|
}
|
|
|
|
|
2018-09-27 07:22:20 -05:00
|
|
|
async annotationQuery(options) {
|
|
|
|
const annotation = options.annotation;
|
|
|
|
const queries = [
|
|
|
|
{
|
|
|
|
refId: 'annotationQuery',
|
|
|
|
datasourceId: this.id,
|
|
|
|
metricType: this.templateSrv.replace(annotation.target.metricType, options.scopedVars || {}),
|
|
|
|
primaryAggregation: 'REDUCE_NONE',
|
|
|
|
perSeriesAligner: 'ALIGN_NONE',
|
|
|
|
title: this.templateSrv.replace(annotation.target.title, options.scopedVars || {}),
|
|
|
|
text: this.templateSrv.replace(annotation.target.text, options.scopedVars || {}),
|
|
|
|
tags: this.templateSrv.replace(annotation.target.tags, options.scopedVars || {}),
|
|
|
|
view: 'FULL',
|
|
|
|
filters: (annotation.target.filters || []).map(f => {
|
|
|
|
return this.templateSrv.replace(f, options.scopedVars || {});
|
|
|
|
}),
|
|
|
|
type: 'annotationQuery',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const { data } = await this.backendSrv.datasourceRequest({
|
|
|
|
url: '/api/tsdb/query',
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
from: options.range.from.valueOf().toString(),
|
|
|
|
to: options.range.to.valueOf().toString(),
|
|
|
|
queries,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const results = data.results['annotationQuery'].tables[0].rows.map(v => {
|
|
|
|
return {
|
|
|
|
annotation: annotation,
|
|
|
|
time: Date.parse(v[0]),
|
|
|
|
title: v[1],
|
2018-09-28 12:17:34 -05:00
|
|
|
tags: [],
|
2018-09-27 07:22:20 -05:00
|
|
|
text: v[3],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-09-28 09:36:43 -05:00
|
|
|
metricFindQuery(query) {
|
|
|
|
throw new Error('Template variables support is not yet imlemented');
|
|
|
|
}
|
|
|
|
|
2018-09-05 07:14:34 -05:00
|
|
|
testDatasource() {
|
2018-09-18 09:02:38 -05:00
|
|
|
const path = `v3/projects/${this.projectName}/metricDescriptors`;
|
2018-09-05 07:14:34 -05:00
|
|
|
return this.doRequest(`${this.baseUrl}${path}`)
|
|
|
|
.then(response => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
return {
|
|
|
|
status: 'success',
|
2018-09-05 11:04:51 -05:00
|
|
|
message: 'Successfully queried the Stackdriver API.',
|
2018-09-05 07:14:34 -05:00
|
|
|
title: 'Success',
|
|
|
|
};
|
|
|
|
}
|
2018-09-06 08:50:16 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: 'Returned http status code ' + response.status,
|
|
|
|
};
|
2018-09-05 07:14:34 -05:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-09-05 09:18:03 -05:00
|
|
|
let message = 'Stackdriver: ';
|
2018-09-05 07:14:34 -05:00
|
|
|
message += error.statusText ? error.statusText + ': ' : '';
|
|
|
|
|
|
|
|
if (error.data && error.data.error && error.data.error.code) {
|
2018-09-05 09:18:03 -05:00
|
|
|
// 400, 401
|
2018-09-05 07:14:34 -05:00
|
|
|
message += error.data.error.code + '. ' + error.data.error.message;
|
|
|
|
} else {
|
2018-09-05 09:18:03 -05:00
|
|
|
message += 'Cannot connect to Stackdriver API';
|
2018-09-05 07:14:34 -05:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: message,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:17:58 -05:00
|
|
|
async getProjects() {
|
|
|
|
const response = await this.doRequest(`/cloudresourcemanager/v1/projects`);
|
|
|
|
return response.data.projects.map(p => ({ id: p.projectId, name: p.name }));
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:17:06 -05:00
|
|
|
async getDefaultProject() {
|
2018-09-27 07:22:20 -05:00
|
|
|
try {
|
|
|
|
const projects = await this.getProjects();
|
|
|
|
if (projects && projects.length > 0) {
|
|
|
|
const test = projects.filter(p => p.id === this.projectName)[0];
|
|
|
|
return test;
|
|
|
|
} else {
|
|
|
|
throw new Error('No projects found');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
let message = 'Projects cannot be fetched: ';
|
|
|
|
message += error.statusText ? error.statusText + ': ' : '';
|
|
|
|
if (error && error.data && error.data.error && error.data.error.message) {
|
|
|
|
if (error.data.error.code === 403) {
|
|
|
|
message += `
|
|
|
|
A list of projects could not be fetched from the Google Cloud Resource Manager API.
|
|
|
|
You might need to enable it first:
|
|
|
|
https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`;
|
|
|
|
} else {
|
|
|
|
message += error.data.error.code + '. ' + error.data.error.message;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message += 'Cannot connect to Stackdriver API';
|
|
|
|
}
|
|
|
|
appEvents.emit('ds-request-error', message);
|
2018-09-24 03:17:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 10:18:15 -05:00
|
|
|
async getMetricTypes(projectId: string) {
|
|
|
|
try {
|
|
|
|
const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`;
|
|
|
|
const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`);
|
2018-10-08 08:34:28 -05:00
|
|
|
|
|
|
|
const metrics = data.metricDescriptors.map(m => {
|
|
|
|
const [service] = m.type.split('/');
|
|
|
|
const [serviceShortName] = service.split('.');
|
|
|
|
m.service = service;
|
|
|
|
m.serviceShortName = serviceShortName;
|
|
|
|
m.displayName = m.displayName || m.type;
|
|
|
|
return m;
|
|
|
|
});
|
|
|
|
|
|
|
|
return metrics;
|
2018-09-07 10:18:15 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:17:58 -05:00
|
|
|
async doRequest(url, maxRetries = 1) {
|
2018-09-05 07:14:34 -05:00
|
|
|
return this.backendSrv
|
|
|
|
.datasourceRequest({
|
|
|
|
url: this.url + url,
|
|
|
|
method: 'GET',
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (maxRetries > 0) {
|
|
|
|
return this.doRequest(url, maxRetries - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
2018-09-04 06:21:02 -05:00
|
|
|
}
|