diff --git a/collector/nodes.go b/collector/nodes.go index 1a895b7c..debe6463 100644 --- a/collector/nodes.go +++ b/collector/nodes.go @@ -20,6 +20,7 @@ import ( "net/http" "net/url" "path" + "time" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -165,11 +166,12 @@ type filesystemIODeviceMetric struct { // Nodes information struct type Nodes struct { - logger log.Logger - client *http.Client - url *url.URL - all bool - node string + logger log.Logger + client *http.Client + url *url.URL + all bool + node string + timeout time.Duration up prometheus.Gauge totalScrapes, jsonParseFailures prometheus.Counter @@ -183,13 +185,14 @@ type Nodes struct { } // NewNodes defines Nodes Prometheus metrics -func NewNodes(logger log.Logger, client *http.Client, url *url.URL, all bool, node string) *Nodes { +func NewNodes(logger log.Logger, client *http.Client, url *url.URL, all bool, node string, timeout time.Duration) *Nodes { return &Nodes{ - logger: logger, - client: client, - url: url, - all: all, - node: node, + logger: logger, + client: client, + url: url, + all: all, + node: node, + timeout: timeout, up: prometheus.NewGauge(prometheus.GaugeOpts{ Name: prometheus.BuildFQName(namespace, "node_stats", "up"), @@ -1809,6 +1812,8 @@ func (c *Nodes) fetchAndDecodeNodeStats() (nodeStatsResponse, error) { u.Path = path.Join(u.Path, "_nodes", c.node, "stats") } + u.RawQuery = fmt.Sprintf("timeout=%s", c.timeout.String()) + res, err := c.client.Get(u.String()) if err != nil { return nsr, fmt.Errorf("failed to get cluster health from %s://%s:%s%s: %s", diff --git a/collector/nodes_test.go b/collector/nodes_test.go index 26bca30e..9f00b12f 100644 --- a/collector/nodes_test.go +++ b/collector/nodes_test.go @@ -22,6 +22,7 @@ import ( "net/url" "strings" "testing" + "time" "github.com/go-kit/log" ) @@ -59,7 +60,7 @@ func TestNodesStats(t *testing.T) { t.Fatalf("Failed to parse URL: %s", err) } u.User = url.UserPassword("elastic", "changeme") - c := NewNodes(log.NewNopLogger(), http.DefaultClient, u, true, "_local") + c := NewNodes(log.NewNopLogger(), http.DefaultClient, u, true, "_local", time.Duration(5)) nsr, err := c.fetchAndDecodeNodeStats() if err != nil { t.Fatalf("Failed to fetch or decode node stats: %s", err) diff --git a/main.go b/main.go index cde2e747..78a29d47 100644 --- a/main.go +++ b/main.go @@ -161,7 +161,7 @@ func main() { clusterInfoRetriever := clusterinfo.New(logger, httpClient, esURL, *esClusterInfoInterval) prometheus.MustRegister(collector.NewClusterHealth(logger, httpClient, esURL)) - prometheus.MustRegister(collector.NewNodes(logger, httpClient, esURL, *esAllNodes, *esNode)) + prometheus.MustRegister(collector.NewNodes(logger, httpClient, esURL, *esAllNodes, *esNode, *esTimeout)) if *esExportIndices || *esExportShards { iC := collector.NewIndices(logger, httpClient, esURL, *esExportShards)