github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.nodes.hot_threads.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 newNodesHotThreadsFunc(t Transport) NodesHotThreads {
    38  	return func(o ...func(*NodesHotThreadsRequest)) (*Response, error) {
    39  		var r = NodesHotThreadsRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // NodesHotThreads returns information about hot threads on each node in the cluster.
    50  //
    51  //
    52  type NodesHotThreads func(o ...func(*NodesHotThreadsRequest)) (*Response, error)
    53  
    54  // NodesHotThreadsRequest configures the Nodes Hot Threads API request.
    55  //
    56  type NodesHotThreadsRequest struct {
    57  	NodeID []string
    58  
    59  	IgnoreIdleThreads *bool
    60  	Interval          time.Duration
    61  	Snapshots         *int
    62  	Threads           *int
    63  	Timeout           time.Duration
    64  
    65  	Pretty     bool
    66  	Human      bool
    67  	ErrorTrace bool
    68  	FilterPath []string
    69  
    70  	Header http.Header
    71  
    72  	ctx context.Context
    73  }
    74  
    75  // Do executes the request and returns response or error.
    76  //
    77  func (r NodesHotThreadsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    78  	var (
    79  		method string
    80  		path   strings.Builder
    81  		params map[string]string
    82  	)
    83  
    84  	method = "GET"
    85  
    86  	path.Grow(1 + len("_cluster") + 1 + len("nodes") + 1 + len(strings.Join(r.NodeID, ",")) + 1 + len("hot_threads"))
    87  	path.WriteString("/")
    88  	path.WriteString("_cluster")
    89  	path.WriteString("/")
    90  	path.WriteString("nodes")
    91  	if len(r.NodeID) > 0 {
    92  		path.WriteString("/")
    93  		path.WriteString(strings.Join(r.NodeID, ","))
    94  	}
    95  	path.WriteString("/")
    96  	path.WriteString("hot_threads")
    97  
    98  	params = make(map[string]string)
    99  
   100  	if r.IgnoreIdleThreads != nil {
   101  		params["ignore_idle_threads"] = strconv.FormatBool(*r.IgnoreIdleThreads)
   102  	}
   103  
   104  	if r.Interval != 0 {
   105  		params["interval"] = formatDuration(r.Interval)
   106  	}
   107  
   108  	if r.Snapshots != nil {
   109  		params["snapshots"] = strconv.FormatInt(int64(*r.Snapshots), 10)
   110  	}
   111  
   112  	if r.Threads != nil {
   113  		params["threads"] = strconv.FormatInt(int64(*r.Threads), 10)
   114  	}
   115  
   116  	if r.Timeout != 0 {
   117  		params["timeout"] = formatDuration(r.Timeout)
   118  	}
   119  
   120  	if r.Pretty {
   121  		params["pretty"] = "true"
   122  	}
   123  
   124  	if r.Human {
   125  		params["human"] = "true"
   126  	}
   127  
   128  	if r.ErrorTrace {
   129  		params["error_trace"] = "true"
   130  	}
   131  
   132  	if len(r.FilterPath) > 0 {
   133  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   134  	}
   135  
   136  	req, err := newRequest(method, path.String(), nil)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	if len(params) > 0 {
   142  		q := req.URL.Query()
   143  		for k, v := range params {
   144  			q.Set(k, v)
   145  		}
   146  		req.URL.RawQuery = q.Encode()
   147  	}
   148  
   149  	if len(r.Header) > 0 {
   150  		if len(req.Header) == 0 {
   151  			req.Header = r.Header
   152  		} else {
   153  			for k, vv := range r.Header {
   154  				for _, v := range vv {
   155  					req.Header.Add(k, v)
   156  				}
   157  			}
   158  		}
   159  	}
   160  
   161  	if ctx != nil {
   162  		req = req.WithContext(ctx)
   163  	}
   164  
   165  	res, err := transport.Perform(req)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	response := Response{
   171  		StatusCode: res.StatusCode,
   172  		Body:       res.Body,
   173  		Header:     res.Header,
   174  	}
   175  
   176  	return &response, nil
   177  }
   178  
   179  // WithContext sets the request context.
   180  //
   181  func (f NodesHotThreads) WithContext(v context.Context) func(*NodesHotThreadsRequest) {
   182  	return func(r *NodesHotThreadsRequest) {
   183  		r.ctx = v
   184  	}
   185  }
   186  
   187  // 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.
   188  //
   189  func (f NodesHotThreads) WithNodeID(v ...string) func(*NodesHotThreadsRequest) {
   190  	return func(r *NodesHotThreadsRequest) {
   191  		r.NodeID = v
   192  	}
   193  }
   194  
   195  // WithIgnoreIdleThreads - don't show threads that are in known-idle places, such as waiting on a socket select or pulling from an empty task queue (default: true).
   196  //
   197  func (f NodesHotThreads) WithIgnoreIdleThreads(v bool) func(*NodesHotThreadsRequest) {
   198  	return func(r *NodesHotThreadsRequest) {
   199  		r.IgnoreIdleThreads = &v
   200  	}
   201  }
   202  
   203  // WithInterval - the interval for the second sampling of threads.
   204  //
   205  func (f NodesHotThreads) WithInterval(v time.Duration) func(*NodesHotThreadsRequest) {
   206  	return func(r *NodesHotThreadsRequest) {
   207  		r.Interval = v
   208  	}
   209  }
   210  
   211  // WithSnapshots - number of samples of thread stacktrace (default: 10).
   212  //
   213  func (f NodesHotThreads) WithSnapshots(v int) func(*NodesHotThreadsRequest) {
   214  	return func(r *NodesHotThreadsRequest) {
   215  		r.Snapshots = &v
   216  	}
   217  }
   218  
   219  // WithThreads - specify the number of threads to provide information for (default: 3).
   220  //
   221  func (f NodesHotThreads) WithThreads(v int) func(*NodesHotThreadsRequest) {
   222  	return func(r *NodesHotThreadsRequest) {
   223  		r.Threads = &v
   224  	}
   225  }
   226  
   227  // WithTimeout - explicit operation timeout.
   228  //
   229  func (f NodesHotThreads) WithTimeout(v time.Duration) func(*NodesHotThreadsRequest) {
   230  	return func(r *NodesHotThreadsRequest) {
   231  		r.Timeout = v
   232  	}
   233  }
   234  
   235  // WithPretty makes the response body pretty-printed.
   236  //
   237  func (f NodesHotThreads) WithPretty() func(*NodesHotThreadsRequest) {
   238  	return func(r *NodesHotThreadsRequest) {
   239  		r.Pretty = true
   240  	}
   241  }
   242  
   243  // WithHuman makes statistical values human-readable.
   244  //
   245  func (f NodesHotThreads) WithHuman() func(*NodesHotThreadsRequest) {
   246  	return func(r *NodesHotThreadsRequest) {
   247  		r.Human = true
   248  	}
   249  }
   250  
   251  // WithErrorTrace includes the stack trace for errors in the response body.
   252  //
   253  func (f NodesHotThreads) WithErrorTrace() func(*NodesHotThreadsRequest) {
   254  	return func(r *NodesHotThreadsRequest) {
   255  		r.ErrorTrace = true
   256  	}
   257  }
   258  
   259  // WithFilterPath filters the properties of the response body.
   260  //
   261  func (f NodesHotThreads) WithFilterPath(v ...string) func(*NodesHotThreadsRequest) {
   262  	return func(r *NodesHotThreadsRequest) {
   263  		r.FilterPath = v
   264  	}
   265  }
   266  
   267  // WithHeader adds the headers to the HTTP request.
   268  //
   269  func (f NodesHotThreads) WithHeader(h map[string]string) func(*NodesHotThreadsRequest) {
   270  	return func(r *NodesHotThreadsRequest) {
   271  		if r.Header == nil {
   272  			r.Header = make(http.Header)
   273  		}
   274  		for k, v := range h {
   275  			r.Header.Add(k, v)
   276  		}
   277  	}
   278  }
   279  
   280  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   281  //
   282  func (f NodesHotThreads) WithOpaqueID(s string) func(*NodesHotThreadsRequest) {
   283  	return func(r *NodesHotThreadsRequest) {
   284  		if r.Header == nil {
   285  			r.Header = make(http.Header)
   286  		}
   287  		r.Header.Set("X-Opaque-Id", s)
   288  	}
   289  }