2018-09-27 14:03:52 +02:00
|
|
|
import { stackdriverUnitMappings } from './constants';
|
2018-09-27 14:22:20 +02:00
|
|
|
import appEvents from 'app/core/app_events';
|
2018-10-11 13:46:51 +02:00
|
|
|
import _ from 'lodash';
|
2018-10-26 14:03:05 +02:00
|
|
|
import StackdriverMetricFindQuery from './StackdriverMetricFindQuery';
|
2020-01-17 12:25:47 +01:00
|
|
|
import { StackdriverQuery, MetricDescriptor, StackdriverOptions, Filter } from './types';
|
2019-10-31 10:48:05 +01:00
|
|
|
import { DataSourceApi, DataQueryRequest, DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
|
2020-01-21 09:08:07 +00:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2019-05-10 02:37:43 -07:00
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
|
|
|
|
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2019-10-14 09:27:47 +01:00
|
|
|
import { CoreEvents } from 'app/types';
|
2018-09-27 14:22:20 +02:00
|
|
|
|
2019-05-10 02:37:43 -07:00
|
|
|
export default class StackdriverDatasource extends DataSourceApi<StackdriverQuery, StackdriverOptions> {
|
2018-09-05 14:14:34 +02:00
|
|
|
url: string;
|
|
|
|
|
baseUrl: string;
|
2018-09-18 16:02:38 +02:00
|
|
|
projectName: string;
|
2018-10-11 13:46:51 +02:00
|
|
|
authenticationType: string;
|
2018-10-10 11:04:06 +02:00
|
|
|
queryPromise: Promise<any>;
|
2018-10-26 14:03:05 +02:00
|
|
|
metricTypes: any[];
|
2018-09-05 14:14:34 +02:00
|
|
|
|
2018-09-27 14:22:20 +02:00
|
|
|
/** @ngInject */
|
2019-05-10 02:37:43 -07:00
|
|
|
constructor(
|
|
|
|
|
instanceSettings: DataSourceInstanceSettings<StackdriverOptions>,
|
2020-01-17 12:25:47 +01:00
|
|
|
public templateSrv: TemplateSrv,
|
2019-05-10 02:37:43 -07:00
|
|
|
private timeSrv: TimeSrv
|
|
|
|
|
) {
|
|
|
|
|
super(instanceSettings);
|
2018-09-05 16:18:03 +02:00
|
|
|
this.baseUrl = `/stackdriver/`;
|
2018-09-05 14:14:34 +02:00
|
|
|
this.url = instanceSettings.url;
|
2018-09-18 16:02:38 +02:00
|
|
|
this.projectName = instanceSettings.jsonData.defaultProject || '';
|
2018-10-11 13:46:51 +02:00
|
|
|
this.authenticationType = instanceSettings.jsonData.authenticationType || 'jwt';
|
2018-10-26 14:03:05 +02:00
|
|
|
this.metricTypes = [];
|
2018-09-05 14:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-17 12:25:47 +01:00
|
|
|
get variables() {
|
|
|
|
|
return this.templateSrv.variables.map(v => `$${v.name}`);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
async getTimeSeries(options: any) {
|
2018-09-14 19:28:48 +02:00
|
|
|
const queries = options.targets
|
2019-06-27 15:56:02 +02:00
|
|
|
.filter((target: any) => {
|
2018-09-14 19:28:48 +02:00
|
|
|
return !target.hide && target.metricType;
|
|
|
|
|
})
|
2019-06-27 15:56:02 +02:00
|
|
|
.map((t: any) => {
|
2018-09-14 19:28:48 +02:00
|
|
|
return {
|
|
|
|
|
refId: t.refId,
|
2018-09-27 10:42:28 +02:00
|
|
|
intervalMs: options.intervalMs,
|
2018-09-14 19:28:48 +02:00
|
|
|
datasourceId: this.id,
|
2018-09-20 11:44:17 +02:00
|
|
|
metricType: this.templateSrv.replace(t.metricType, options.scopedVars || {}),
|
2019-01-29 19:11:54 +01:00
|
|
|
crossSeriesReducer: this.templateSrv.replace(t.crossSeriesReducer || 'REDUCE_MEAN', options.scopedVars || {}),
|
2018-12-20 11:00:16 +01:00
|
|
|
perSeriesAligner: this.templateSrv.replace(t.perSeriesAligner, options.scopedVars || {}),
|
|
|
|
|
alignmentPeriod: this.templateSrv.replace(t.alignmentPeriod, options.scopedVars || {}),
|
|
|
|
|
groupBys: this.interpolateGroupBys(t.groupBys, options.scopedVars),
|
2018-09-14 19:28:48 +02:00
|
|
|
view: t.view || 'FULL',
|
2019-02-05 15:38:42 +01:00
|
|
|
filters: this.interpolateFilters(t.filters, options.scopedVars),
|
2018-09-20 11:44:17 +02:00
|
|
|
aliasBy: this.templateSrv.replace(t.aliasBy, options.scopedVars || {}),
|
2018-09-24 10:17:06 +02:00
|
|
|
type: 'timeSeriesQuery',
|
2018-09-14 17:01:27 +02:00
|
|
|
};
|
2018-09-14 19:28:48 +02:00
|
|
|
});
|
2018-09-11 15:52:37 +02:00
|
|
|
|
2018-10-11 15:42:44 +02:00
|
|
|
if (queries.length > 0) {
|
2020-01-21 09:08:07 +00:00
|
|
|
const { data } = await getBackendSrv().datasourceRequest({
|
2018-10-11 15:42:44 +02:00
|
|
|
url: '/api/tsdb/query',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
from: options.range.from.valueOf().toString(),
|
|
|
|
|
to: options.range.to.valueOf().toString(),
|
|
|
|
|
queries,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return data;
|
|
|
|
|
} else {
|
|
|
|
|
return { results: [] };
|
|
|
|
|
}
|
2018-09-13 18:22:48 +02:00
|
|
|
}
|
2018-09-10 19:09:43 +02:00
|
|
|
|
2019-05-10 02:37:43 -07:00
|
|
|
interpolateFilters(filters: string[], scopedVars: ScopedVars) {
|
2020-01-17 12:25:47 +01:00
|
|
|
const completeFilter = _.chunk(filters, 4)
|
|
|
|
|
.map(([key, operator, value, condition = 'AND']) => ({
|
|
|
|
|
key,
|
|
|
|
|
operator,
|
|
|
|
|
value,
|
|
|
|
|
condition,
|
|
|
|
|
}))
|
|
|
|
|
.reduce((res, filter) => (filter.value ? [...res, filter] : res), []);
|
|
|
|
|
|
|
|
|
|
const filterArray = _.flatten(
|
|
|
|
|
completeFilter.map(({ key, operator, value, condition }: Filter) => [key, operator, value, condition])
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (filterArray || []).map(f => {
|
2019-02-05 15:38:42 +01:00
|
|
|
return this.templateSrv.replace(f, scopedVars || {}, 'regex');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 12:25:47 +01:00
|
|
|
async getLabels(metricType: string, refId: string, groupBys?: string[]) {
|
2018-10-26 14:56:55 +02:00
|
|
|
const response = await this.getTimeSeries({
|
2018-09-24 15:26:49 +02:00
|
|
|
targets: [
|
|
|
|
|
{
|
|
|
|
|
refId: refId,
|
|
|
|
|
datasourceId: this.id,
|
|
|
|
|
metricType: this.templateSrv.replace(metricType),
|
2020-01-17 12:25:47 +01:00
|
|
|
groupBys: this.interpolateGroupBys(groupBys || [], {}),
|
2018-12-20 11:00:16 +01:00
|
|
|
crossSeriesReducer: 'REDUCE_NONE',
|
2018-09-24 15:26:49 +02:00
|
|
|
view: 'HEADERS',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
range: this.timeSrv.timeRange(),
|
|
|
|
|
});
|
2020-01-17 12:25:47 +01:00
|
|
|
const result = response.results[refId];
|
|
|
|
|
return result && result.meta ? result.meta.labels : {};
|
2018-09-24 15:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
interpolateGroupBys(groupBys: string[], scopedVars: {}): string[] {
|
|
|
|
|
let interpolatedGroupBys: any[] = [];
|
2018-09-20 11:44:17 +02:00
|
|
|
(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 14:24:28 +02:00
|
|
|
resolvePanelUnitFromTargets(targets: any[]) {
|
2018-10-18 16:42:08 +02:00
|
|
|
let unit;
|
2018-09-27 14:03:52 +02:00
|
|
|
if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) {
|
|
|
|
|
if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) {
|
2019-06-27 15:56:02 +02:00
|
|
|
// @ts-ignore
|
2018-09-27 14:03:52 +02:00
|
|
|
unit = stackdriverUnitMappings[targets[0].unit];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 21:56:27 -07:00
|
|
|
async query(options: DataQueryRequest<StackdriverQuery>) {
|
2019-06-27 15:56:02 +02:00
|
|
|
const result: any[] = [];
|
2018-10-24 11:18:49 +02:00
|
|
|
const data = await this.getTimeSeries(options);
|
|
|
|
|
if (data.results) {
|
2019-04-17 12:43:00 +02:00
|
|
|
Object['values'](data.results).forEach((queryRes: any) => {
|
2018-10-24 11:18:49 +02:00
|
|
|
if (!queryRes.series) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const unit = this.resolvePanelUnitFromTargets(options.targets);
|
2019-04-17 12:43:00 +02:00
|
|
|
queryRes.series.forEach((series: any) => {
|
2018-10-24 11:18:49 +02:00
|
|
|
let timeSerie: any = {
|
|
|
|
|
target: series.name,
|
|
|
|
|
datapoints: series.points,
|
|
|
|
|
refId: queryRes.refId,
|
|
|
|
|
meta: queryRes.meta,
|
|
|
|
|
};
|
|
|
|
|
if (unit) {
|
|
|
|
|
timeSerie = { ...timeSerie, unit };
|
2018-10-18 16:42:08 +02:00
|
|
|
}
|
2018-10-24 11:18:49 +02:00
|
|
|
result.push(timeSerie);
|
2018-09-10 19:09:43 +02:00
|
|
|
});
|
2018-10-24 11:18:49 +02:00
|
|
|
});
|
|
|
|
|
return { data: result };
|
|
|
|
|
} else {
|
|
|
|
|
return { data: [] };
|
|
|
|
|
}
|
2018-09-07 17:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
async annotationQuery(options: any) {
|
2018-09-27 14:22:20 +02:00
|
|
|
const annotation = options.annotation;
|
|
|
|
|
const queries = [
|
|
|
|
|
{
|
|
|
|
|
refId: 'annotationQuery',
|
|
|
|
|
datasourceId: this.id,
|
|
|
|
|
metricType: this.templateSrv.replace(annotation.target.metricType, options.scopedVars || {}),
|
2019-01-29 19:11:54 +01:00
|
|
|
crossSeriesReducer: 'REDUCE_NONE',
|
2018-09-27 14:22:20 +02:00
|
|
|
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',
|
2020-01-17 12:25:47 +01:00
|
|
|
filters: this.interpolateFilters(annotation.target.filters || [], options.scopedVars),
|
2018-09-27 14:22:20 +02:00
|
|
|
type: 'annotationQuery',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2020-01-21 09:08:07 +00:00
|
|
|
const { data } = await getBackendSrv().datasourceRequest({
|
2018-09-27 14:22:20 +02:00
|
|
|
url: '/api/tsdb/query',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
from: options.range.from.valueOf().toString(),
|
|
|
|
|
to: options.range.to.valueOf().toString(),
|
|
|
|
|
queries,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
const results = data.results['annotationQuery'].tables[0].rows.map((v: any) => {
|
2018-09-27 14:22:20 +02:00
|
|
|
return {
|
|
|
|
|
annotation: annotation,
|
|
|
|
|
time: Date.parse(v[0]),
|
|
|
|
|
title: v[1],
|
2018-09-28 19:17:34 +02:00
|
|
|
tags: [],
|
2018-09-27 14:22:20 +02:00
|
|
|
text: v[3],
|
2019-06-27 15:56:02 +02:00
|
|
|
} as any;
|
2018-09-27 14:22:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
async metricFindQuery(query: string) {
|
2018-10-26 14:03:05 +02:00
|
|
|
const stackdriverMetricFindQuery = new StackdriverMetricFindQuery(this);
|
2018-11-09 13:48:57 +01:00
|
|
|
return stackdriverMetricFindQuery.execute(query);
|
2018-09-28 16:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-09 13:17:29 +02:00
|
|
|
async testDatasource() {
|
2018-10-11 13:46:51 +02:00
|
|
|
let status, message;
|
|
|
|
|
const defaultErrorMessage = 'Cannot connect to Stackdriver API';
|
2018-10-09 15:20:05 +02:00
|
|
|
try {
|
2018-10-11 13:46:51 +02:00
|
|
|
const projectName = await this.getDefaultProject();
|
|
|
|
|
const path = `v3/projects/${projectName}/metricDescriptors`;
|
|
|
|
|
const response = await this.doRequest(`${this.baseUrl}${path}`);
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
status = 'success';
|
|
|
|
|
message = 'Successfully queried the Stackdriver API.';
|
|
|
|
|
} else {
|
|
|
|
|
status = 'error';
|
|
|
|
|
message = response.statusText ? response.statusText : defaultErrorMessage;
|
|
|
|
|
}
|
2018-10-09 15:20:05 +02:00
|
|
|
} catch (error) {
|
2018-10-11 13:46:51 +02:00
|
|
|
status = 'error';
|
|
|
|
|
if (_.isString(error)) {
|
|
|
|
|
message = error;
|
|
|
|
|
} else {
|
|
|
|
|
message = 'Stackdriver: ';
|
2018-10-11 14:44:51 +02:00
|
|
|
message += error.statusText ? error.statusText : defaultErrorMessage;
|
2018-10-11 13:46:51 +02:00
|
|
|
if (error.data && error.data.error && error.data.error.code) {
|
2018-10-11 14:44:51 +02:00
|
|
|
message += ': ' + error.data.error.code + '. ' + error.data.error.message;
|
2018-10-11 13:46:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
2018-10-09 15:20:05 +02:00
|
|
|
return {
|
2018-10-11 13:46:51 +02:00
|
|
|
status,
|
|
|
|
|
message,
|
2018-10-09 15:20:05 +02:00
|
|
|
};
|
|
|
|
|
}
|
2018-09-05 14:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
formatStackdriverError(error: any) {
|
2018-10-11 09:51:44 +02:00
|
|
|
let message = 'Stackdriver: ';
|
|
|
|
|
message += error.statusText ? error.statusText + ': ' : '';
|
|
|
|
|
if (error.data && error.data.error) {
|
|
|
|
|
try {
|
|
|
|
|
const res = JSON.parse(error.data.error);
|
|
|
|
|
message += res.error.code + '. ' + res.error.message;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
message += error.data.error;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
message += 'Cannot connect to Stackdriver API';
|
|
|
|
|
}
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-24 10:17:06 +02:00
|
|
|
async getDefaultProject() {
|
2018-09-27 14:22:20 +02:00
|
|
|
try {
|
2018-10-11 13:46:51 +02:00
|
|
|
if (this.authenticationType === 'gce' || !this.projectName) {
|
2020-01-21 09:08:07 +00:00
|
|
|
const { data } = await getBackendSrv().datasourceRequest({
|
2018-10-10 11:55:45 +02:00
|
|
|
url: '/api/tsdb/query',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
queries: [
|
|
|
|
|
{
|
2018-10-11 13:46:51 +02:00
|
|
|
refId: 'ensureDefaultProjectQuery',
|
|
|
|
|
type: 'ensureDefaultProjectQuery',
|
2018-10-10 11:55:45 +02:00
|
|
|
datasourceId: this.id,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
2018-10-11 13:46:51 +02:00
|
|
|
this.projectName = data.results.ensureDefaultProjectQuery.meta.defaultProject;
|
2018-10-10 11:55:45 +02:00
|
|
|
return this.projectName;
|
|
|
|
|
} else {
|
|
|
|
|
return this.projectName;
|
|
|
|
|
}
|
2018-09-27 14:22:20 +02:00
|
|
|
} catch (error) {
|
2018-10-11 13:46:51 +02:00
|
|
|
throw this.formatStackdriverError(error);
|
2018-09-24 10:17:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 14:03:52 +01:00
|
|
|
async getMetricTypes(projectName: string): Promise<MetricDescriptor[]> {
|
2018-09-07 17:18:15 +02:00
|
|
|
try {
|
2018-10-26 14:03:05 +02:00
|
|
|
if (this.metricTypes.length === 0) {
|
|
|
|
|
const metricsApiPath = `v3/projects/${projectName}/metricDescriptors`;
|
|
|
|
|
const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`);
|
2018-10-08 15:34:28 +02:00
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
this.metricTypes = data.metricDescriptors.map((m: any) => {
|
2018-10-26 14:03:05 +02:00
|
|
|
const [service] = m.type.split('/');
|
|
|
|
|
const [serviceShortName] = service.split('.');
|
|
|
|
|
m.service = service;
|
|
|
|
|
m.serviceShortName = serviceShortName;
|
|
|
|
|
m.displayName = m.displayName || m.type;
|
2018-12-12 09:14:06 +01:00
|
|
|
|
2018-10-26 14:03:05 +02:00
|
|
|
return m;
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-08 15:34:28 +02:00
|
|
|
|
2018-10-26 14:03:05 +02:00
|
|
|
return this.metricTypes;
|
2018-09-07 17:18:15 +02:00
|
|
|
} catch (error) {
|
2019-10-14 09:27:47 +01:00
|
|
|
appEvents.emit(CoreEvents.dsRequestError, { error: { data: { error: this.formatStackdriverError(error) } } });
|
2018-10-11 09:51:44 +02:00
|
|
|
return [];
|
2018-09-07 17:18:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 09:08:07 +00:00
|
|
|
async doRequest(url: string, maxRetries = 1): Promise<any> {
|
|
|
|
|
return getBackendSrv()
|
2018-09-05 14:14:34 +02:00
|
|
|
.datasourceRequest({
|
|
|
|
|
url: this.url + url,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
})
|
2019-06-27 15:56:02 +02:00
|
|
|
.catch((error: any) => {
|
2018-09-05 14:14:34 +02:00
|
|
|
if (maxRetries > 0) {
|
|
|
|
|
return this.doRequest(url, maxRetries - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-09-04 13:21:02 +02:00
|
|
|
}
|