Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xpack_watcher_stats.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2012-2018 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "net/url"
  10. )
  11. // XPackWatcherStatsService returns the current watcher metrics.
  12. // See https://www.elastic.co/guide/en/elasticsearch/reference/6.4/watcher-api-stats.html.
  13. type XPackWatcherStatsService struct {
  14. client *Client
  15. pretty bool
  16. metric string
  17. emitStacktraces *bool
  18. }
  19. // NewXPackWatcherStatsService creates a new XPackWatcherStatsService.
  20. func NewXPackWatcherStatsService(client *Client) *XPackWatcherStatsService {
  21. return &XPackWatcherStatsService{
  22. client: client,
  23. }
  24. }
  25. // Metric controls what additional stat metrics should be include in the response.
  26. func (s *XPackWatcherStatsService) Metric(metric string) *XPackWatcherStatsService {
  27. s.metric = metric
  28. return s
  29. }
  30. // EmitStacktraces, if enabled, emits stack traces of currently running watches.
  31. func (s *XPackWatcherStatsService) EmitStacktraces(emitStacktraces bool) *XPackWatcherStatsService {
  32. s.emitStacktraces = &emitStacktraces
  33. return s
  34. }
  35. // Pretty indicates that the JSON response be indented and human readable.
  36. func (s *XPackWatcherStatsService) Pretty(pretty bool) *XPackWatcherStatsService {
  37. s.pretty = pretty
  38. return s
  39. }
  40. // buildURL builds the URL for the operation.
  41. func (s *XPackWatcherStatsService) buildURL() (string, url.Values, error) {
  42. // Build URL
  43. path := "/_xpack/watcher/stats"
  44. // Add query string parameters
  45. params := url.Values{}
  46. if s.pretty {
  47. params.Set("pretty", "true")
  48. }
  49. if s.emitStacktraces != nil {
  50. params.Set("emit_stacktraces", fmt.Sprintf("%v", *s.emitStacktraces))
  51. }
  52. if s.metric != "" {
  53. params.Set("metric", s.metric)
  54. }
  55. return path, params, nil
  56. }
  57. // Validate checks if the operation is valid.
  58. func (s *XPackWatcherStatsService) Validate() error {
  59. return nil
  60. }
  61. // Do executes the operation.
  62. func (s *XPackWatcherStatsService) Do(ctx context.Context) (*XPackWatcherStatsResponse, error) {
  63. // Check pre-conditions
  64. if err := s.Validate(); err != nil {
  65. return nil, err
  66. }
  67. // Get URL for request
  68. path, params, err := s.buildURL()
  69. if err != nil {
  70. return nil, err
  71. }
  72. // Get HTTP response
  73. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  74. Method: "GET",
  75. Path: path,
  76. Params: params,
  77. })
  78. if err != nil {
  79. return nil, err
  80. }
  81. // Return operation response
  82. ret := new(XPackWatcherStatsResponse)
  83. if err := json.Unmarshal(res.Body, ret); err != nil {
  84. return nil, err
  85. }
  86. return ret, nil
  87. }
  88. // XPackWatcherStatsResponse is the response of XPackWatcherStatsService.Do.
  89. type XPackWatcherStatsResponse struct {
  90. Stats []XPackWatcherStats `json:"stats"`
  91. }
  92. // XPackWatcherStats represents the stats used in XPackWatcherStatsResponse.
  93. type XPackWatcherStats struct {
  94. WatcherState string `json:"watcher_state"`
  95. WatchCount int `json:"watch_count"`
  96. ExecutionThreadPool map[string]interface{} `json:"execution_thread_pool"`
  97. }