Просмотр исходного кода

tech(alerting): replace goreq with native http

bergquist 9 лет назад
Родитель
Сommit
5bbfe39f84
1 измененных файлов с 19 добавлено и 12 удалено
  1. 19 12
      pkg/services/alerting/datasources/graphite.go

+ 19 - 12
pkg/services/alerting/datasources/graphite.go

@@ -1,15 +1,17 @@
 package graphite
 
 import (
+	"bytes"
+	"encoding/json"
 	"fmt"
+	"io/ioutil"
 	"net/http"
 	"net/url"
 	"strconv"
 	"time"
 
-	"github.com/franela/goreq"
-	"github.com/grafana/grafana/pkg/cmd/grafana-cli/log"
 	"github.com/grafana/grafana/pkg/components/simplejson"
+	"github.com/grafana/grafana/pkg/log"
 	m "github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/util"
 )
@@ -21,6 +23,10 @@ type GraphiteSerie struct {
 	Target     string
 }
 
+var DefaultClient = &http.Client{
+	Timeout: time.Minute,
+}
+
 type GraphiteResponse []GraphiteSerie
 
 func (client GraphiteClient) GetSeries(rule m.AlertJob, datasource m.DataSource) (m.TimeSeriesSlice, error) {
@@ -31,20 +37,21 @@ func (client GraphiteClient) GetSeries(rule m.AlertJob, datasource m.DataSource)
 		"from":   []string{"-" + strconv.Itoa(rule.Rule.QueryRange) + "s"},
 	}
 
-	log.Debug("Graphite: sending request with querystring: ", v.Encode())
+	log.Trace("Graphite: sending request with querystring: ", v.Encode())
+
+	req, err := http.NewRequest("POST", datasource.Url+"/render", nil)
 
-	req := goreq.Request{
-		Method:  "POST",
-		Uri:     datasource.Url + "/render",
-		Body:    v.Encode(),
-		Timeout: 5 * time.Second,
+	if err != nil {
+		return nil, fmt.Errorf("Could not create request")
 	}
 
+	req.Body = ioutil.NopCloser(bytes.NewReader([]byte(v.Encode())))
+
 	if datasource.BasicAuth {
-		req.AddHeader("Authorization", util.GetBasicAuthHeader(datasource.User, datasource.Password))
+		req.Header.Add("Authorization", util.GetBasicAuthHeader(datasource.User, datasource.Password))
 	}
 
-	res, err := req.Do()
+	res, err := DefaultClient.Do(req)
 
 	if err != nil {
 		return nil, err
@@ -55,10 +62,10 @@ func (client GraphiteClient) GetSeries(rule m.AlertJob, datasource m.DataSource)
 	}
 
 	response := GraphiteResponse{}
-	res.Body.FromJsonTo(&response)
 
-	timeSeries := make([]*m.TimeSeries, 0)
+	json.NewDecoder(res.Body).Decode(&response)
 
+	var timeSeries []*m.TimeSeries
 	for _, v := range response {
 		timeSeries = append(timeSeries, m.NewTimeSeries(v.Target, v.Datapoints))
 	}