Skip to content

add timeout to node stats #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions collector/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"net/url"
"path"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -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
Expand All @@ -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"),
Expand Down Expand 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",
Expand Down
3 changes: 2 additions & 1 deletion collector/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/url"
"strings"
"testing"
"time"

"github.com/go-kit/log"
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down