elasticsearch.go 3.0 KB

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