bergquist 9 лет назад
Родитель
Сommit
c973241435

+ 26 - 0
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
 }

+ 9 - 4
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)
 		})
 	})
 }

+ 1 - 0
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{}
 }

+ 61 - 0
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
+}

+ 28 - 0
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)
+	})
+}

+ 19 - 0
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
+}