Merge branch 'master' into alerting_opentsdb

This commit is contained in:
bergquist
2016-10-03 10:36:33 +02:00
70 changed files with 965 additions and 522 deletions
+8 -5
View File
@@ -1,6 +1,9 @@
package tsdb
import "errors"
import (
"context"
"errors"
)
type Batch struct {
DataSourceId int64
@@ -20,7 +23,7 @@ func newBatch(dsId int64, queries QuerySlice) *Batch {
}
}
func (bg *Batch) process(context *QueryContext) {
func (bg *Batch) process(ctx context.Context, queryContext *QueryContext) {
executor := getExecutorFor(bg.Queries[0].DataSource)
if executor == nil {
@@ -32,13 +35,13 @@ func (bg *Batch) process(context *QueryContext) {
for _, query := range bg.Queries {
result.QueryResults[query.RefId] = &QueryResult{Error: result.Error}
}
context.ResultsChan <- result
queryContext.ResultsChan <- result
return
}
res := executor.Execute(bg.Queries, context)
res := executor.Execute(ctx, bg.Queries, queryContext)
bg.Done = true
context.ResultsChan <- res
queryContext.ResultsChan <- res
}
func (bg *Batch) addQuery(query *Query) {
+3 -1
View File
@@ -1,7 +1,9 @@
package tsdb
import "context"
type Executor interface {
Execute(queries QuerySlice, context *QueryContext) *BatchResult
Execute(ctx context.Context, queries QuerySlice, context *QueryContext) *BatchResult
}
var registry map[string]GetExecutorFn
+3 -1
View File
@@ -1,5 +1,7 @@
package tsdb
import "context"
type FakeExecutor struct {
results map[string]*QueryResult
resultsFn map[string]ResultsFn
@@ -14,7 +16,7 @@ func NewFakeExecutor(dsInfo *DataSourceInfo) *FakeExecutor {
}
}
func (e *FakeExecutor) Execute(queries QuerySlice, context *QueryContext) *BatchResult {
func (e *FakeExecutor) Execute(ctx context.Context, queries QuerySlice, context *QueryContext) *BatchResult {
result := &BatchResult{QueryResults: make(map[string]*QueryResult)}
for _, query := range queries {
if results, has := e.results[query.RefId]; has {
+8 -4
View File
@@ -1,6 +1,7 @@
package graphite
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
@@ -11,6 +12,8 @@ import (
"strings"
"time"
"golang.org/x/net/context/ctxhttp"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb"
@@ -26,7 +29,7 @@ func NewGraphiteExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor {
var (
glog log.Logger
HttpClient http.Client
HttpClient *http.Client
)
func init() {
@@ -37,13 +40,13 @@ func init() {
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
HttpClient = http.Client{
HttpClient = &http.Client{
Timeout: time.Duration(15 * time.Second),
Transport: tr,
}
}
func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
func (e *GraphiteExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
result := &tsdb.BatchResult{}
formData := url.Values{
@@ -66,7 +69,8 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
result.Error = err
return result
}
res, err := HttpClient.Do(req)
res, err := ctxhttp.Do(ctx, HttpClient, req)
if err != nil {
result.Error = err
return result
+3 -3
View File
@@ -1,6 +1,7 @@
package prometheus
import (
"context"
"fmt"
"net/http"
"regexp"
@@ -11,7 +12,6 @@ import (
"github.com/grafana/grafana/pkg/tsdb"
"github.com/prometheus/client_golang/api/prometheus"
pmodel "github.com/prometheus/common/model"
"golang.org/x/net/context"
)
type PrometheusExecutor struct {
@@ -45,7 +45,7 @@ func (e *PrometheusExecutor) getClient() (prometheus.QueryAPI, error) {
return prometheus.NewQueryAPI(client), nil
}
func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
func (e *PrometheusExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
result := &tsdb.BatchResult{}
client, err := e.getClient()
@@ -64,7 +64,7 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
Step: query.Step,
}
value, err := client.QueryRange(context.Background(), query.Expr, timeRange)
value, err := client.QueryRange(ctx, query.Expr, timeRange)
if err != nil {
return resultWithError(result, err)
+6 -4
View File
@@ -1,8 +1,10 @@
package tsdb
type HandleRequestFunc func(req *Request) (*Response, error)
import "context"
func HandleRequest(req *Request) (*Response, error) {
type HandleRequestFunc func(ctx context.Context, req *Request) (*Response, error)
func HandleRequest(ctx context.Context, req *Request) (*Response, error) {
context := NewQueryContext(req.Queries, req.TimeRange)
batches, err := getBatches(req)
@@ -16,7 +18,7 @@ func HandleRequest(req *Request) (*Response, error) {
if len(batch.Depends) == 0 {
currentlyExecuting += 1
batch.Started = true
go batch.process(context)
go batch.process(ctx, context)
}
}
@@ -46,7 +48,7 @@ func HandleRequest(req *Request) (*Response, error) {
if batch.allDependenciesAreIn(context) {
currentlyExecuting += 1
batch.Started = true
go batch.process(context)
go batch.process(ctx, context)
}
}
}
+3 -1
View File
@@ -1,6 +1,8 @@
package testdata
import (
"context"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/tsdb"
)
@@ -21,7 +23,7 @@ func init() {
tsdb.RegisterExecutor("grafana-testdata-datasource", NewTestDataExecutor)
}
func (e *TestDataExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
func (e *TestDataExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
result := &tsdb.BatchResult{}
result.QueryResults = make(map[string]*tsdb.QueryResult)
+6 -5
View File
@@ -1,6 +1,7 @@
package tsdb
import (
"context"
"testing"
"time"
@@ -62,7 +63,7 @@ func TestMetricQuery(t *testing.T) {
fakeExecutor := registerFakeExecutor()
fakeExecutor.Return("A", TimeSeriesSlice{&TimeSeries{Name: "argh"}})
res, err := HandleRequest(req)
res, err := HandleRequest(context.TODO(), req)
So(err, ShouldBeNil)
Convey("Should return query results", func() {
@@ -83,7 +84,7 @@ func TestMetricQuery(t *testing.T) {
fakeExecutor.Return("A", TimeSeriesSlice{&TimeSeries{Name: "argh"}})
fakeExecutor.Return("B", TimeSeriesSlice{&TimeSeries{Name: "barg"}})
res, err := HandleRequest(req)
res, err := HandleRequest(context.TODO(), req)
So(err, ShouldBeNil)
Convey("Should return query results", func() {
@@ -106,7 +107,7 @@ func TestMetricQuery(t *testing.T) {
},
}
res, err := HandleRequest(req)
res, err := HandleRequest(context.TODO(), req)
So(err, ShouldBeNil)
Convey("Should have been batched in two requests", func() {
@@ -121,7 +122,7 @@ func TestMetricQuery(t *testing.T) {
},
}
_, err := HandleRequest(req)
_, err := HandleRequest(context.TODO(), req)
So(err, ShouldNotBeNil)
})
@@ -152,7 +153,7 @@ func TestMetricQuery(t *testing.T) {
}}
})
res, err := HandleRequest(req)
res, err := HandleRequest(context.TODO(), req)
So(err, ShouldBeNil)
Convey("Should have been batched in two requests", func() {