grafana/pkg/tsdb/graphite/graphite.go

185 lines
4.4 KiB
Go
Raw Normal View History

package graphite
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"regexp"
"strings"
"golang.org/x/net/context/ctxhttp"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb"
opentracing "github.com/opentracing/opentracing-go"
)
type GraphiteExecutor struct {
*models.DataSource
HttpClient *http.Client
}
2017-09-21 03:44:25 -05:00
func NewGraphiteExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
httpClient, err := datasource.GetHttpClient()
if err != nil {
return nil, err
}
return &GraphiteExecutor{
DataSource: datasource,
HttpClient: httpClient,
}, nil
}
var (
glog log.Logger
)
func init() {
glog = log.New("tsdb.graphite")
2017-09-21 03:44:25 -05:00
tsdb.RegisterTsdbQueryEndpoint("graphite", NewGraphiteExecutor)
}
2017-09-21 03:44:25 -05:00
func (e *GraphiteExecutor) Query(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
result := &tsdb.BatchResult{}
from := "-" + formatTimeRange(context.TimeRange.From)
until := formatTimeRange(context.TimeRange.To)
var target string
2016-09-01 06:03:15 -05:00
formData := url.Values{
"from": []string{from},
"until": []string{until},
"format": []string{"json"},
"maxDataPoints": []string{"500"},
}
2017-09-20 11:56:33 -05:00
for _, query := range context.Queries {
if fullTarget, err := query.Model.Get("targetFull").String(); err == nil {
target = fixIntervalFormat(fullTarget)
} else {
target = fixIntervalFormat(query.Model.Get("target").MustString())
}
}
formData["target"] = []string{target}
if setting.Env == setting.DEV {
glog.Debug("Graphite request", "params", formData)
}
2016-09-01 06:03:15 -05:00
req, err := e.createRequest(formData)
2016-09-01 03:09:13 -05:00
if err != nil {
2016-09-01 06:03:15 -05:00
result.Error = err
2016-09-01 03:09:13 -05:00
return result
}
2017-09-11 13:57:08 -05:00
span, ctx := opentracing.StartSpanFromContext(ctx, "graphite query")
2017-09-11 11:48:20 -05:00
span.SetTag("target", target)
span.SetTag("from", from)
span.SetTag("until", until)
defer span.Finish()
opentracing.GlobalTracer().Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header))
res, err := ctxhttp.Do(ctx, e.HttpClient, req)
2016-09-01 06:03:15 -05:00
if err != nil {
result.Error = err
return result
}
2016-09-01 06:03:15 -05:00
data, err := e.parseResponse(res)
if err != nil {
result.Error = err
return result
}
2016-09-01 06:03:15 -05:00
result.QueryResults = make(map[string]*tsdb.QueryResult)
queryRes := tsdb.NewQueryResult()
2016-09-01 06:03:15 -05:00
for _, series := range data {
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
Name: series.Target,
Points: series.DataPoints,
})
if setting.Env == setting.DEV {
glog.Debug("Graphite response", "target", series.Target, "datapoints", len(series.DataPoints))
}
2016-09-01 06:03:15 -05:00
}
result.QueryResults["A"] = queryRes
return result
}
func (e *GraphiteExecutor) parseResponse(res *http.Response) ([]TargetResponseDTO, error) {
body, err := ioutil.ReadAll(res.Body)
2016-09-01 03:09:13 -05:00
defer res.Body.Close()
if err != nil {
2016-09-01 06:03:15 -05:00
return nil, err
}
2016-09-27 04:13:13 -05:00
if res.StatusCode/100 != 2 {
glog.Info("Request failed", "status", res.Status, "body", string(body))
return nil, fmt.Errorf("Request failed status: %v", res.Status)
}
var data []TargetResponseDTO
err = json.Unmarshal(body, &data)
if err != nil {
glog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body))
2016-09-01 06:03:15 -05:00
return nil, err
}
2016-09-01 06:03:15 -05:00
return data, nil
}
func (e *GraphiteExecutor) createRequest(data url.Values) (*http.Request, error) {
u, _ := url.Parse(e.Url)
u.Path = path.Join(u.Path, "render")
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(data.Encode()))
if err != nil {
glog.Info("Failed to create request", "error", err)
return nil, fmt.Errorf("Failed to create request. error: %v", err)
}
2016-09-01 06:03:15 -05:00
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if e.BasicAuth {
req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
}
return req, err
}
func formatTimeRange(input string) string {
if input == "now" {
return input
}
return strings.Replace(strings.Replace(input, "m", "min", -1), "M", "mon", -1)
}
func fixIntervalFormat(target string) string {
rMinute := regexp.MustCompile(`'(\d+)m'`)
rMin := regexp.MustCompile("m")
target = rMinute.ReplaceAllStringFunc(target, func(m string) string {
return rMin.ReplaceAllString(m, "min")
})
rMonth := regexp.MustCompile(`'(\d+)M'`)
rMon := regexp.MustCompile("M")
target = rMonth.ReplaceAllStringFunc(target, func(M string) string {
return rMon.ReplaceAllString(M, "mon")
})
return target
}