Skip to content

Commit 74d8ca4

Browse files
committed
Refactor tests for indices_mappings
- Remove up, totalScrapes, and jsonParseFailures metrics. They are not useful. - Move fixtures to individual files - Base tests on the metric output for better testing the expected output instead of the internals. Signed-off-by: Joe Adams <[email protected]>
1 parent b0f63df commit 74d8ca4

File tree

4 files changed

+272
-330
lines changed

4 files changed

+272
-330
lines changed

collector/indices_mappings.go

-30
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ type IndicesMappings struct {
4242
client *http.Client
4343
url *url.URL
4444

45-
up prometheus.Gauge
46-
totalScrapes, jsonParseFailures prometheus.Counter
47-
4845
metrics []*indicesMappingsMetric
4946
}
5047

@@ -57,18 +54,6 @@ func NewIndicesMappings(logger log.Logger, client *http.Client, url *url.URL) *I
5754
client: client,
5855
url: url,
5956

60-
up: prometheus.NewGauge(prometheus.GaugeOpts{
61-
Name: prometheus.BuildFQName(namespace, subsystem, "up"),
62-
Help: "Was the last scrape of the Elasticsearch Indices Mappings endpoint successful.",
63-
}),
64-
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
65-
Name: prometheus.BuildFQName(namespace, subsystem, "scrapes_total"),
66-
Help: "Current total Elasticsearch Indices Mappings scrapes.",
67-
}),
68-
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
69-
Name: prometheus.BuildFQName(namespace, subsystem, "json_parse_failures_total"),
70-
Help: "Number of errors while parsing JSON.",
71-
}),
7257
metrics: []*indicesMappingsMetric{
7358
{
7459
Type: prometheus.GaugeValue,
@@ -117,10 +102,6 @@ func (im *IndicesMappings) Describe(ch chan<- *prometheus.Desc) {
117102
for _, metric := range im.metrics {
118103
ch <- metric.Desc
119104
}
120-
121-
ch <- im.up.Desc()
122-
ch <- im.totalScrapes.Desc()
123-
ch <- im.jsonParseFailures.Desc()
124105
}
125106

126107
func (im *IndicesMappings) getAndParseURL(u *url.URL) (*IndicesMappingsResponse, error) {
@@ -148,7 +129,6 @@ func (im *IndicesMappings) getAndParseURL(u *url.URL) (*IndicesMappingsResponse,
148129

149130
var imr IndicesMappingsResponse
150131
if err := json.Unmarshal(body, &imr); err != nil {
151-
im.jsonParseFailures.Inc()
152132
return nil, err
153133
}
154134

@@ -163,24 +143,14 @@ func (im *IndicesMappings) fetchAndDecodeIndicesMappings() (*IndicesMappingsResp
163143

164144
// Collect gets all indices mappings metric values
165145
func (im *IndicesMappings) Collect(ch chan<- prometheus.Metric) {
166-
167-
im.totalScrapes.Inc()
168-
defer func() {
169-
ch <- im.up
170-
ch <- im.totalScrapes
171-
ch <- im.jsonParseFailures
172-
}()
173-
174146
indicesMappingsResponse, err := im.fetchAndDecodeIndicesMappings()
175147
if err != nil {
176-
im.up.Set(0)
177148
level.Warn(im.logger).Log(
178149
"msg", "failed to fetch and decode cluster mappings stats",
179150
"err", err,
180151
)
181152
return
182153
}
183-
im.up.Set(1)
184154

185155
for _, metric := range im.metrics {
186156
for indexName, mappings := range *indicesMappingsResponse {

0 commit comments

Comments
 (0)