elasticsearch.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package elasticsearch
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/davecgh/go-spew/spew"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/setting"
  11. "github.com/grafana/grafana/pkg/tsdb"
  12. "golang.org/x/net/context/ctxhttp"
  13. "net/http"
  14. "net/url"
  15. "path"
  16. "strings"
  17. "time"
  18. )
  19. type ElasticsearchExecutor struct {
  20. Transport *http.Transport
  21. }
  22. var (
  23. glog log.Logger
  24. intervalCalculator tsdb.IntervalCalculator
  25. )
  26. func NewElasticsearchExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
  27. transport, err := dsInfo.GetHttpTransport()
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &ElasticsearchExecutor{
  32. Transport: transport,
  33. }, nil
  34. }
  35. func init() {
  36. glog = log.New("tsdb.elasticsearch")
  37. tsdb.RegisterTsdbQueryEndpoint("elasticsearch", NewElasticsearchExecutor)
  38. intervalCalculator = tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{MinInterval: time.Millisecond * 1})
  39. }
  40. func (e *ElasticsearchExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
  41. result := &tsdb.Response{}
  42. result.Results = make(map[string]*tsdb.QueryResult)
  43. queryParser := ElasticSearchQueryParser{
  44. dsInfo,
  45. tsdbQuery.TimeRange,
  46. tsdbQuery.Queries,
  47. glog,
  48. }
  49. glog.Warn(spew.Sdump(dsInfo))
  50. glog.Warn(spew.Sdump(tsdbQuery))
  51. payload, err := queryParser.Parse()
  52. if err != nil {
  53. return nil, err
  54. }
  55. if setting.Env == setting.DEV {
  56. glog.Debug("Elasticsearch playload", "raw playload", payload)
  57. }
  58. glog.Info("Elasticsearch playload", "raw playload", payload)
  59. req, err := e.createRequest(dsInfo, payload)
  60. if err != nil {
  61. return nil, err
  62. }
  63. httpClient, err := dsInfo.GetHttpClient()
  64. if err != nil {
  65. return nil, err
  66. }
  67. resp, err := ctxhttp.Do(ctx, httpClient, req)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if resp.StatusCode/100 != 2 {
  72. return nil, fmt.Errorf("elasticsearch returned statuscode invalid status code: %v", resp.Status)
  73. }
  74. var responses Responses
  75. dec := json.NewDecoder(resp.Body)
  76. defer resp.Body.Close()
  77. dec.UseNumber()
  78. err = dec.Decode(&responses)
  79. if err != nil {
  80. return nil, err
  81. }
  82. glog.Warn(spew.Sdump(responses))
  83. for _, res := range responses.Responses {
  84. if res.Err != nil {
  85. return nil, errors.New(res.getErrMsg())
  86. }
  87. }
  88. return result, nil
  89. }
  90. func (e *ElasticsearchExecutor) createRequest(dsInfo *models.DataSource, query string) (*http.Request, error) {
  91. u, _ := url.Parse(dsInfo.Url)
  92. u.Path = path.Join(u.Path, "_msearch")
  93. req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(query))
  94. if err != nil {
  95. return nil, err
  96. }
  97. req.Header.Set("User-Agent", "Grafana")
  98. req.Header.Set("Content-Type", "application/json")
  99. if dsInfo.BasicAuth {
  100. req.SetBasicAuth(dsInfo.BasicAuthUser, dsInfo.BasicAuthPassword)
  101. }
  102. if !dsInfo.BasicAuth && dsInfo.User != "" {
  103. req.SetBasicAuth(dsInfo.User, dsInfo.Password)
  104. }
  105. glog.Debug("Elasticsearch request", "url", req.URL.String())
  106. glog.Debug("Elasticsearch request", "body", query)
  107. return req, nil
  108. }