feat(mqe): add token request

This commit is contained in:
bergquist 2016-11-14 14:00:35 +01:00
parent 118e2a6364
commit c973241435
6 changed files with 144 additions and 4 deletions

View File

@ -1,6 +1,11 @@
package tsdb package tsdb
import ( import (
"crypto/tls"
"net"
"net/http"
"time"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"gopkg.in/guregu/null.v3" "gopkg.in/guregu/null.v3"
) )
@ -42,6 +47,27 @@ type DataSourceInfo struct {
JsonData *simplejson.Json 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 { type BatchTiming struct {
TimeElapsed int64 TimeElapsed int64
} }

View File

@ -16,6 +16,8 @@ func TestMQEQueryParser(t *testing.T) {
JsonData: simplejson.New(), JsonData: simplejson.New(),
} }
queryContext := &tsdb.QueryContext{}
Convey("can parse simple mqe model", func() { Convey("can parse simple mqe model", func() {
json := ` json := `
{ {
@ -23,10 +25,9 @@ func TestMQEQueryParser(t *testing.T) {
"hosts": [ "hosts": [
"staples-lab-1" "staples-lab-1"
], ],
"metric": "$metric_cpu",
"metrics": [ "metrics": [
{ {
"metric": "$metric_cpu" "metric": "os.cpu.all*"
} }
], ],
"rawQuery": "", "rawQuery": "",
@ -36,9 +37,11 @@ func TestMQEQueryParser(t *testing.T) {
modelJson, err := simplejson.NewJson([]byte(json)) modelJson, err := simplejson.NewJson([]byte(json))
So(err, ShouldBeNil) So(err, ShouldBeNil)
res, err := parser.Parse(modelJson, dsInfo) query, err := parser.Parse(modelJson, dsInfo)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(res.Interval, ShouldEqual, ">20s")
rawQuery := query.Build(queryContext)
So(rawQuery, ShouldEqual, "")
}) })
Convey("can parse multi serie mqe model", func() { Convey("can parse multi serie mqe model", func() {
@ -62,6 +65,8 @@ func TestMQEQueryParser(t *testing.T) {
"addHostToAlias": true "addHostToAlias": true
} }
` `
So(json, ShouldNotBeNil)
}) })
}) })
} }

View File

@ -29,5 +29,6 @@ func init() {
} }
func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult { func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
return &tsdb.BatchResult{} return &tsdb.BatchResult{}
} }

View File

@ -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
}

View File

@ -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)
})
}

View File

@ -1,7 +1,26 @@
package mqe package mqe
import (
"github.com/grafana/grafana/pkg/tsdb"
)
type MQEQuery struct { type MQEQuery struct {
Metrics []string Metrics []string
Hosts []string Hosts []string
Apps []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
}