Commit dd9e9ed4 by Erik Sundell

stackdriver: make backend query a pure test datasource function

parent 9c7022be
package stackdriver
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/components/simplejson"
"golang.org/x/net/context/ctxhttp"
"github.com/grafana/grafana/pkg/tsdb"
)
func (e *StackdriverExecutor) executeMetricDescriptors(ctx context.Context, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
logger.Info("metricDescriptors", "metricDescriptors", tsdbQuery.Queries[0].RefId)
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: tsdbQuery.Queries[0].RefId}
result := &tsdb.Response{
Results: make(map[string]*tsdb.QueryResult),
}
req, err := e.createRequest(ctx, e.dsInfo, "metricDescriptorss")
if err != nil {
slog.Error("Failed to create request", "error", err)
return nil, fmt.Errorf("Failed to create request. error: %v", err)
}
res, err := ctxhttp.Do(ctx, e.httpClient, req)
if err != nil {
logger.Info("error2", err)
return nil, err
}
data, err := e.unmarshalMetricDescriptors(res)
if err != nil {
queryResult.ErrorString = fmt.Sprintf(`Status code: %d`, res.StatusCode)
logger.Info("error2", "ErrorString", queryResult.ErrorString)
queryResult.Error = err
result.Results[tsdbQuery.Queries[0].RefId] = queryResult
return result, nil
}
parts := strings.Split(req.URL.Path, "/")
defaultProject := parts[3]
table := transformMetricDescriptorResponseToTable(data)
queryResult.Tables = append(queryResult.Tables, table)
result.Results[tsdbQuery.Queries[0].RefId] = queryResult
result.Results[tsdbQuery.Queries[0].RefId].Meta.Set("defaultProject", defaultProject)
return result, nil
}
func transformMetricDescriptorResponseToTable(data MetricDescriptorsResponse) *tsdb.Table {
table := &tsdb.Table{
Columns: make([]tsdb.TableColumn, 1),
Rows: make([]tsdb.RowValues, 0),
}
table.Columns[0].Text = "metricDescriptor"
for _, r := range data.MetricDescriptors {
values := make([]interface{}, 1)
values[0] = r
table.Rows = append(table.Rows, values)
}
return table
}
func (e *StackdriverExecutor) unmarshalMetricDescriptors(res *http.Response) (MetricDescriptorsResponse, error) {
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return MetricDescriptorsResponse{}, err
}
if res.StatusCode/100 != 2 {
slog.Error("Request failed", "status", res.Status, "body", string(body))
return MetricDescriptorsResponse{}, fmt.Errorf(`Status code: %d - %s`, res.StatusCode, string(body))
}
var data MetricDescriptorsResponse
err = json.Unmarshal(body, &data)
if err != nil {
slog.Error("Failed to unmarshal MetricDescriptorResponse", "error", err, "status", res.Status, "body", string(body))
return MetricDescriptorsResponse{}, err
}
return data, nil
}
......@@ -15,10 +15,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"golang.org/x/net/context/ctxhttp"
"golang.org/x/oauth2/google"
"github.com/grafana/grafana/pkg/api/pluginproxy"
"github.com/grafana/grafana/pkg/components/null"
......@@ -75,7 +72,7 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
case "annotationQuery":
result, err = e.executeAnnotationQuery(ctx, tsdbQuery)
case "metricDescriptors":
result, err = e.executeMetricDescriptors(ctx, tsdbQuery)
result, err = e.executeTestDataSource(ctx, tsdbQuery)
case "timeSeriesQuery":
fallthrough
default:
......@@ -521,25 +518,6 @@ func replaceWithMetricPart(metaPartName string, metricType string) []byte {
return nil
}
func getProjectName(ctx context.Context, dsInfo *models.DataSource, route *plugins.AppPluginRoute) (string, error) {
var projectName string
gceAutomaticAuthentication := dsInfo.JsonData.Get("gceAutomaticAuthentication").MustBool()
logger.Info("gceAutomaticAuthentication", "gceAutomaticAuthentication", gceAutomaticAuthentication)
if gceAutomaticAuthentication {
defaultCredentials, err := google.FindDefaultCredentials(ctx, route.JwtTokenAuth.Scopes...)
if err != nil {
// return "", err
projectName = "raintank-dev"
} else {
projectName = defaultCredentials.ProjectID
}
} else {
projectName = dsInfo.JsonData.Get("defaultProject").MustString()
}
logger.Info("projectName", "projectName", projectName)
return projectName, nil
}
func calcBucketBound(bucketOptions StackdriverBucketOptions, n int) string {
bucketBound := "0"
if n == 0 {
......@@ -583,12 +561,7 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
}
}
// projectName := dsInfo.JsonData.Get("defaultProject").MustString()
// logger.Info("projectName", "projectName", projectName)
projectName, err := getProjectName(ctx, dsInfo, stackdriverRoute)
if err != nil {
return nil, err
}
projectName := dsInfo.JsonData.Get("defaultProject").MustString()
proxyPass := fmt.Sprintf("stackdriver%s", "v3/projects/"+projectName+"/"+endpointName)
pluginproxy.ApplyRoute(ctx, req, proxyPass, stackdriverRoute, dsInfo)
......
......@@ -174,52 +174,45 @@ export default class StackdriverDatasource {
}
async testDatasource() {
const { data } = await this.backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
queries: [
{
refId: 'metricDescriptors',
datasourceId: this.id,
type: 'metricDescriptors',
},
],
},
});
console.log(data);
return data;
// const path = `v3/projects/${this.projectName}/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 + ': ' : '';
try {
await this.backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
queries: [
{
refId: 'metricDescriptors',
datasourceId: this.id,
type: 'metricDescriptors',
},
],
},
});
return {
status: 'success',
message: 'Successfully queried the Stackdriver API.',
title: 'Success',
};
} catch (error) {
console.log(error.data.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,
// };
// });
if (error.data && error.data.error && 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 {
status: 'error',
message: message,
};
}
}
async getProjects() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment