diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index 366709cfba7..a8ba3c1a648 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -1,6 +1,11 @@ package tsdb import ( + "crypto/tls" + "net" + "net/http" + "time" + "github.com/grafana/grafana/pkg/components/simplejson" "gopkg.in/guregu/null.v3" ) @@ -42,6 +47,27 @@ type DataSourceInfo struct { JsonData *simplejson.Json } +func (ds *DataSourceInfo) GetDefaultClient() *http.Client { + tr := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + + return &http.Client{ + Timeout: time.Duration(30 * time.Second), + Transport: tr, + } +} + type BatchTiming struct { TimeElapsed int64 } diff --git a/pkg/tsdb/mqe/model_parser_test.go b/pkg/tsdb/mqe/model_parser_test.go index 13329e4c662..b88698cdbb9 100644 --- a/pkg/tsdb/mqe/model_parser_test.go +++ b/pkg/tsdb/mqe/model_parser_test.go @@ -16,6 +16,8 @@ func TestMQEQueryParser(t *testing.T) { JsonData: simplejson.New(), } + queryContext := &tsdb.QueryContext{} + Convey("can parse simple mqe model", func() { json := ` { @@ -23,10 +25,9 @@ func TestMQEQueryParser(t *testing.T) { "hosts": [ "staples-lab-1" ], - "metric": "$metric_cpu", "metrics": [ { - "metric": "$metric_cpu" + "metric": "os.cpu.all*" } ], "rawQuery": "", @@ -36,9 +37,11 @@ func TestMQEQueryParser(t *testing.T) { modelJson, err := simplejson.NewJson([]byte(json)) So(err, ShouldBeNil) - res, err := parser.Parse(modelJson, dsInfo) + query, err := parser.Parse(modelJson, dsInfo) So(err, ShouldBeNil) - So(res.Interval, ShouldEqual, ">20s") + + rawQuery := query.Build(queryContext) + So(rawQuery, ShouldEqual, "") }) Convey("can parse multi serie mqe model", func() { @@ -62,6 +65,8 @@ func TestMQEQueryParser(t *testing.T) { "addHostToAlias": true } ` + + So(json, ShouldNotBeNil) }) }) } diff --git a/pkg/tsdb/mqe/mqe.go b/pkg/tsdb/mqe/mqe.go index 1f77c9ab34d..094d5414c44 100644 --- a/pkg/tsdb/mqe/mqe.go +++ b/pkg/tsdb/mqe/mqe.go @@ -29,5 +29,6 @@ func init() { } func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult { + return &tsdb.BatchResult{} } diff --git a/pkg/tsdb/mqe/token_client.go b/pkg/tsdb/mqe/token_client.go new file mode 100644 index 00000000000..618ce671bb2 --- /dev/null +++ b/pkg/tsdb/mqe/token_client.go @@ -0,0 +1,61 @@ +package mqe + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "path" + + "golang.org/x/net/context/ctxhttp" + + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/tsdb" +) + +type TokenClient struct { + tlog log.Logger +} + +func NewTokenClient() *TokenClient { + return &TokenClient{ + tlog: log.New("tsdb.mqe.tokenclient"), + } +} + +func (client *TokenClient) GetTokenData(ctx context.Context, datasource *tsdb.DataSourceInfo) (*TokenResponse, error) { + u, _ := url.Parse(datasource.Url) + u.Path = path.Join(u.Path, "token") + + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + client.tlog.Info("Failed to create request", "error", err) + } + + res, err := ctxhttp.Do(ctx, HttpClient, req) + if err != nil { + return nil, err + } + + body, err := ioutil.ReadAll(res.Body) + defer res.Body.Close() + if err != nil { + return nil, err + } + + if res.StatusCode/100 != 2 { + client.tlog.Info("Request failed", "status", res.Status, "body", string(body)) + return nil, fmt.Errorf("Request failed status: %v", res.Status) + } + + var result *TokenResponse + err = json.Unmarshal(body, &result) + if err != nil { + client.tlog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body)) + return nil, err + } + + return result, nil +} diff --git a/pkg/tsdb/mqe/token_client_test.go b/pkg/tsdb/mqe/token_client_test.go new file mode 100644 index 00000000000..5265a64e513 --- /dev/null +++ b/pkg/tsdb/mqe/token_client_test.go @@ -0,0 +1,28 @@ +package mqe + +import ( + "context" + "testing" + + "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/tsdb" + . "github.com/smartystreets/goconvey/convey" +) + +func TestTokenClient(t *testing.T) { + SkipConvey("Token client", t, func() { + dsInfo := &tsdb.DataSourceInfo{ + JsonData: simplejson.New(), + Url: "", + } + + client := NewTokenClient() + + body, err := client.GetTokenData(context.TODO(), dsInfo) + + So(err, ShouldBeNil) + So(len(body.Body.Functions), ShouldBeGreaterThan, 1) + So(len(body.Body.Metrics), ShouldBeGreaterThan, 1) + So(body.Success, ShouldBeTrue) + }) +} diff --git a/pkg/tsdb/mqe/types.go b/pkg/tsdb/mqe/types.go index eaebd89993c..1c53ae62c5b 100644 --- a/pkg/tsdb/mqe/types.go +++ b/pkg/tsdb/mqe/types.go @@ -1,7 +1,26 @@ package mqe +import ( + "github.com/grafana/grafana/pkg/tsdb" +) + type MQEQuery struct { Metrics []string Hosts []string Apps []string } + +func (q *MQEQuery) Build(queryContext *tsdb.QueryContext) string { + return "" +} + +type TokenBody struct { + Functions []string + Metrics []string + //tagset +} + +type TokenResponse struct { + Success bool + Body TokenBody +}