graphite.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package graphite
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "github.com/grafana/grafana/pkg/log"
  11. "github.com/grafana/grafana/pkg/tsdb"
  12. )
  13. var (
  14. HttpClient = http.Client{Timeout: time.Duration(10 * time.Second)}
  15. )
  16. type GraphiteExecutor struct {
  17. *tsdb.DataSourceInfo
  18. }
  19. func NewGraphiteExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor {
  20. return &GraphiteExecutor{dsInfo}
  21. }
  22. var glog log.Logger
  23. func init() {
  24. glog = log.New("tsdb.graphite")
  25. tsdb.RegisterExecutor("graphite", NewGraphiteExecutor)
  26. }
  27. func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
  28. result := &tsdb.BatchResult{}
  29. params := url.Values{
  30. "from": []string{"-" + formatTimeRange(context.TimeRange.From)},
  31. "until": []string{formatTimeRange(context.TimeRange.To)},
  32. "format": []string{"json"},
  33. "maxDataPoints": []string{"500"},
  34. }
  35. for _, query := range queries {
  36. params["target"] = []string{query.Query}
  37. glog.Debug("Graphite request", "query", query.Query)
  38. }
  39. formData := params.Encode()
  40. glog.Info("Graphite request body", "formdata", formData)
  41. req, err := http.NewRequest(http.MethodPost, e.Url+"/render", strings.NewReader(formData))
  42. if err != nil {
  43. glog.Info("Failed to create request", "error", err)
  44. result.Error = fmt.Errorf("Failed to create request. error: %v", err)
  45. return result
  46. }
  47. if e.BasicAuth {
  48. req.SetBasicAuth(e.BasicAuthUser, e.BasicAuthPassword)
  49. }
  50. res, err := HttpClient.Do(req)
  51. if err != nil {
  52. result.Error = err
  53. return result
  54. }
  55. body, err := ioutil.ReadAll(res.Body)
  56. defer res.Body.Close()
  57. if err != nil {
  58. result.Error = err
  59. return result
  60. }
  61. if res.StatusCode == http.StatusUnauthorized {
  62. glog.Info("Request is Unauthorized", "status", res.Status, "body", string(body))
  63. result.Error = fmt.Errorf("Request is Unauthorized status: %v body: %s", res.Status, string(body))
  64. return result
  65. }
  66. var data []TargetResponseDTO
  67. err = json.Unmarshal(body, &data)
  68. if err != nil {
  69. glog.Info("Failed to unmarshal graphite response", "error", err, "status", res.Status, "body", string(body))
  70. result.Error = err
  71. return result
  72. }
  73. result.QueryResults = make(map[string]*tsdb.QueryResult)
  74. queryRes := &tsdb.QueryResult{}
  75. for _, series := range data {
  76. queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
  77. Name: series.Target,
  78. Points: series.DataPoints,
  79. })
  80. }
  81. result.QueryResults["A"] = queryRes
  82. return result
  83. }
  84. func formatTimeRange(input string) string {
  85. if input == "now" {
  86. return input
  87. }
  88. return strings.Replace(strings.Replace(input, "m", "min", -1), "M", "mon", -1)
  89. }