|
|
@@ -36,7 +36,7 @@ func init() {
|
|
|
func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
|
|
|
result := &tsdb.BatchResult{}
|
|
|
|
|
|
- params := url.Values{
|
|
|
+ formData := url.Values{
|
|
|
"from": []string{"-" + formatTimeRange(context.TimeRange.From)},
|
|
|
"until": []string{formatTimeRange(context.TimeRange.To)},
|
|
|
"format": []string{"json"},
|
|
|
@@ -44,63 +44,79 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
|
|
|
}
|
|
|
|
|
|
for _, query := range queries {
|
|
|
- params["target"] = []string{query.Query}
|
|
|
+ formData["target"] = []string{query.Query}
|
|
|
}
|
|
|
|
|
|
- u, _ := url.Parse(e.Url)
|
|
|
- u.Path = path.Join(u.Path, "render")
|
|
|
- glog.Info("Graphite request body", "formdata", params.Encode())
|
|
|
+ glog.Info("Graphite request body", "formdata", formData.Encode())
|
|
|
|
|
|
- req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
|
|
|
+ req, err := e.createRequest(formData)
|
|
|
if err != nil {
|
|
|
- glog.Info("Failed to create request", "error", err)
|
|
|
- result.Error = fmt.Errorf("Failed to create request. error: %v", err)
|
|
|
+ result.Error = err
|
|
|
return result
|
|
|
}
|
|
|
-
|
|
|
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
- if e.BasicAuth {
|
|
|
- req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
|
|
|
+ res, err := HttpClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ result.Error = err
|
|
|
+ return result
|
|
|
}
|
|
|
|
|
|
- res, err := HttpClient.Do(req)
|
|
|
+ data, err := e.parseResponse(res)
|
|
|
if err != nil {
|
|
|
result.Error = err
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
+ result.QueryResults = make(map[string]*tsdb.QueryResult)
|
|
|
+ queryRes := &tsdb.QueryResult{}
|
|
|
+ for _, series := range data {
|
|
|
+ queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
|
|
|
+ Name: series.Target,
|
|
|
+ Points: series.DataPoints,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ result.QueryResults["A"] = queryRes
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+func (e *GraphiteExecutor) parseResponse(res *http.Response) ([]TargetResponseDTO, error) {
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
defer res.Body.Close()
|
|
|
if err != nil {
|
|
|
- result.Error = err
|
|
|
- return result
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
if res.StatusCode == http.StatusUnauthorized {
|
|
|
glog.Info("Request is Unauthorized", "status", res.Status, "body", string(body))
|
|
|
- result.Error = fmt.Errorf("Request is Unauthorized status: %v body: %s", res.Status, string(body))
|
|
|
- return result
|
|
|
+ return nil, fmt.Errorf("Request is Unauthorized status: %v body: %s", res.Status, string(body))
|
|
|
}
|
|
|
|
|
|
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))
|
|
|
- result.Error = err
|
|
|
- return result
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- result.QueryResults = make(map[string]*tsdb.QueryResult)
|
|
|
- queryRes := &tsdb.QueryResult{}
|
|
|
- for _, series := range data {
|
|
|
- queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
|
|
|
- Name: series.Target,
|
|
|
- Points: series.DataPoints,
|
|
|
- })
|
|
|
+ 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)
|
|
|
}
|
|
|
|
|
|
- result.QueryResults["A"] = queryRes
|
|
|
- return result
|
|
|
+ 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 {
|