grafana/pkg/tsdb/influxdb/influxdb.go

138 lines
3.2 KiB
Go
Raw Normal View History

package influxdb
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb"
"golang.org/x/net/context/ctxhttp"
)
type InfluxDBExecutor struct {
2017-09-21 08:03:47 -05:00
//*models.DataSource
QueryParser *InfluxdbQueryParser
ResponseParser *ResponseParser
2017-09-21 08:03:47 -05:00
//HttpClient *http.Client
}
2017-09-21 03:44:25 -05:00
func NewInfluxDBExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
2016-10-05 03:56:34 -05:00
return &InfluxDBExecutor{
QueryParser: &InfluxdbQueryParser{},
ResponseParser: &ResponseParser{},
}, nil
}
var (
glog log.Logger
)
func init() {
glog = log.New("tsdb.influxdb")
2017-09-21 03:44:25 -05:00
tsdb.RegisterTsdbQueryEndpoint("influxdb", NewInfluxDBExecutor)
}
func (e *InfluxDBExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
result := &tsdb.Response{}
2016-10-10 04:34:52 -05:00
2017-09-21 08:23:34 -05:00
query, err := e.getQuery(dsInfo, tsdbQuery.Queries, tsdbQuery)
2016-10-10 04:34:52 -05:00
if err != nil {
return nil, err
2016-10-10 04:34:52 -05:00
}
2017-09-21 08:23:34 -05:00
rawQuery, err := query.Build(tsdbQuery)
if err != nil {
return nil, err
}
if setting.Env == setting.DEV {
glog.Debug("Influxdb query", "raw query", rawQuery)
}
2016-10-10 04:34:52 -05:00
2017-09-21 08:03:47 -05:00
req, err := e.createRequest(dsInfo, rawQuery)
if err != nil {
return nil, err
2017-09-21 08:03:47 -05:00
}
httpClient, err := dsInfo.GetHttpClient()
2016-10-10 04:34:52 -05:00
if err != nil {
return nil, err
2016-10-10 04:34:52 -05:00
}
2017-09-21 08:03:47 -05:00
resp, err := ctxhttp.Do(ctx, httpClient, req)
2016-10-10 04:34:52 -05:00
if err != nil {
return nil, err
2016-10-10 04:34:52 -05:00
}
defer resp.Body.Close()
2016-10-10 04:34:52 -05:00
if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("Influxdb returned statuscode invalid status code: %v", resp.Status)
2016-10-10 04:34:52 -05:00
}
var response Response
dec := json.NewDecoder(resp.Body)
dec.UseNumber()
err = dec.Decode(&response)
2016-11-08 12:16:37 -06:00
2016-10-10 04:34:52 -05:00
if err != nil {
return nil, err
2016-10-10 04:34:52 -05:00
}
if response.Err != nil {
return nil, response.Err
}
result.Results = make(map[string]*tsdb.QueryResult)
result.Results["A"] = e.ResponseParser.Parse(&response, query)
2016-10-10 04:34:52 -05:00
return result, nil
2016-10-10 04:34:52 -05:00
}
2017-09-21 08:03:47 -05:00
func (e *InfluxDBExecutor) getQuery(dsInfo *models.DataSource, queries []*tsdb.Query, context *tsdb.TsdbQuery) (*Query, error) {
// The model supports multiple queries, but right now this is only used from
// alerting so we only needed to support batch executing 1 query at a time.
if len(queries) > 0 {
query, err := e.QueryParser.Parse(queries[0].Model, dsInfo)
2016-10-04 14:28:05 -05:00
if err != nil {
return nil, err
}
return query, nil
}
return nil, fmt.Errorf("query request contains no queries")
}
2017-09-21 08:03:47 -05:00
func (e *InfluxDBExecutor) createRequest(dsInfo *models.DataSource, query string) (*http.Request, error) {
u, _ := url.Parse(dsInfo.Url)
u.Path = path.Join(u.Path, "query")
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
2016-10-06 07:16:26 -05:00
return nil, err
}
params := req.URL.Query()
params.Set("q", query)
2017-09-21 08:03:47 -05:00
params.Set("db", dsInfo.Database)
params.Set("epoch", "s")
req.URL.RawQuery = params.Encode()
req.Header.Set("User-Agent", "Grafana")
2017-09-21 08:03:47 -05:00
if dsInfo.BasicAuth {
req.SetBasicAuth(dsInfo.BasicAuthUser, dsInfo.DecryptedBasicAuthPassword())
}
2017-09-21 08:03:47 -05:00
if !dsInfo.BasicAuth && dsInfo.User != "" {
req.SetBasicAuth(dsInfo.User, dsInfo.DecryptedPassword())
}
2016-10-10 04:58:06 -05:00
glog.Debug("Influxdb request", "url", req.URL.String())
2016-10-06 07:16:26 -05:00
return req, nil
}