Skip to content

Commit e3d4fbf

Browse files
committed
Fix elasticsearch v8 cluster_settings unmarshal error
Closes #840 Signed-off-by: takahisa <[email protected]>
1 parent d13c555 commit e3d4fbf

File tree

6 files changed

+1750
-52
lines changed

6 files changed

+1750
-52
lines changed

collector/cluster_settings.go

+6-52
Original file line numberDiff line numberDiff line change
@@ -103,49 +103,6 @@ var clusterSettingsDesc = map[string]*prometheus.Desc{
103103
),
104104
}
105105

106-
// clusterSettingsResponse is a representation of a Elasticsearch Cluster Settings
107-
type clusterSettingsResponse struct {
108-
Defaults clusterSettingsSection `json:"defaults"`
109-
Persistent clusterSettingsSection `json:"persistent"`
110-
Transient clusterSettingsSection `json:"transient"`
111-
}
112-
113-
// clusterSettingsSection is a representation of a Elasticsearch Cluster Settings
114-
type clusterSettingsSection struct {
115-
Cluster clusterSettingsCluster `json:"cluster"`
116-
}
117-
118-
// clusterSettingsCluster is a representation of a Elasticsearch clusterSettingsCluster Settings
119-
type clusterSettingsCluster struct {
120-
Routing clusterSettingsRouting `json:"routing"`
121-
// This can be either a JSON object (which does not contain the value we are interested in) or a string
122-
MaxShardsPerNode interface{} `json:"max_shards_per_node"`
123-
}
124-
125-
// clusterSettingsRouting is a representation of a Elasticsearch Cluster shard routing configuration
126-
type clusterSettingsRouting struct {
127-
Allocation clusterSettingsAllocation `json:"allocation"`
128-
}
129-
130-
// clusterSettingsAllocation is a representation of a Elasticsearch Cluster shard routing allocation settings
131-
type clusterSettingsAllocation struct {
132-
Enabled string `json:"enable"`
133-
Disk clusterSettingsDisk `json:"disk"`
134-
}
135-
136-
// clusterSettingsDisk is a representation of a Elasticsearch Cluster shard routing disk allocation settings
137-
type clusterSettingsDisk struct {
138-
ThresholdEnabled string `json:"threshold_enabled"`
139-
Watermark clusterSettingsWatermark `json:"watermark"`
140-
}
141-
142-
// clusterSettingsWatermark is representation of Elasticsearch Cluster shard routing disk allocation watermark settings
143-
type clusterSettingsWatermark struct {
144-
FloodStage string `json:"flood_stage"`
145-
High string `json:"high"`
146-
Low string `json:"low"`
147-
}
148-
149106
func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometheus.Metric) error {
150107
u := c.u.ResolveReference(&url.URL{Path: "_cluster/settings"})
151108
q := u.Query()
@@ -185,15 +142,12 @@ func (c *ClusterSettingsCollector) Update(ctx context.Context, ch chan<- prometh
185142
}
186143

187144
// Max shards per node
188-
if maxShardsPerNodeString, ok := merged.Cluster.MaxShardsPerNode.(string); ok {
189-
maxShardsPerNode, err := strconv.ParseInt(maxShardsPerNodeString, 10, 64)
190-
if err == nil {
191-
ch <- prometheus.MustNewConstMetric(
192-
clusterSettingsDesc["maxShardsPerNode"],
193-
prometheus.GaugeValue,
194-
float64(maxShardsPerNode),
195-
)
196-
}
145+
if maxShardsPerNode, err := strconv.ParseInt(merged.Cluster.MaxShardsPerNode, 10, 64); err == nil {
146+
ch <- prometheus.MustNewConstMetric(
147+
clusterSettingsDesc["maxShardsPerNode"],
148+
prometheus.GaugeValue,
149+
float64(maxShardsPerNode),
150+
)
197151
}
198152

199153
// Shard allocation enabled
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2021 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collector
15+
16+
import (
17+
"encoding/json"
18+
"github.com/mitchellh/mapstructure"
19+
)
20+
21+
// clusterSettingsResponse is a representation of a Elasticsearch Cluster Settings
22+
type clusterSettingsResponse struct {
23+
Defaults clusterSettingsSection `json:"defaults"`
24+
Persistent clusterSettingsSection `json:"persistent"`
25+
Transient clusterSettingsSection `json:"transient"`
26+
}
27+
28+
// clusterSettingsSection is a representation of a Elasticsearch Cluster Settings
29+
type clusterSettingsSection struct {
30+
Cluster clusterSettingsCluster `json:"cluster" mapstructure:",squash"`
31+
}
32+
33+
// clusterSettingsCluster is a representation of a Elasticsearch clusterSettingsCluster Settings
34+
type clusterSettingsCluster struct {
35+
Routing clusterSettingsRouting `json:"routing" mapstructure:",squash"`
36+
37+
MaxShardsPerNode string `json:"max_shards_per_node" mapstructure:"cluster.max_shards_per_node"`
38+
}
39+
40+
// clusterSettingsRouting is a representation of a Elasticsearch Cluster shard routing configuration
41+
type clusterSettingsRouting struct {
42+
Allocation clusterSettingsAllocation `json:"allocation" mapstructure:",squash"`
43+
}
44+
45+
// clusterSettingsAllocation is a representation of a Elasticsearch Cluster shard routing allocation settings
46+
type clusterSettingsAllocation struct {
47+
Enabled string `json:"enable" mapstructure:"cluster.routing.allocation.enable"`
48+
Disk clusterSettingsDisk `json:"disk" mapstructure:",squash"`
49+
}
50+
51+
// clusterSettingsDisk is a representation of a Elasticsearch Cluster shard routing disk allocation settings
52+
type clusterSettingsDisk struct {
53+
ThresholdEnabled string `json:"threshold_enabled" mapstructure:"cluster.routing.allocation.disk.threshold_enabled"`
54+
Watermark clusterSettingsWatermark `json:"watermark" mapstructure:",squash"`
55+
}
56+
57+
// clusterSettingsWatermark is representation of Elasticsearch Cluster shard routing disk allocation watermark settings
58+
type clusterSettingsWatermark struct {
59+
FloodStage string `json:"flood_stage" mapstructure:"cluster.routing.allocation.disk.watermark.flood_stage"`
60+
High string `json:"high" mapstructure:"cluster.routing.allocation.disk.watermark.high"`
61+
Low string `json:"low" mapstructure:"cluster.routing.allocation.disk.watermark.low"`
62+
}
63+
64+
func (c *clusterSettingsSection) UnmarshalJSON(data []byte) error {
65+
var settings map[string]interface{}
66+
67+
if err := json.Unmarshal(data, &settings); err != nil {
68+
return err
69+
}
70+
71+
settings = flatten(settings)
72+
return mapstructure.Decode(settings, c)
73+
}
74+
75+
func flatten(m map[string]interface{}) map[string]interface{} {
76+
result := make(map[string]interface{})
77+
for k1, v1 := range m {
78+
if n, ok := v1.(map[string]interface{}); ok {
79+
for k2, v2 := range flatten(n) {
80+
result[k1+"."+k2] = v2
81+
}
82+
} else {
83+
result[k1] = v1
84+
}
85+
}
86+
return result
87+
}

collector/cluster_settings_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ elasticsearch_clustersettings_allocation_watermark_high_bytes 2.147483648e+11
114114
# HELP elasticsearch_clustersettings_allocation_watermark_low_bytes Low watermark for disk usage in bytes.
115115
# TYPE elasticsearch_clustersettings_allocation_watermark_low_bytes gauge
116116
elasticsearch_clustersettings_allocation_watermark_low_bytes 5.24288e+07
117+
`,
118+
},
119+
{
120+
name: "8.10.4",
121+
file: "../fixtures/settings-8.10.4.json",
122+
want: `
123+
# HELP elasticsearch_clustersettings_stats_max_shards_per_node Current maximum number of shards per node setting.
124+
# TYPE elasticsearch_clustersettings_stats_max_shards_per_node gauge
125+
elasticsearch_clustersettings_stats_max_shards_per_node 1000
126+
# HELP elasticsearch_clustersettings_stats_shard_allocation_enabled Current mode of cluster wide shard routing allocation settings.
127+
# TYPE elasticsearch_clustersettings_stats_shard_allocation_enabled gauge
128+
elasticsearch_clustersettings_stats_shard_allocation_enabled 0
129+
# HELP elasticsearch_clustersettings_allocation_threshold_enabled Is disk allocation decider enabled.
130+
# TYPE elasticsearch_clustersettings_allocation_threshold_enabled gauge
131+
elasticsearch_clustersettings_allocation_threshold_enabled 1
132+
# HELP elasticsearch_clustersettings_allocation_watermark_flood_stage_ratio Flood stage watermark as a ratio.
133+
# TYPE elasticsearch_clustersettings_allocation_watermark_flood_stage_ratio gauge
134+
elasticsearch_clustersettings_allocation_watermark_flood_stage_ratio 0.95
135+
# HELP elasticsearch_clustersettings_allocation_watermark_high_ratio High watermark for disk usage as a ratio.
136+
# TYPE elasticsearch_clustersettings_allocation_watermark_high_ratio gauge
137+
elasticsearch_clustersettings_allocation_watermark_high_ratio 0.9
138+
# HELP elasticsearch_clustersettings_allocation_watermark_low_ratio Low watermark for disk usage as a ratio.
139+
# TYPE elasticsearch_clustersettings_allocation_watermark_low_ratio gauge
140+
elasticsearch_clustersettings_allocation_watermark_low_ratio 0.85
117141
`,
118142
},
119143
}

0 commit comments

Comments
 (0)