-
Notifications
You must be signed in to change notification settings - Fork 793
/
cluster_health.go
252 lines (232 loc) · 7.02 KB
/
cluster_health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package collector
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "elasticsearch"
)
var (
colors = []string{"green", "yellow", "red"}
defaultClusterHealthLabels = []string{"cluster"}
)
type clusterHealthMetric struct {
Type prometheus.ValueType
Desc *prometheus.Desc
Value func(clusterHealth clusterHealthResponse) float64
}
type clusterHealthStatusMetric struct {
Type prometheus.ValueType
Desc *prometheus.Desc
Value func(clusterHealth clusterHealthResponse, color string) float64
Labels func(clusterName, color string) []string
}
type ClusterHealth struct {
logger log.Logger
client *http.Client
url *url.URL
metrics []*clusterHealthMetric
statusMetric *clusterHealthStatusMetric
}
func NewClusterHealth(logger log.Logger, client *http.Client, url *url.URL) *ClusterHealth {
subsystem := "cluster_health"
return &ClusterHealth{
logger: logger,
client: client,
url: url,
metrics: []*clusterHealthMetric{
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "active_primary_shards"),
"Tthe number of primary shards in your cluster. This is an aggregate total across all indices.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.ActivePrimaryShards)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "active_shards"),
"Aggregate total of all shards across all indices, which includes replica shards.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.ActiveShards)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "delayed_unassigned_shards"),
"Shards delayed to reduce reallocation overhead",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.DelayedUnassignedShards)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "initializing_shards"),
"Count of shards that are being freshly created.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.InitializingShards)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "number_of_data_nodes"),
"Number of data nodes in the cluster.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.NumberOfDataNodes)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "number_of_in_flight_fetch"),
"The number of ongoing shard info requests.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.NumberOfInFlightFetch)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "number_of_nodes"),
"Number of nodes in the cluster.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.NumberOfNodes)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "number_of_pending_tasks"),
"Cluster level changes which have not yet been executed",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.NumberOfPendingTasks)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "relocating_shards"),
"The number of shards that are currently moving from one node to another node.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.RelocatingShards)
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "timed_out"),
"Number of cluster health checks timed out",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
if clusterHealth.TimedOut {
return 1
}
return 0
},
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "unassigned_shards"),
"The number of shards that exist in the cluster state, but cannot be found in the cluster itself.",
defaultClusterHealthLabels, nil,
),
Value: func(clusterHealth clusterHealthResponse) float64 {
return float64(clusterHealth.UnassignedShards)
},
},
},
statusMetric: &clusterHealthStatusMetric{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "status"),
"Whether all primary and replica shards are allocated.",
[]string{"cluster", "color"}, nil,
),
Value: func(clusterHealth clusterHealthResponse, color string) float64 {
if clusterHealth.Status == color {
return 1
}
return 0
},
},
}
}
func (c *ClusterHealth) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.metrics {
ch <- metric.Desc
}
ch <- c.statusMetric.Desc
}
func (c *ClusterHealth) fetchAndDecodeClusterHealth() (clusterHealthResponse, error) {
var chr clusterHealthResponse
u := *c.url
u.Path = "/_cluster/health"
res, err := c.client.Get(u.String())
if err != nil {
return chr, fmt.Errorf("failed to get cluster health from %s: %s", u.String(), err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return chr, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode)
}
if err := json.NewDecoder(res.Body).Decode(&chr); err != nil {
return chr, err
}
return chr, nil
}
func (c *ClusterHealth) Collect(ch chan<- prometheus.Metric) {
clusterHealthResponse, err := c.fetchAndDecodeClusterHealth()
if err != nil {
level.Warn(c.logger).Log(
"msg", "failed to fetch and decode cluster health",
"err", err,
)
return
}
for _, metric := range c.metrics {
ch <- prometheus.MustNewConstMetric(
metric.Desc,
metric.Type,
metric.Value(clusterHealthResponse),
clusterHealthResponse.ClusterName,
)
}
for _, color := range colors {
ch <- prometheus.MustNewConstMetric(
c.statusMetric.Desc,
c.statusMetric.Type,
c.statusMetric.Value(clusterHealthResponse, color),
clusterHealthResponse.ClusterName, color,
)
}
}