graphite.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package datasources
  2. // import (
  3. // "bytes"
  4. // "encoding/json"
  5. // "fmt"
  6. // "io/ioutil"
  7. // "net/http"
  8. // "net/url"
  9. // "strconv"
  10. // "time"
  11. //
  12. // "github.com/grafana/grafana/pkg/components/simplejson"
  13. // "github.com/grafana/grafana/pkg/log"
  14. // m "github.com/grafana/grafana/pkg/models"
  15. // "github.com/grafana/grafana/pkg/util"
  16. // )
  17. //
  18. // type GraphiteClient struct{}
  19. //
  20. // type GraphiteSerie struct {
  21. // Datapoints [][2]float64
  22. // Target string
  23. // }
  24. //
  25. // var DefaultClient = &http.Client{
  26. // Timeout: time.Minute,
  27. // }
  28. //
  29. // type GraphiteResponse []GraphiteSerie
  30. //
  31. // func (client GraphiteClient) GetSeries(rule m.AlertJob, datasource m.DataSource) (m.TimeSeriesSlice, error) {
  32. // v := url.Values{
  33. // "format": []string{"json"},
  34. // "target": []string{getTargetFromRule(rule.Rule)},
  35. // "until": []string{"now"},
  36. // "from": []string{"-" + strconv.Itoa(rule.Rule.QueryRange) + "s"},
  37. // }
  38. //
  39. // log.Trace("Graphite: sending request with querystring: ", v.Encode())
  40. //
  41. // req, err := http.NewRequest("POST", datasource.Url+"/render", nil)
  42. //
  43. // if err != nil {
  44. // return nil, fmt.Errorf("Could not create request")
  45. // }
  46. //
  47. // req.Body = ioutil.NopCloser(bytes.NewReader([]byte(v.Encode())))
  48. //
  49. // if datasource.BasicAuth {
  50. // req.Header.Add("Authorization", util.GetBasicAuthHeader(datasource.User, datasource.Password))
  51. // }
  52. //
  53. // res, err := DefaultClient.Do(req)
  54. //
  55. // if err != nil {
  56. // return nil, err
  57. // }
  58. //
  59. // if res.StatusCode != http.StatusOK {
  60. // return nil, fmt.Errorf("expected httpstatus 200, found %d", res.StatusCode)
  61. // }
  62. //
  63. // response := GraphiteResponse{}
  64. //
  65. // json.NewDecoder(res.Body).Decode(&response)
  66. //
  67. // var timeSeries []*m.TimeSeries
  68. // for _, v := range response {
  69. // timeSeries = append(timeSeries, m.NewTimeSeries(v.Target, v.Datapoints))
  70. // }
  71. //
  72. // return timeSeries, nil
  73. // }
  74. //
  75. // func getTargetFromRule(rule m.AlertRule) string {
  76. // json, _ := simplejson.NewJson([]byte(rule.Query))
  77. //
  78. // return json.Get("target").MustString()
  79. // }