|
@@ -2,8 +2,11 @@ package stackdriver
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"context"
|
|
"context"
|
|
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
|
|
|
+ "net/http"
|
|
|
|
|
+ "strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
@@ -29,19 +32,56 @@ func (e *StackdriverExecutor) executeMetricDescriptors(ctx context.Context, tsdb
|
|
|
logger.Info("error2", err)
|
|
logger.Info("error2", err)
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
- body, err := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ data, err := e.unmarshalMetricDescriptors(res)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- logger.Info("error3", err)
|
|
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ 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()
|
|
defer res.Body.Close()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ return MetricDescriptorsResponse{}, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- queryResult.Meta.Set("test", string(body))
|
|
|
|
|
- logger.Info("string(body)", "string(body)", string(body))
|
|
|
|
|
- result.Results[tsdbQuery.Queries[0].RefId] = queryResult
|
|
|
|
|
|
|
+ if res.StatusCode/100 != 2 {
|
|
|
|
|
+ slog.Error("Request failed", "status", res.Status, "body", string(body))
|
|
|
|
|
+ return MetricDescriptorsResponse{}, fmt.Errorf(string(body))
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return result, nil
|
|
|
|
|
|
|
+ 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
|
|
|
}
|
|
}
|