forked from prometheus-community/elasticsearch_exporter
/
indices_settings.go
131 lines (113 loc) · 3.35 KB
/
indices_settings.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
package collector
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
// IndicesSettings information struct
type IndicesSettings struct {
logger log.Logger
client *http.Client
url *url.URL
up prometheus.Gauge
readOnlyIndices prometheus.Gauge
totalScrapes, jsonParseFailures prometheus.Counter
}
// NewIndicesSettings defines Indices Settings Prometheus metrics
func NewIndicesSettings(logger log.Logger, client *http.Client, url *url.URL) *IndicesSettings {
return &IndicesSettings{
logger: logger,
client: client,
url: url,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "indices_settings_stats", "up"),
Help: "Was the last scrape of the ElasticSearch Indices Settings endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "indices_settings_stats", "total_scrapes"),
Help: "Current total ElasticSearch Indices Settings scrapes.",
}),
readOnlyIndices: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "indices_settings_stats", "read_only_indices"),
Help: "Current number of read only indices within cluster",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "indices_settings_stats", "json_parse_failures"),
Help: "Number of errors while parsing JSON.",
}),
}
}
// Describe add Snapshots metrics descriptions
func (cs *IndicesSettings) Describe(ch chan<- *prometheus.Desc) {
ch <- cs.up.Desc()
ch <- cs.totalScrapes.Desc()
ch <- cs.readOnlyIndices.Desc()
ch <- cs.jsonParseFailures.Desc()
}
func (cs *IndicesSettings) getAndParseURL(u *url.URL, data interface{}) error {
res, err := cs.client.Get(u.String())
if err != nil {
return fmt.Errorf("failed to get from %s://%s:%s%s: %s",
u.Scheme, u.Hostname(), u.Port(), u.Path, err)
}
defer func() {
err = res.Body.Close()
if err != nil {
_ = level.Warn(cs.logger).Log(
"msg", "failed to close http.Client",
"err", err,
)
}
}()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP Request failed with code %d", res.StatusCode)
}
if err := json.NewDecoder(res.Body).Decode(data); err != nil {
cs.jsonParseFailures.Inc()
return err
}
return nil
}
func (cs *IndicesSettings) fetchAndDecodeIndicesSettings() (IndicesSettingsResponse, error) {
u := *cs.url
u.Path = path.Join(u.Path, "/_all/_settings")
var asr IndicesSettingsResponse
err := cs.getAndParseURL(&u, &asr)
if err != nil {
return asr, err
}
return asr, err
}
// Collect gets all indices settings metric values
func (cs *IndicesSettings) Collect(ch chan<- prometheus.Metric) {
cs.totalScrapes.Inc()
defer func() {
ch <- cs.up
ch <- cs.totalScrapes
ch <- cs.jsonParseFailures
ch <- cs.readOnlyIndices
}()
asr, err := cs.fetchAndDecodeIndicesSettings()
if err != nil {
cs.readOnlyIndices.Set(0)
cs.up.Set(0)
_ = level.Warn(cs.logger).Log(
"msg", "failed to fetch and decode cluster settings stats",
"err", err,
)
return
}
cs.up.Set(1)
var c int
for _, value := range asr {
if value.Settings.IndexInfo.Blocks.ReadOnly == "true" {
c++
}
}
cs.readOnlyIndices.Set(float64(c))
}