Skip to content

Commit 33f42c6

Browse files
authored
Merge pull request #936 from sysadmind/nodes-test-refactor
Refactor tests for nodes collector
2 parents 79b0e15 + 22d4a8a commit 33f42c6

File tree

8 files changed

+1602
-223
lines changed

8 files changed

+1602
-223
lines changed

collector/nodes.go

+31-57
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,13 @@ func getRoles(node NodeStatsNodeResponse) map[string]bool {
7070
return roles
7171
}
7272

73-
func createRoleMetric(role string) *nodeMetric {
74-
return &nodeMetric{
75-
Type: prometheus.GaugeValue,
76-
Desc: prometheus.NewDesc(
77-
prometheus.BuildFQName(namespace, "nodes", "roles"),
78-
"Node roles",
79-
defaultRoleLabels, prometheus.Labels{"role": role},
80-
),
81-
Value: func(node NodeStatsNodeResponse) float64 {
82-
return 1.0
83-
},
84-
Labels: func(cluster string, node NodeStatsNodeResponse) []string {
85-
return []string{
86-
cluster,
87-
node.Host,
88-
node.Name,
89-
}
90-
},
91-
}
92-
}
73+
var (
74+
nodesRolesMetric = prometheus.NewDesc(
75+
prometheus.BuildFQName(namespace, "nodes", "roles"),
76+
"Node roles",
77+
append(defaultRoleLabels, "role"), nil,
78+
)
79+
)
9380

9481
var (
9582
defaultNodeLabels = []string{"cluster", "host", "name", "es_master_node", "es_data_node", "es_ingest_node", "es_client_node"}
@@ -187,9 +174,6 @@ type Nodes struct {
187174
all bool
188175
node string
189176

190-
up prometheus.Gauge
191-
totalScrapes, jsonParseFailures prometheus.Counter
192-
193177
nodeMetrics []*nodeMetric
194178
gcCollectionMetrics []*gcCollectionMetric
195179
breakerMetrics []*breakerMetric
@@ -208,19 +192,6 @@ func NewNodes(logger log.Logger, client *http.Client, url *url.URL, all bool, no
208192
all: all,
209193
node: node,
210194

211-
up: prometheus.NewGauge(prometheus.GaugeOpts{
212-
Name: prometheus.BuildFQName(namespace, "node_stats", "up"),
213-
Help: "Was the last scrape of the Elasticsearch nodes endpoint successful.",
214-
}),
215-
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
216-
Name: prometheus.BuildFQName(namespace, "node_stats", "total_scrapes"),
217-
Help: "Current total Elasticsearch node scrapes.",
218-
}),
219-
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
220-
Name: prometheus.BuildFQName(namespace, "node_stats", "json_parse_failures"),
221-
Help: "Number of errors while parsing JSON.",
222-
}),
223-
224195
nodeMetrics: []*nodeMetric{
225196
{
226197
Type: prometheus.GaugeValue,
@@ -1859,12 +1830,20 @@ func NewNodes(logger log.Logger, client *http.Client, url *url.URL, all bool, no
18591830

18601831
// Describe add metrics descriptions
18611832
func (c *Nodes) Describe(ch chan<- *prometheus.Desc) {
1833+
ch <- nodesRolesMetric
1834+
18621835
for _, metric := range c.nodeMetrics {
18631836
ch <- metric.Desc
18641837
}
18651838
for _, metric := range c.gcCollectionMetrics {
18661839
ch <- metric.Desc
18671840
}
1841+
for _, metric := range c.breakerMetrics {
1842+
ch <- metric.Desc
1843+
}
1844+
for _, metric := range c.indexingPressureMetrics {
1845+
ch <- metric.Desc
1846+
}
18681847
for _, metric := range c.threadPoolMetrics {
18691848
ch <- metric.Desc
18701849
}
@@ -1874,9 +1853,6 @@ func (c *Nodes) Describe(ch chan<- *prometheus.Desc) {
18741853
for _, metric := range c.filesystemIODeviceMetrics {
18751854
ch <- metric.Desc
18761855
}
1877-
ch <- c.up.Desc()
1878-
ch <- c.totalScrapes.Desc()
1879-
ch <- c.jsonParseFailures.Desc()
18801856
}
18811857

18821858
func (c *Nodes) fetchAndDecodeNodeStats() (nodeStatsResponse, error) {
@@ -1912,51 +1888,49 @@ func (c *Nodes) fetchAndDecodeNodeStats() (nodeStatsResponse, error) {
19121888

19131889
bts, err := io.ReadAll(res.Body)
19141890
if err != nil {
1915-
c.jsonParseFailures.Inc()
19161891
return nsr, err
19171892
}
19181893

19191894
if err := json.Unmarshal(bts, &nsr); err != nil {
1920-
c.jsonParseFailures.Inc()
19211895
return nsr, err
19221896
}
19231897
return nsr, nil
19241898
}
19251899

19261900
// Collect gets nodes metric values
19271901
func (c *Nodes) Collect(ch chan<- prometheus.Metric) {
1928-
c.totalScrapes.Inc()
1929-
defer func() {
1930-
ch <- c.up
1931-
ch <- c.totalScrapes
1932-
ch <- c.jsonParseFailures
1933-
}()
1934-
19351902
nodeStatsResp, err := c.fetchAndDecodeNodeStats()
19361903
if err != nil {
1937-
c.up.Set(0)
19381904
level.Warn(c.logger).Log(
19391905
"msg", "failed to fetch and decode node stats",
19401906
"err", err,
19411907
)
19421908
return
19431909
}
1944-
c.up.Set(1)
19451910

19461911
for _, node := range nodeStatsResp.Nodes {
19471912
// Handle the node labels metric
19481913
roles := getRoles(node)
19491914

19501915
for role, roleEnabled := range roles {
1916+
val := 0.0
19511917
if roleEnabled {
1952-
metric := createRoleMetric(role)
1953-
ch <- prometheus.MustNewConstMetric(
1954-
metric.Desc,
1955-
metric.Type,
1956-
metric.Value(node),
1957-
metric.Labels(nodeStatsResp.ClusterName, node)...,
1958-
)
1918+
val = 1.0
19591919
}
1920+
1921+
labels := []string{
1922+
nodeStatsResp.ClusterName,
1923+
node.Host,
1924+
node.Name,
1925+
role,
1926+
}
1927+
1928+
ch <- prometheus.MustNewConstMetric(
1929+
nodesRolesMetric,
1930+
prometheus.GaugeValue,
1931+
val,
1932+
labels...,
1933+
)
19601934
}
19611935

19621936
for _, metric := range c.nodeMetrics {

collector/nodes_test.go

+1,571-137
Large diffs are not rendered by default.

collector/versions_test.go

-24
This file was deleted.

fixtures/nodestats/5.4.2.json

-1
This file was deleted.

fixtures/nodestats/6.5.4.json

-1
This file was deleted.

0 commit comments

Comments
 (0)