2016-10-04 09:25:33 -05:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2016-10-06 05:51:45 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-10-04 09:25:33 -05:00
|
|
|
"net/http"
|
2016-10-06 05:51:45 -05:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2016-10-04 09:25:33 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-12-07 04:10:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-11-08 12:56:57 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-10-04 09:25:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2019-03-25 11:42:27 -05:00
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2016-10-04 09:25:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type InfluxDBExecutor struct {
|
2017-09-21 08:03:47 -05:00
|
|
|
//*models.DataSource
|
2016-10-06 08:30:09 -05:00
|
|
|
QueryParser *InfluxdbQueryParser
|
|
|
|
ResponseParser *ResponseParser
|
2017-09-21 08:03:47 -05:00
|
|
|
//HttpClient *http.Client
|
2016-10-04 09:25:33 -05:00
|
|
|
}
|
|
|
|
|
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{},
|
2016-10-06 08:30:09 -05:00
|
|
|
ResponseParser: &ResponseParser{},
|
2016-12-07 04:10:42 -06:00
|
|
|
}, nil
|
2016-10-04 09:25:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-12-07 04:10:42 -06:00
|
|
|
glog log.Logger
|
2016-10-04 09:25:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
glog = log.New("tsdb.influxdb")
|
2017-09-21 03:44:25 -05:00
|
|
|
tsdb.RegisterTsdbQueryEndpoint("influxdb", NewInfluxDBExecutor)
|
2016-10-04 09:25:33 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
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 {
|
2017-09-21 11:04:06 -05:00
|
|
|
return nil, err
|
2016-10-10 04:34:52 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 08:23:34 -05:00
|
|
|
rawQuery, err := query.Build(tsdbQuery)
|
2016-11-08 12:56:57 -06:00
|
|
|
if err != nil {
|
2017-09-21 11:04:06 -05:00
|
|
|
return nil, err
|
2016-11-08 12:56:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if setting.Env == setting.DEV {
|
2016-11-10 05:22:17 -06:00
|
|
|
glog.Debug("Influxdb query", "raw query", rawQuery)
|
2016-11-08 12:56:57 -06:00
|
|
|
}
|
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 {
|
2017-09-21 11:04:06 -05:00
|
|
|
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 {
|
2017-09-21 11:04:06 -05:00
|
|
|
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 {
|
2017-09-21 11:04:06 -05:00
|
|
|
return nil, err
|
2016-10-10 04:34:52 -05:00
|
|
|
}
|
|
|
|
|
2019-03-25 11:42:27 -05:00
|
|
|
defer resp.Body.Close()
|
2016-10-10 04:34:52 -05:00
|
|
|
if resp.StatusCode/100 != 2 {
|
2017-09-21 11:04:06 -05:00
|
|
|
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 {
|
2017-09-21 11:04:06 -05:00
|
|
|
return nil, err
|
2016-10-10 04:34:52 -05:00
|
|
|
}
|
|
|
|
|
2016-11-11 01:46:52 -06:00
|
|
|
if response.Err != nil {
|
2017-09-21 11:04:06 -05:00
|
|
|
return nil, response.Err
|
2016-11-11 01:46:52 -06:00
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
result.Results = make(map[string]*tsdb.QueryResult)
|
|
|
|
result.Results["A"] = e.ResponseParser.Parse(&response, query)
|
2016-10-10 04:34:52 -05:00
|
|
|
|
2017-09-21 11:04:06 -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) {
|
2018-11-09 12:40:07 -06:00
|
|
|
// 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 {
|
2016-11-08 12:56:57 -06:00
|
|
|
return nil, err
|
2016-10-06 05:51:45 -05:00
|
|
|
}
|
2016-11-08 12:56:57 -06:00
|
|
|
return query, nil
|
2016-10-06 05:51:45 -05:00
|
|
|
}
|
2016-11-08 12:56:57 -06:00
|
|
|
return nil, fmt.Errorf("query request contains no queries")
|
2016-10-06 05:51:45 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2016-10-06 05:51:45 -05:00
|
|
|
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
|
2016-10-06 05:51:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
params := req.URL.Query()
|
|
|
|
params.Set("q", query)
|
2017-09-21 08:03:47 -05:00
|
|
|
params.Set("db", dsInfo.Database)
|
2016-10-06 05:51:45 -05:00
|
|
|
params.Set("epoch", "s")
|
|
|
|
req.URL.RawQuery = params.Encode()
|
2016-10-05 13:36:05 -05:00
|
|
|
|
2016-10-06 05:51:45 -05:00
|
|
|
req.Header.Set("User-Agent", "Grafana")
|
2016-10-18 08:12:01 -05:00
|
|
|
|
2017-09-21 08:03:47 -05:00
|
|
|
if dsInfo.BasicAuth {
|
2019-04-15 04:11:17 -05:00
|
|
|
req.SetBasicAuth(dsInfo.BasicAuthUser, dsInfo.DecryptedBasicAuthPassword())
|
2016-10-06 05:51:45 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 08:03:47 -05:00
|
|
|
if !dsInfo.BasicAuth && dsInfo.User != "" {
|
2019-04-15 04:11:17 -05:00
|
|
|
req.SetBasicAuth(dsInfo.User, dsInfo.DecryptedPassword())
|
2016-10-18 08:12:01 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|