github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.nodes.stats.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // The OpenSearch Contributors require contributions made to 4 // this file be licensed under the Apache-2.0 license or a 5 // compatible open source license. 6 // 7 // Modifications Copyright OpenSearch Contributors. See 8 // GitHub history for details. 9 10 // Licensed to Elasticsearch B.V. under one or more contributor 11 // license agreements. See the NOTICE file distributed with 12 // this work for additional information regarding copyright 13 // ownership. Elasticsearch B.V. licenses this file to you under 14 // the Apache License, Version 2.0 (the "License"); you may 15 // not use this file except in compliance with the License. 16 // You may obtain a copy of the License at 17 // 18 // http://www.apache.org/licenses/LICENSE-2.0 19 // 20 // Unless required by applicable law or agreed to in writing, 21 // software distributed under the License is distributed on an 22 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 23 // KIND, either express or implied. See the License for the 24 // specific language governing permissions and limitations 25 // under the License. 26 27 package opensearchapi 28 29 import ( 30 "context" 31 "net/http" 32 "strconv" 33 "strings" 34 "time" 35 ) 36 37 func newNodesStatsFunc(t Transport) NodesStats { 38 return func(o ...func(*NodesStatsRequest)) (*Response, error) { 39 var r = NodesStatsRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // NodesStats returns statistical information about nodes in the cluster. 50 // 51 // 52 type NodesStats func(o ...func(*NodesStatsRequest)) (*Response, error) 53 54 // NodesStatsRequest configures the Nodes Stats API request. 55 // 56 type NodesStatsRequest struct { 57 IndexMetric []string 58 Metric []string 59 NodeID []string 60 61 CompletionFields []string 62 FielddataFields []string 63 Fields []string 64 Groups *bool 65 IncludeSegmentFileSizes *bool 66 IncludeUnloadedSegments *bool 67 Level string 68 Timeout time.Duration 69 Types []string 70 71 Pretty bool 72 Human bool 73 ErrorTrace bool 74 FilterPath []string 75 76 Header http.Header 77 78 ctx context.Context 79 } 80 81 // Do executes the request and returns response or error. 82 // 83 func (r NodesStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 84 var ( 85 method string 86 path strings.Builder 87 params map[string]string 88 ) 89 90 method = "GET" 91 92 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, ","))) 93 path.WriteString("/") 94 path.WriteString("_nodes") 95 if len(r.NodeID) > 0 { 96 path.WriteString("/") 97 path.WriteString(strings.Join(r.NodeID, ",")) 98 } 99 path.WriteString("/") 100 path.WriteString("stats") 101 if len(r.Metric) > 0 { 102 path.WriteString("/") 103 path.WriteString(strings.Join(r.Metric, ",")) 104 } 105 if len(r.IndexMetric) > 0 { 106 path.WriteString("/") 107 path.WriteString(strings.Join(r.IndexMetric, ",")) 108 } 109 110 params = make(map[string]string) 111 112 if len(r.CompletionFields) > 0 { 113 params["completion_fields"] = strings.Join(r.CompletionFields, ",") 114 } 115 116 if len(r.FielddataFields) > 0 { 117 params["fielddata_fields"] = strings.Join(r.FielddataFields, ",") 118 } 119 120 if len(r.Fields) > 0 { 121 params["fields"] = strings.Join(r.Fields, ",") 122 } 123 124 if r.Groups != nil { 125 params["groups"] = strconv.FormatBool(*r.Groups) 126 } 127 128 if r.IncludeSegmentFileSizes != nil { 129 params["include_segment_file_sizes"] = strconv.FormatBool(*r.IncludeSegmentFileSizes) 130 } 131 132 if r.IncludeUnloadedSegments != nil { 133 params["include_unloaded_segments"] = strconv.FormatBool(*r.IncludeUnloadedSegments) 134 } 135 136 if r.Level != "" { 137 params["level"] = r.Level 138 } 139 140 if r.Timeout != 0 { 141 params["timeout"] = formatDuration(r.Timeout) 142 } 143 144 if len(r.Types) > 0 { 145 params["types"] = strings.Join(r.Types, ",") 146 } 147 148 if r.Pretty { 149 params["pretty"] = "true" 150 } 151 152 if r.Human { 153 params["human"] = "true" 154 } 155 156 if r.ErrorTrace { 157 params["error_trace"] = "true" 158 } 159 160 if len(r.FilterPath) > 0 { 161 params["filter_path"] = strings.Join(r.FilterPath, ",") 162 } 163 164 req, err := newRequest(method, path.String(), nil) 165 if err != nil { 166 return nil, err 167 } 168 169 if len(params) > 0 { 170 q := req.URL.Query() 171 for k, v := range params { 172 q.Set(k, v) 173 } 174 req.URL.RawQuery = q.Encode() 175 } 176 177 if len(r.Header) > 0 { 178 if len(req.Header) == 0 { 179 req.Header = r.Header 180 } else { 181 for k, vv := range r.Header { 182 for _, v := range vv { 183 req.Header.Add(k, v) 184 } 185 } 186 } 187 } 188 189 if ctx != nil { 190 req = req.WithContext(ctx) 191 } 192 193 res, err := transport.Perform(req) 194 if err != nil { 195 return nil, err 196 } 197 198 response := Response{ 199 StatusCode: res.StatusCode, 200 Body: res.Body, 201 Header: res.Header, 202 } 203 204 return &response, nil 205 } 206 207 // WithContext sets the request context. 208 // 209 func (f NodesStats) WithContext(v context.Context) func(*NodesStatsRequest) { 210 return func(r *NodesStatsRequest) { 211 r.ctx = v 212 } 213 } 214 215 // WithIndexMetric - limit the information returned for `indices` metric to the specific index metrics. isn't used if `indices` (or `all`) metric isn't specified.. 216 // 217 func (f NodesStats) WithIndexMetric(v ...string) func(*NodesStatsRequest) { 218 return func(r *NodesStatsRequest) { 219 r.IndexMetric = v 220 } 221 } 222 223 // WithMetric - limit the information returned to the specified metrics. 224 // 225 func (f NodesStats) WithMetric(v ...string) func(*NodesStatsRequest) { 226 return func(r *NodesStatsRequest) { 227 r.Metric = v 228 } 229 } 230 231 // 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. 232 // 233 func (f NodesStats) WithNodeID(v ...string) func(*NodesStatsRequest) { 234 return func(r *NodesStatsRequest) { 235 r.NodeID = v 236 } 237 } 238 239 // WithCompletionFields - a list of fields for `fielddata` and `suggest` index metric (supports wildcards). 240 // 241 func (f NodesStats) WithCompletionFields(v ...string) func(*NodesStatsRequest) { 242 return func(r *NodesStatsRequest) { 243 r.CompletionFields = v 244 } 245 } 246 247 // WithFielddataFields - a list of fields for `fielddata` index metric (supports wildcards). 248 // 249 func (f NodesStats) WithFielddataFields(v ...string) func(*NodesStatsRequest) { 250 return func(r *NodesStatsRequest) { 251 r.FielddataFields = v 252 } 253 } 254 255 // WithFields - a list of fields for `fielddata` and `completion` index metric (supports wildcards). 256 // 257 func (f NodesStats) WithFields(v ...string) func(*NodesStatsRequest) { 258 return func(r *NodesStatsRequest) { 259 r.Fields = v 260 } 261 } 262 263 // WithGroups - a list of search groups for `search` index metric. 264 // 265 func (f NodesStats) WithGroups(v bool) func(*NodesStatsRequest) { 266 return func(r *NodesStatsRequest) { 267 r.Groups = &v 268 } 269 } 270 271 // WithIncludeSegmentFileSizes - whether to report the aggregated disk usage of each one of the lucene index files (only applies if segment stats are requested). 272 // 273 func (f NodesStats) WithIncludeSegmentFileSizes(v bool) func(*NodesStatsRequest) { 274 return func(r *NodesStatsRequest) { 275 r.IncludeSegmentFileSizes = &v 276 } 277 } 278 279 // WithIncludeUnloadedSegments - if set to true segment stats will include stats for segments that are not currently loaded into memory. 280 // 281 func (f NodesStats) WithIncludeUnloadedSegments(v bool) func(*NodesStatsRequest) { 282 return func(r *NodesStatsRequest) { 283 r.IncludeUnloadedSegments = &v 284 } 285 } 286 287 // WithLevel - return indices stats aggregated at index, node or shard level. 288 // 289 func (f NodesStats) WithLevel(v string) func(*NodesStatsRequest) { 290 return func(r *NodesStatsRequest) { 291 r.Level = v 292 } 293 } 294 295 // WithTimeout - explicit operation timeout. 296 // 297 func (f NodesStats) WithTimeout(v time.Duration) func(*NodesStatsRequest) { 298 return func(r *NodesStatsRequest) { 299 r.Timeout = v 300 } 301 } 302 303 // WithTypes - a list of document types for the `indexing` index metric. 304 // 305 func (f NodesStats) WithTypes(v ...string) func(*NodesStatsRequest) { 306 return func(r *NodesStatsRequest) { 307 r.Types = v 308 } 309 } 310 311 // WithPretty makes the response body pretty-printed. 312 // 313 func (f NodesStats) WithPretty() func(*NodesStatsRequest) { 314 return func(r *NodesStatsRequest) { 315 r.Pretty = true 316 } 317 } 318 319 // WithHuman makes statistical values human-readable. 320 // 321 func (f NodesStats) WithHuman() func(*NodesStatsRequest) { 322 return func(r *NodesStatsRequest) { 323 r.Human = true 324 } 325 } 326 327 // WithErrorTrace includes the stack trace for errors in the response body. 328 // 329 func (f NodesStats) WithErrorTrace() func(*NodesStatsRequest) { 330 return func(r *NodesStatsRequest) { 331 r.ErrorTrace = true 332 } 333 } 334 335 // WithFilterPath filters the properties of the response body. 336 // 337 func (f NodesStats) WithFilterPath(v ...string) func(*NodesStatsRequest) { 338 return func(r *NodesStatsRequest) { 339 r.FilterPath = v 340 } 341 } 342 343 // WithHeader adds the headers to the HTTP request. 344 // 345 func (f NodesStats) WithHeader(h map[string]string) func(*NodesStatsRequest) { 346 return func(r *NodesStatsRequest) { 347 if r.Header == nil { 348 r.Header = make(http.Header) 349 } 350 for k, v := range h { 351 r.Header.Add(k, v) 352 } 353 } 354 } 355 356 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 357 // 358 func (f NodesStats) WithOpaqueID(s string) func(*NodesStatsRequest) { 359 return func(r *NodesStatsRequest) { 360 if r.Header == nil { 361 r.Header = make(http.Header) 362 } 363 r.Header.Set("X-Opaque-Id", s) 364 } 365 }