api.nodes.stats.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. "time"
  26. )
  27. func newNodesStatsFunc(t Transport) NodesStats {
  28. return func(o ...func(*NodesStatsRequest)) (*Response, error) {
  29. var r = NodesStatsRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // NodesStats returns statistical information about nodes in the cluster.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-nodes-stats.html.
  40. type NodesStats func(o ...func(*NodesStatsRequest)) (*Response, error)
  41. // NodesStatsRequest configures the Nodes Stats API request.
  42. type NodesStatsRequest struct {
  43. IndexMetric []string
  44. Metric []string
  45. NodeID []string
  46. CompletionFields []string
  47. FielddataFields []string
  48. Fields []string
  49. Groups *bool
  50. IncludeSegmentFileSizes *bool
  51. IncludeUnloadedSegments *bool
  52. Level string
  53. Timeout time.Duration
  54. Types []string
  55. Pretty bool
  56. Human bool
  57. ErrorTrace bool
  58. FilterPath []string
  59. Header http.Header
  60. ctx context.Context
  61. }
  62. // Do executes the request and returns response or error.
  63. func (r NodesStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  64. var (
  65. method string
  66. path strings.Builder
  67. params map[string]string
  68. )
  69. method = "GET"
  70. path.Grow(1 + len("_nodes") + 1 + len(strings.Join(r.NodeID, ",")) + 1 + len("stats") + 1 + len(strings.Join(r.Metric, ",")) + 1 + len(strings.Join(r.IndexMetric, ",")))
  71. path.WriteString("/")
  72. path.WriteString("_nodes")
  73. if len(r.NodeID) > 0 {
  74. path.WriteString("/")
  75. path.WriteString(strings.Join(r.NodeID, ","))
  76. }
  77. path.WriteString("/")
  78. path.WriteString("stats")
  79. if len(r.Metric) > 0 {
  80. path.WriteString("/")
  81. path.WriteString(strings.Join(r.Metric, ","))
  82. }
  83. if len(r.IndexMetric) > 0 {
  84. path.WriteString("/")
  85. path.WriteString(strings.Join(r.IndexMetric, ","))
  86. }
  87. params = make(map[string]string)
  88. if len(r.CompletionFields) > 0 {
  89. params["completion_fields"] = strings.Join(r.CompletionFields, ",")
  90. }
  91. if len(r.FielddataFields) > 0 {
  92. params["fielddata_fields"] = strings.Join(r.FielddataFields, ",")
  93. }
  94. if len(r.Fields) > 0 {
  95. params["fields"] = strings.Join(r.Fields, ",")
  96. }
  97. if r.Groups != nil {
  98. params["groups"] = strconv.FormatBool(*r.Groups)
  99. }
  100. if r.IncludeSegmentFileSizes != nil {
  101. params["include_segment_file_sizes"] = strconv.FormatBool(*r.IncludeSegmentFileSizes)
  102. }
  103. if r.IncludeUnloadedSegments != nil {
  104. params["include_unloaded_segments"] = strconv.FormatBool(*r.IncludeUnloadedSegments)
  105. }
  106. if r.Level != "" {
  107. params["level"] = r.Level
  108. }
  109. if r.Timeout != 0 {
  110. params["timeout"] = formatDuration(r.Timeout)
  111. }
  112. if len(r.Types) > 0 {
  113. params["types"] = strings.Join(r.Types, ",")
  114. }
  115. if r.Pretty {
  116. params["pretty"] = "true"
  117. }
  118. if r.Human {
  119. params["human"] = "true"
  120. }
  121. if r.ErrorTrace {
  122. params["error_trace"] = "true"
  123. }
  124. if len(r.FilterPath) > 0 {
  125. params["filter_path"] = strings.Join(r.FilterPath, ",")
  126. }
  127. req, err := newRequest(method, path.String(), nil)
  128. if err != nil {
  129. return nil, err
  130. }
  131. if len(params) > 0 {
  132. q := req.URL.Query()
  133. for k, v := range params {
  134. q.Set(k, v)
  135. }
  136. req.URL.RawQuery = q.Encode()
  137. }
  138. if len(r.Header) > 0 {
  139. if len(req.Header) == 0 {
  140. req.Header = r.Header
  141. } else {
  142. for k, vv := range r.Header {
  143. for _, v := range vv {
  144. req.Header.Add(k, v)
  145. }
  146. }
  147. }
  148. }
  149. if ctx != nil {
  150. req = req.WithContext(ctx)
  151. }
  152. res, err := transport.Perform(req)
  153. if err != nil {
  154. return nil, err
  155. }
  156. response := Response{
  157. StatusCode: res.StatusCode,
  158. Body: res.Body,
  159. Header: res.Header,
  160. }
  161. return &response, nil
  162. }
  163. // WithContext sets the request context.
  164. func (f NodesStats) WithContext(v context.Context) func(*NodesStatsRequest) {
  165. return func(r *NodesStatsRequest) {
  166. r.ctx = v
  167. }
  168. }
  169. // WithIndexMetric - limit the information returned for `indices` metric to the specific index metrics. isn't used if `indices` (or `all`) metric isn't specified..
  170. func (f NodesStats) WithIndexMetric(v ...string) func(*NodesStatsRequest) {
  171. return func(r *NodesStatsRequest) {
  172. r.IndexMetric = v
  173. }
  174. }
  175. // WithMetric - limit the information returned to the specified metrics.
  176. func (f NodesStats) WithMetric(v ...string) func(*NodesStatsRequest) {
  177. return func(r *NodesStatsRequest) {
  178. r.Metric = v
  179. }
  180. }
  181. // WithNodeID - a list of node ids or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes.
  182. func (f NodesStats) WithNodeID(v ...string) func(*NodesStatsRequest) {
  183. return func(r *NodesStatsRequest) {
  184. r.NodeID = v
  185. }
  186. }
  187. // WithCompletionFields - a list of fields for `fielddata` and `suggest` index metric (supports wildcards).
  188. func (f NodesStats) WithCompletionFields(v ...string) func(*NodesStatsRequest) {
  189. return func(r *NodesStatsRequest) {
  190. r.CompletionFields = v
  191. }
  192. }
  193. // WithFielddataFields - a list of fields for `fielddata` index metric (supports wildcards).
  194. func (f NodesStats) WithFielddataFields(v ...string) func(*NodesStatsRequest) {
  195. return func(r *NodesStatsRequest) {
  196. r.FielddataFields = v
  197. }
  198. }
  199. // WithFields - a list of fields for `fielddata` and `completion` index metric (supports wildcards).
  200. func (f NodesStats) WithFields(v ...string) func(*NodesStatsRequest) {
  201. return func(r *NodesStatsRequest) {
  202. r.Fields = v
  203. }
  204. }
  205. // WithGroups - a list of search groups for `search` index metric.
  206. func (f NodesStats) WithGroups(v bool) func(*NodesStatsRequest) {
  207. return func(r *NodesStatsRequest) {
  208. r.Groups = &v
  209. }
  210. }
  211. // WithIncludeSegmentFileSizes - whether to report the aggregated disk usage of each one of the lucene index files (only applies if segment stats are requested).
  212. func (f NodesStats) WithIncludeSegmentFileSizes(v bool) func(*NodesStatsRequest) {
  213. return func(r *NodesStatsRequest) {
  214. r.IncludeSegmentFileSizes = &v
  215. }
  216. }
  217. // WithIncludeUnloadedSegments - if set to true segment stats will include stats for segments that are not currently loaded into memory.
  218. func (f NodesStats) WithIncludeUnloadedSegments(v bool) func(*NodesStatsRequest) {
  219. return func(r *NodesStatsRequest) {
  220. r.IncludeUnloadedSegments = &v
  221. }
  222. }
  223. // WithLevel - return indices stats aggregated at index, node or shard level.
  224. func (f NodesStats) WithLevel(v string) func(*NodesStatsRequest) {
  225. return func(r *NodesStatsRequest) {
  226. r.Level = v
  227. }
  228. }
  229. // WithTimeout - explicit operation timeout.
  230. func (f NodesStats) WithTimeout(v time.Duration) func(*NodesStatsRequest) {
  231. return func(r *NodesStatsRequest) {
  232. r.Timeout = v
  233. }
  234. }
  235. // WithTypes - a list of document types for the `indexing` index metric.
  236. func (f NodesStats) WithTypes(v ...string) func(*NodesStatsRequest) {
  237. return func(r *NodesStatsRequest) {
  238. r.Types = v
  239. }
  240. }
  241. // WithPretty makes the response body pretty-printed.
  242. func (f NodesStats) WithPretty() func(*NodesStatsRequest) {
  243. return func(r *NodesStatsRequest) {
  244. r.Pretty = true
  245. }
  246. }
  247. // WithHuman makes statistical values human-readable.
  248. func (f NodesStats) WithHuman() func(*NodesStatsRequest) {
  249. return func(r *NodesStatsRequest) {
  250. r.Human = true
  251. }
  252. }
  253. // WithErrorTrace includes the stack trace for errors in the response body.
  254. func (f NodesStats) WithErrorTrace() func(*NodesStatsRequest) {
  255. return func(r *NodesStatsRequest) {
  256. r.ErrorTrace = true
  257. }
  258. }
  259. // WithFilterPath filters the properties of the response body.
  260. func (f NodesStats) WithFilterPath(v ...string) func(*NodesStatsRequest) {
  261. return func(r *NodesStatsRequest) {
  262. r.FilterPath = v
  263. }
  264. }
  265. // WithHeader adds the headers to the HTTP request.
  266. func (f NodesStats) WithHeader(h map[string]string) func(*NodesStatsRequest) {
  267. return func(r *NodesStatsRequest) {
  268. if r.Header == nil {
  269. r.Header = make(http.Header)
  270. }
  271. for k, v := range h {
  272. r.Header.Add(k, v)
  273. }
  274. }
  275. }
  276. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  277. func (f NodesStats) WithOpaqueID(s string) func(*NodesStatsRequest) {
  278. return func(r *NodesStatsRequest) {
  279. if r.Header == nil {
  280. r.Header = make(http.Header)
  281. }
  282. r.Header.Set("X-Opaque-Id", s)
  283. }
  284. }