Skip to content

Refactor tests for nodes collector #936

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

Merged
merged 3 commits into from
Oct 16, 2024
Merged
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
88 changes: 31 additions & 57 deletions collector/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,13 @@ func getRoles(node NodeStatsNodeResponse) map[string]bool {
return roles
}

func createRoleMetric(role string) *nodeMetric {
return &nodeMetric{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "nodes", "roles"),
"Node roles",
defaultRoleLabels, prometheus.Labels{"role": role},
),
Value: func(node NodeStatsNodeResponse) float64 {
return 1.0
},
Labels: func(cluster string, node NodeStatsNodeResponse) []string {
return []string{
cluster,
node.Host,
node.Name,
}
},
}
}
var (
nodesRolesMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "nodes", "roles"),
"Node roles",
append(defaultRoleLabels, "role"), nil,
)
)

var (
defaultNodeLabels = []string{"cluster", "host", "name", "es_master_node", "es_data_node", "es_ingest_node", "es_client_node"}
Expand Down Expand Up @@ -187,9 +174,6 @@ type Nodes struct {
all bool
node string

up prometheus.Gauge
totalScrapes, jsonParseFailures prometheus.Counter

nodeMetrics []*nodeMetric
gcCollectionMetrics []*gcCollectionMetric
breakerMetrics []*breakerMetric
Expand All @@ -208,19 +192,6 @@ func NewNodes(logger log.Logger, client *http.Client, url *url.URL, all bool, no
all: all,
node: node,

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "node_stats", "up"),
Help: "Was the last scrape of the Elasticsearch nodes endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "node_stats", "total_scrapes"),
Help: "Current total Elasticsearch node scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "node_stats", "json_parse_failures"),
Help: "Number of errors while parsing JSON.",
}),

nodeMetrics: []*nodeMetric{
{
Type: prometheus.GaugeValue,
Expand Down Expand Up @@ -1859,12 +1830,20 @@ func NewNodes(logger log.Logger, client *http.Client, url *url.URL, all bool, no

// Describe add metrics descriptions
func (c *Nodes) Describe(ch chan<- *prometheus.Desc) {
ch <- nodesRolesMetric

for _, metric := range c.nodeMetrics {
ch <- metric.Desc
}
for _, metric := range c.gcCollectionMetrics {
ch <- metric.Desc
}
for _, metric := range c.breakerMetrics {
ch <- metric.Desc
}
for _, metric := range c.indexingPressureMetrics {
ch <- metric.Desc
}
for _, metric := range c.threadPoolMetrics {
ch <- metric.Desc
}
Expand All @@ -1874,9 +1853,6 @@ func (c *Nodes) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.filesystemIODeviceMetrics {
ch <- metric.Desc
}
ch <- c.up.Desc()
ch <- c.totalScrapes.Desc()
ch <- c.jsonParseFailures.Desc()
}

func (c *Nodes) fetchAndDecodeNodeStats() (nodeStatsResponse, error) {
Expand Down Expand Up @@ -1912,51 +1888,49 @@ func (c *Nodes) fetchAndDecodeNodeStats() (nodeStatsResponse, error) {

bts, err := io.ReadAll(res.Body)
if err != nil {
c.jsonParseFailures.Inc()
return nsr, err
}

if err := json.Unmarshal(bts, &nsr); err != nil {
c.jsonParseFailures.Inc()
return nsr, err
}
return nsr, nil
}

// Collect gets nodes metric values
func (c *Nodes) Collect(ch chan<- prometheus.Metric) {
c.totalScrapes.Inc()
defer func() {
ch <- c.up
ch <- c.totalScrapes
ch <- c.jsonParseFailures
}()

nodeStatsResp, err := c.fetchAndDecodeNodeStats()
if err != nil {
c.up.Set(0)
level.Warn(c.logger).Log(
"msg", "failed to fetch and decode node stats",
"err", err,
)
return
}
c.up.Set(1)

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

for role, roleEnabled := range roles {
val := 0.0
if roleEnabled {
metric := createRoleMetric(role)
ch <- prometheus.MustNewConstMetric(
metric.Desc,
metric.Type,
metric.Value(node),
metric.Labels(nodeStatsResp.ClusterName, node)...,
)
val = 1.0
}

labels := []string{
nodeStatsResp.ClusterName,
node.Host,
node.Name,
role,
}

ch <- prometheus.MustNewConstMetric(
nodesRolesMetric,
prometheus.GaugeValue,
val,
labels...,
)
}

for _, metric := range c.nodeMetrics {
Expand Down
1,708 changes: 1,571 additions & 137 deletions collector/nodes_test.go

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions collector/versions_test.go

This file was deleted.

1 change: 0 additions & 1 deletion fixtures/nodestats/5.4.2.json

This file was deleted.

1 change: 0 additions & 1 deletion fixtures/nodestats/6.5.4.json

This file was deleted.

Loading
Loading