Skip to content

Commit 102a9d3

Browse files
authored
Fix cluster settings collection when max_shards_per_node is manually set (prometheus-community#603)
Signed-off-by: akazs <[email protected]>
1 parent d9b9ecd commit 102a9d3

4 files changed

+1542
-8
lines changed

collector/cluster_settings.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ func (cs *ClusterSettings) Collect(ch chan<- prometheus.Metric) {
174174

175175
cs.shardAllocationEnabled.Set(float64(shardAllocationMap[csr.Cluster.Routing.Allocation.Enabled]))
176176

177-
maxShardsPerNode, err := strconv.ParseInt(csr.Cluster.MaxShardsPerNode, 10, 64)
178-
if err == nil {
179-
cs.maxShardsPerNode.Set(float64(maxShardsPerNode))
177+
if maxShardsPerNodeString, ok := csr.Cluster.MaxShardsPerNode.(string); ok {
178+
maxShardsPerNode, err := strconv.ParseInt(maxShardsPerNodeString, 10, 64)
179+
if err == nil {
180+
cs.maxShardsPerNode.Set(float64(maxShardsPerNode))
181+
}
180182
}
181183
}

collector/cluster_settings_response.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ type ClusterSettingsResponse struct {
2727

2828
// Cluster is a representation of a Elasticsearch Cluster Settings
2929
type Cluster struct {
30-
Routing Routing `json:"routing"`
31-
MaxShardsPerNode string `json:"max_shards_per_node"`
30+
Routing Routing `json:"routing"`
31+
// This can be either a JSON object (which does not contain the value we are interested in) or a string
32+
MaxShardsPerNode interface{} `json:"max_shards_per_node"`
3233
}
3334

3435
// Routing is a representation of a Elasticsearch Cluster shard routing configuration

collector/cluster_settings_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ func TestClusterSettingsStats(t *testing.T) {
5353
if nsr.Cluster.Routing.Allocation.Enabled != "ALL" {
5454
t.Errorf("Wrong setting for cluster routing allocation enabled")
5555
}
56-
if nsr.Cluster.MaxShardsPerNode != "" {
56+
if nsr.Cluster.MaxShardsPerNode != nil {
5757
t.Errorf("MaxShardsPerNode should be empty on older releases")
5858
}
5959
}
6060
}
6161
}
6262

6363
func TestClusterMaxShardsPerNode(t *testing.T) {
64-
// Testcases created using:
64+
// settings-7.3.0.json testcase created using:
6565
// docker run -d -p 9200:9200 elasticsearch:VERSION-alpine
6666
// curl http://localhost:9200/_cluster/settings/?include_defaults=true
67-
files := []string{"../fixtures/settings-7.3.0.json"}
67+
// settings-persistent-clustermaxshartspernode-7.17.json testcase created using:
68+
// docker run -d -p 9200:9200 elasticsearch:VERSION
69+
// curl -X PUT http://localhost:9200/_cluster/settings -H 'Content-Type: application/json' -d '{"persistent":{"cluster.max_shards_per_node":1000}}'
70+
// curl http://localhost:9200/_cluster/settings/?include_defaults=true
71+
files := []string{"../fixtures/settings-7.3.0.json", "../fixtures/settings-persistent-clustermaxshartspernode-7.17.5.json"}
6872
for _, filename := range files {
6973
f, _ := os.Open(filename)
7074
defer f.Close()

0 commit comments

Comments
 (0)