2018-09-04 06:21:58 -05:00
|
|
|
package stackdriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-09-10 10:49:13 -05:00
|
|
|
"errors"
|
2018-09-04 06:21:58 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2018-09-10 07:49:41 -05:00
|
|
|
"time"
|
2018-09-04 06:21:58 -05:00
|
|
|
|
|
|
|
"golang.org/x/net/context/ctxhttp"
|
|
|
|
|
2018-09-09 15:52:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
|
|
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
2018-09-10 04:31:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/null"
|
2018-09-04 06:21:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-09-10 10:49:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2018-09-04 06:21:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StackdriverExecutor struct {
|
|
|
|
HttpClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStackdriverExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
|
|
|
return &StackdriverExecutor{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var glog = log.New("tsdb.stackdriver")
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
tsdb.RegisterTsdbQueryEndpoint("stackdriver", NewStackdriverExecutor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
|
2018-09-10 07:49:41 -05:00
|
|
|
result := &tsdb.Response{
|
|
|
|
Results: make(map[string]*tsdb.QueryResult),
|
|
|
|
}
|
2018-09-04 06:21:58 -05:00
|
|
|
var target string
|
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
startTime, err := tsdbQuery.TimeRange.ParseFrom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
endTime, err := tsdbQuery.TimeRange.ParseTo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("tsdbQuery", "req.URL.RawQuery", tsdbQuery.TimeRange.From)
|
|
|
|
|
2018-09-10 10:49:13 -05:00
|
|
|
for _, query := range tsdbQuery.Queries {
|
2018-09-04 06:21:58 -05:00
|
|
|
if fullTarget, err := query.Model.Get("targetFull").String(); err == nil {
|
|
|
|
target = fixIntervalFormat(fullTarget)
|
|
|
|
} else {
|
|
|
|
target = fixIntervalFormat(query.Model.Get("target").MustString())
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
if setting.Env == setting.DEV {
|
2018-09-10 10:49:13 -05:00
|
|
|
glog.Debug("Stackdriver request", "params")
|
2018-09-10 07:49:41 -05:00
|
|
|
}
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 10:49:13 -05:00
|
|
|
req, err := e.createRequest(ctx, dsInfo)
|
2018-09-10 07:49:41 -05:00
|
|
|
metricType := query.Model.Get("metricType").MustString()
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("interval.startTime", startTime.UTC().Format(time.RFC3339))
|
|
|
|
q.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
|
|
|
|
q.Add("aggregation.perSeriesAligner", "ALIGN_NONE")
|
|
|
|
q.Add("filter", metricType)
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
logger.Info("tsdbQuery", "req.URL.RawQuery", req.URL.RawQuery)
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
httpClient, err := dsInfo.GetHttpClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "stackdriver query")
|
|
|
|
span.SetTag("target", target)
|
|
|
|
span.SetTag("from", tsdbQuery.TimeRange.From)
|
|
|
|
span.SetTag("until", tsdbQuery.TimeRange.To)
|
|
|
|
span.SetTag("datasource_id", dsInfo.Id)
|
|
|
|
span.SetTag("org_id", dsInfo.OrgId)
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
defer span.Finish()
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
opentracing.GlobalTracer().Inject(
|
|
|
|
span.Context(),
|
|
|
|
opentracing.HTTPHeaders,
|
|
|
|
opentracing.HTTPHeadersCarrier(req.Header))
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 07:49:41 -05:00
|
|
|
res, err := ctxhttp.Do(ctx, httpClient, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 12:07:44 -05:00
|
|
|
data, err := e.unmarshalResponse(res)
|
2018-09-10 07:49:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-10 04:31:53 -05:00
|
|
|
}
|
2018-09-04 06:21:58 -05:00
|
|
|
|
2018-09-10 12:07:44 -05:00
|
|
|
queryRes, err := e.parseResponse(data, query.RefId)
|
2018-09-10 10:49:13 -05:00
|
|
|
result.Results[query.RefId] = queryRes
|
2018-09-04 06:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:07:44 -05:00
|
|
|
func (e *StackdriverExecutor) unmarshalResponse(res *http.Response) (StackDriverResponse, error) {
|
2018-09-04 06:21:58 -05:00
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
defer res.Body.Close()
|
|
|
|
if err != nil {
|
2018-09-09 15:52:12 -05:00
|
|
|
return StackDriverResponse{}, err
|
2018-09-04 06:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode/100 != 2 {
|
|
|
|
glog.Info("Request failed", "status", res.Status, "body", string(body))
|
2018-09-09 15:52:12 -05:00
|
|
|
return StackDriverResponse{}, fmt.Errorf("Request failed status: %v", res.Status)
|
2018-09-04 06:21:58 -05:00
|
|
|
}
|
|
|
|
|
2018-09-09 15:52:12 -05:00
|
|
|
var data StackDriverResponse
|
2018-09-04 06:21:58 -05:00
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
if err != nil {
|
2018-09-10 12:07:44 -05:00
|
|
|
glog.Info("Failed to unmarshal Stackdriver response", "error", err, "status", res.Status, "body", string(body))
|
2018-09-09 15:52:12 -05:00
|
|
|
return StackDriverResponse{}, err
|
2018-09-04 06:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:07:44 -05:00
|
|
|
func (e *StackdriverExecutor) parseResponse(data StackDriverResponse, queryRefId string) (*tsdb.QueryResult, error) {
|
|
|
|
queryRes := tsdb.NewQueryResult()
|
|
|
|
queryRes.RefId = queryRefId
|
|
|
|
|
|
|
|
for _, series := range data.TimeSeries {
|
|
|
|
points := make([]tsdb.TimePoint, 0)
|
|
|
|
for _, point := range series.Points {
|
|
|
|
points = append(points, tsdb.NewTimePoint(null.FloatFrom(point.Value.DoubleValue), float64((point.Interval.EndTime).Unix())*1000))
|
|
|
|
}
|
|
|
|
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
|
|
|
|
Name: series.Metric.Type,
|
|
|
|
Points: points,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return queryRes, nil
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:49:13 -05:00
|
|
|
func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.DataSource) (*http.Request, error) {
|
2018-09-04 06:21:58 -05:00
|
|
|
u, _ := url.Parse(dsInfo.Url)
|
|
|
|
u.Path = path.Join(u.Path, "render")
|
|
|
|
|
2018-09-10 10:49:13 -05:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "https://monitoring.googleapis.com/", nil)
|
2018-09-04 06:21:58 -05:00
|
|
|
if err != nil {
|
|
|
|
glog.Info("Failed to create request", "error", err)
|
|
|
|
return nil, fmt.Errorf("Failed to create request. error: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-09 15:52:12 -05:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
2018-09-10 10:49:13 -05:00
|
|
|
// find plugin
|
|
|
|
plugin, ok := plugins.DataSources[dsInfo.Type]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Unable to find datasource plugin Stackdriver")
|
2018-09-04 06:21:58 -05:00
|
|
|
}
|
2018-09-10 10:49:13 -05:00
|
|
|
proxyPass := fmt.Sprintf("stackdriver%s", "v3/projects/raintank-production/timeSeries")
|
|
|
|
|
|
|
|
var stackdriverRoute *plugins.AppPluginRoute
|
|
|
|
for _, route := range plugin.Routes {
|
|
|
|
if route.Path == "stackdriver" {
|
|
|
|
stackdriverRoute = route
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginproxy.ApplyRoute(ctx, req, proxyPass, stackdriverRoute, dsInfo)
|
2018-09-04 06:21:58 -05:00
|
|
|
|
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatTimeRange(input string) string {
|
|
|
|
if input == "now" {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
return strings.Replace(strings.Replace(strings.Replace(input, "now", "", -1), "m", "min", -1), "M", "mon", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixIntervalFormat(target string) string {
|
|
|
|
rMinute := regexp.MustCompile(`'(\d+)m'`)
|
|
|
|
rMin := regexp.MustCompile("m")
|
|
|
|
target = rMinute.ReplaceAllStringFunc(target, func(m string) string {
|
|
|
|
return rMin.ReplaceAllString(m, "min")
|
|
|
|
})
|
|
|
|
rMonth := regexp.MustCompile(`'(\d+)M'`)
|
|
|
|
rMon := regexp.MustCompile("M")
|
|
|
|
target = rMonth.ReplaceAllStringFunc(target, func(M string) string {
|
|
|
|
return rMon.ReplaceAllString(M, "mon")
|
|
|
|
})
|
|
|
|
return target
|
|
|
|
}
|