api.xpack.watcher.stats.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "net/http"
  23. "strconv"
  24. "strings"
  25. )
  26. func newWatcherStatsFunc(t Transport) WatcherStats {
  27. return func(o ...func(*WatcherStatsRequest)) (*Response, error) {
  28. var r = WatcherStatsRequest{}
  29. for _, f := range o {
  30. f(&r)
  31. }
  32. return r.Do(r.ctx, t)
  33. }
  34. }
  35. // ----- API Definition -------------------------------------------------------
  36. // WatcherStats - Retrieves the current Watcher metrics.
  37. //
  38. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-stats.html.
  39. type WatcherStats func(o ...func(*WatcherStatsRequest)) (*Response, error)
  40. // WatcherStatsRequest configures the Watcher Stats API request.
  41. type WatcherStatsRequest struct {
  42. Metric []string
  43. EmitStacktraces *bool
  44. Pretty bool
  45. Human bool
  46. ErrorTrace bool
  47. FilterPath []string
  48. Header http.Header
  49. ctx context.Context
  50. }
  51. // Do executes the request and returns response or error.
  52. func (r WatcherStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  53. var (
  54. method string
  55. path strings.Builder
  56. params map[string]string
  57. )
  58. method = "GET"
  59. path.Grow(1 + len("_watcher") + 1 + len("stats") + 1 + len(strings.Join(r.Metric, ",")))
  60. path.WriteString("/")
  61. path.WriteString("_watcher")
  62. path.WriteString("/")
  63. path.WriteString("stats")
  64. if len(r.Metric) > 0 {
  65. path.WriteString("/")
  66. path.WriteString(strings.Join(r.Metric, ","))
  67. }
  68. params = make(map[string]string)
  69. if r.EmitStacktraces != nil {
  70. params["emit_stacktraces"] = strconv.FormatBool(*r.EmitStacktraces)
  71. }
  72. if len(r.Metric) > 0 {
  73. params["metric"] = strings.Join(r.Metric, ",")
  74. }
  75. if r.Pretty {
  76. params["pretty"] = "true"
  77. }
  78. if r.Human {
  79. params["human"] = "true"
  80. }
  81. if r.ErrorTrace {
  82. params["error_trace"] = "true"
  83. }
  84. if len(r.FilterPath) > 0 {
  85. params["filter_path"] = strings.Join(r.FilterPath, ",")
  86. }
  87. req, err := newRequest(method, path.String(), nil)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if len(params) > 0 {
  92. q := req.URL.Query()
  93. for k, v := range params {
  94. q.Set(k, v)
  95. }
  96. req.URL.RawQuery = q.Encode()
  97. }
  98. if len(r.Header) > 0 {
  99. if len(req.Header) == 0 {
  100. req.Header = r.Header
  101. } else {
  102. for k, vv := range r.Header {
  103. for _, v := range vv {
  104. req.Header.Add(k, v)
  105. }
  106. }
  107. }
  108. }
  109. if ctx != nil {
  110. req = req.WithContext(ctx)
  111. }
  112. res, err := transport.Perform(req)
  113. if err != nil {
  114. return nil, err
  115. }
  116. response := Response{
  117. StatusCode: res.StatusCode,
  118. Body: res.Body,
  119. Header: res.Header,
  120. }
  121. return &response, nil
  122. }
  123. // WithContext sets the request context.
  124. func (f WatcherStats) WithContext(v context.Context) func(*WatcherStatsRequest) {
  125. return func(r *WatcherStatsRequest) {
  126. r.ctx = v
  127. }
  128. }
  129. // WithMetric - controls what additional stat metrics should be include in the response.
  130. func (f WatcherStats) WithMetric(v ...string) func(*WatcherStatsRequest) {
  131. return func(r *WatcherStatsRequest) {
  132. r.Metric = v
  133. }
  134. }
  135. // WithEmitStacktraces - emits stack traces of currently running watches.
  136. func (f WatcherStats) WithEmitStacktraces(v bool) func(*WatcherStatsRequest) {
  137. return func(r *WatcherStatsRequest) {
  138. r.EmitStacktraces = &v
  139. }
  140. }
  141. // WithPretty makes the response body pretty-printed.
  142. func (f WatcherStats) WithPretty() func(*WatcherStatsRequest) {
  143. return func(r *WatcherStatsRequest) {
  144. r.Pretty = true
  145. }
  146. }
  147. // WithHuman makes statistical values human-readable.
  148. func (f WatcherStats) WithHuman() func(*WatcherStatsRequest) {
  149. return func(r *WatcherStatsRequest) {
  150. r.Human = true
  151. }
  152. }
  153. // WithErrorTrace includes the stack trace for errors in the response body.
  154. func (f WatcherStats) WithErrorTrace() func(*WatcherStatsRequest) {
  155. return func(r *WatcherStatsRequest) {
  156. r.ErrorTrace = true
  157. }
  158. }
  159. // WithFilterPath filters the properties of the response body.
  160. func (f WatcherStats) WithFilterPath(v ...string) func(*WatcherStatsRequest) {
  161. return func(r *WatcherStatsRequest) {
  162. r.FilterPath = v
  163. }
  164. }
  165. // WithHeader adds the headers to the HTTP request.
  166. func (f WatcherStats) WithHeader(h map[string]string) func(*WatcherStatsRequest) {
  167. return func(r *WatcherStatsRequest) {
  168. if r.Header == nil {
  169. r.Header = make(http.Header)
  170. }
  171. for k, v := range h {
  172. r.Header.Add(k, v)
  173. }
  174. }
  175. }
  176. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  177. func (f WatcherStats) WithOpaqueID(s string) func(*WatcherStatsRequest) {
  178. return func(r *WatcherStatsRequest) {
  179. if r.Header == nil {
  180. r.Header = make(http.Header)
  181. }
  182. r.Header.Set("X-Opaque-Id", s)
  183. }
  184. }