The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/plugins/datasource/stackdriver/datasource.ts

123 lines
3.3 KiB

/** @ngInject */
export default class StackdriverDatasource {
id: number;
url: string;
baseUrl: string;
constructor(instanceSettings, private backendSrv) {
this.baseUrl = `/stackdriver/`;
this.url = instanceSettings.url;
this.doRequest = this.doRequest;
this.id = instanceSettings.id;
}
async getTimeSeries(options) {
const queries = options.targets.filter(target => !target.hide).map(t => ({
refId: t.refId,
datasourceId: this.id,
metricType: t.metricType,
primaryAggregation: t.aggregation.crossSeriesReducer,
groupBys: t.aggregation.groupBys,
view: t.view || 'FULL',
filters: t.filters,
}));
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,
},
});
return data;
}
async query(options) {
const result = [];
const data = await this.getTimeSeries(options);
if (data.results) {
Object['values'](data.results).forEach(queryRes => {
if (!queryRes.series) {
return;
}
queryRes.series.forEach(series => {
result.push({
target: series.name,
datapoints: series.points,
refId: queryRes.refId,
meta: queryRes.meta,
});
});
});
}
return { data: result };
}
testDatasource() {
const path = `v3/projects/raintank-production/metricDescriptors`;
return this.doRequest(`${this.baseUrl}${path}`)
.then(response => {
if (response.status === 200) {
return {
status: 'success',
message: 'Successfully queried the Stackdriver API.',
title: 'Success',
};
}
return {
status: 'error',
message: 'Returned http status code ' + response.status,
};
})
.catch(error => {
let message = 'Stackdriver: ';
message += error.statusText ? error.statusText + ': ' : '';
if (error.data && error.data.error && error.data.error.code) {
// 400, 401
message += error.data.error.code + '. ' + error.data.error.message;
} else {
message += 'Cannot connect to Stackdriver API';
}
return {
status: 'error',
message: message,
};
});
}
async getProjects() {
const response = await this.doRequest(`/cloudresourcemanager/v1/projects`);
return response.data.projects.map(p => ({ id: p.projectId, name: p.name }));
}
async getMetricTypes(projectId: string) {
try {
const metricsApiPath = `v3/projects/${projectId}/metricDescriptors`;
const { data } = await this.doRequest(`${this.baseUrl}${metricsApiPath}`);
return data.metricDescriptors.map(m => ({ id: m.type, name: m.displayName }));
} catch (error) {
console.log(error);
}
}
async doRequest(url, maxRetries = 1) {
return this.backendSrv
.datasourceRequest({
url: this.url + url,
method: 'GET',
})
.catch(error => {
if (maxRetries > 0) {
return this.doRequest(url, maxRetries - 1);
}
throw error;
});
}
}