Commit 7f99a14c by Erik Sundell Committed by Daniel Lee

Stackdriver: Parsed url params

parent c8ec3a34
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"path" "path"
"regexp" "regexp"
"strings" "strings"
"time"
"golang.org/x/net/context/ctxhttp" "golang.org/x/net/context/ctxhttp"
...@@ -38,35 +39,57 @@ func init() { ...@@ -38,35 +39,57 @@ func init() {
} }
func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) { func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
result := &tsdb.Response{} result := &tsdb.Response{
logger.Info("tsdbQuery", "tsdbQuery", tsdbQuery) Results: make(map[string]*tsdb.QueryResult),
from := "-" + formatTimeRange(tsdbQuery.TimeRange.From) }
until := formatTimeRange(tsdbQuery.TimeRange.To)
// from := "-" + formatTimeRange(tsdbQuery.TimeRange.From)
// until := formatTimeRange(tsdbQuery.TimeRange.To)
var target string var target string
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)
formData := url.Values{ formData := url.Values{
"from": []string{from}, "interval.startTime": []string{startTime.String()},
"until": []string{until}, "interval.endTime": []string{endTime.String()},
"format": []string{"json"}, "aggregation.perSeriesAligner": []string{"ALIGN_NONE"},
"maxDataPoints": []string{"500"},
} }
for _, query := range tsdbQuery.Queries { for i, query := range tsdbQuery.Queries {
glog.Info("stackdriver", "query", query.Model)
if fullTarget, err := query.Model.Get("targetFull").String(); err == nil { if fullTarget, err := query.Model.Get("targetFull").String(); err == nil {
target = fixIntervalFormat(fullTarget) target = fixIntervalFormat(fullTarget)
} else { } else {
target = fixIntervalFormat(query.Model.Get("target").MustString()) target = fixIntervalFormat(query.Model.Get("target").MustString())
} }
}
formData["target"] = []string{target}
if setting.Env == setting.DEV { if setting.Env == setting.DEV {
glog.Debug("Graphite request", "params", formData) glog.Debug("Stackdriver request", "params", formData)
} }
req, err := e.createRequest(dsInfo, formData) req, err := e.createRequest(dsInfo, formData)
RefId := tsdbQuery.Queries[i].RefId
metricType := query.Model.Get("metricType").MustString()
// formData["metric.type"] = []string{metricType}
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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -78,8 +101,8 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour ...@@ -78,8 +101,8 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
span, ctx := opentracing.StartSpanFromContext(ctx, "stackdriver query") span, ctx := opentracing.StartSpanFromContext(ctx, "stackdriver query")
span.SetTag("target", target) span.SetTag("target", target)
span.SetTag("from", from) span.SetTag("from", tsdbQuery.TimeRange.From)
span.SetTag("until", until) span.SetTag("until", tsdbQuery.TimeRange.To)
span.SetTag("datasource_id", dsInfo.Id) span.SetTag("datasource_id", dsInfo.Id)
span.SetTag("org_id", dsInfo.OrgId) span.SetTag("org_id", dsInfo.OrgId)
...@@ -100,8 +123,9 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour ...@@ -100,8 +123,9 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
return nil, err return nil, err
} }
result.Results = make(map[string]*tsdb.QueryResult) // result.Results = make(map[string]*tsdb.QueryResult)
queryRes := tsdb.NewQueryResult() queryRes := tsdb.NewQueryResult()
queryRes.RefId = RefId
for _, series := range data.TimeSeries { for _, series := range data.TimeSeries {
points := make([]tsdb.TimePoint, 0) points := make([]tsdb.TimePoint, 0)
...@@ -112,13 +136,10 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour ...@@ -112,13 +136,10 @@ func (e *StackdriverExecutor) Query(ctx context.Context, dsInfo *models.DataSour
Name: series.Metric.Type, Name: series.Metric.Type,
Points: points, Points: points,
}) })
if setting.Env == setting.DEV {
glog.Debug("Stackdriver response", "target", points, "datapoints", len(points))
} }
result.Results[queryRes.RefId] = queryRes
} }
result.Results["A"] = queryRes
return result, nil return result, nil
} }
...@@ -148,7 +169,8 @@ func (e *StackdriverExecutor) createRequest(dsInfo *models.DataSource, data url. ...@@ -148,7 +169,8 @@ func (e *StackdriverExecutor) createRequest(dsInfo *models.DataSource, data url.
u, _ := url.Parse(dsInfo.Url) u, _ := url.Parse(dsInfo.Url)
u.Path = path.Join(u.Path, "render") u.Path = path.Join(u.Path, "render")
req, err := http.NewRequest(http.MethodGet, "https://monitoring.googleapis.com/v3/projects/raintank-production/timeSeries?&filter=metric.type%20%3D%20%22compute.googleapis.com%2Finstance%2Fcpu%2Fusage_time%22&aggregation.perSeriesAligner=ALIGN_NONE&interval.startTime=2018-09-04T11%3A14%3A02.383Z&interval.endTime=2018-09-04T11%3A16%3A02.383Z", nil) //?&filter=metric.type%20%3D%20%22compute.googleapis.com%2Finstance%2Fcpu%2Fusage_time%22&aggregation.perSeriesAligner=ALIGN_NONE&interval.startTime=2018-09-04T11%3A14%3A02.383Z&interval.endTime=2018-09-04T11%3A16%3A02.383Z
req, err := http.NewRequest(http.MethodGet, "https://monitoring.googleapis.com/v3/projects/raintank-production/timeSeries", nil)
if err != nil { if err != nil {
glog.Info("Failed to create request", "error", err) glog.Info("Failed to create request", "error", err)
return nil, fmt.Errorf("Failed to create request. error: %v", err) return nil, fmt.Errorf("Failed to create request. error: %v", err)
......
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