Skip to content

Commit 391c7df

Browse files
committed
Refactor tests for ilm_indices
- 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 391c7df

File tree

3 files changed

+63
-79
lines changed

3 files changed

+63
-79
lines changed

collector/ilm_indices.go

-30
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ type IlmIndiciesCollector struct {
3939
client *http.Client
4040
url *url.URL
4141

42-
up prometheus.Gauge
43-
totalScrapes prometheus.Counter
44-
jsonParseFailures prometheus.Counter
45-
4642
ilmMetric ilmMetric
4743
}
4844

@@ -72,18 +68,6 @@ func NewIlmIndicies(logger log.Logger, client *http.Client, url *url.URL) *IlmIn
7268
client: client,
7369
url: url,
7470

75-
up: prometheus.NewGauge(prometheus.GaugeOpts{
76-
Name: prometheus.BuildFQName(namespace, subsystem, "up"),
77-
Help: "Was the last scrape of the ElasticSearch ILM endpoint successful.",
78-
}),
79-
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
80-
Name: prometheus.BuildFQName(namespace, subsystem, "total_scrapes"),
81-
Help: "Current total ElasticSearch ILM scrapes.",
82-
}),
83-
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
84-
Name: prometheus.BuildFQName(namespace, subsystem, "json_parse_failures"),
85-
Help: "Number of errors while parsing JSON.",
86-
}),
8771
ilmMetric: ilmMetric{
8872
Type: prometheus.GaugeValue,
8973
Desc: prometheus.NewDesc(
@@ -100,9 +84,6 @@ func NewIlmIndicies(logger log.Logger, client *http.Client, url *url.URL) *IlmIn
10084
// Describe adds metrics description
10185
func (i *IlmIndiciesCollector) Describe(ch chan<- *prometheus.Desc) {
10286
ch <- i.ilmMetric.Desc
103-
ch <- i.up.Desc()
104-
ch <- i.totalScrapes.Desc()
105-
ch <- i.jsonParseFailures.Desc()
10687
}
10788

10889
func (i *IlmIndiciesCollector) fetchAndDecodeIlm() (IlmResponse, error) {
@@ -133,12 +114,10 @@ func (i *IlmIndiciesCollector) fetchAndDecodeIlm() (IlmResponse, error) {
133114

134115
bts, err := io.ReadAll(res.Body)
135116
if err != nil {
136-
i.jsonParseFailures.Inc()
137117
return ir, err
138118
}
139119

140120
if err := json.Unmarshal(bts, &ir); err != nil {
141-
i.jsonParseFailures.Inc()
142121
return ir, err
143122
}
144123

@@ -154,24 +133,15 @@ func bool2int(managed bool) float64 {
154133

155134
// Collect pulls metric values from Elasticsearch
156135
func (i *IlmIndiciesCollector) Collect(ch chan<- prometheus.Metric) {
157-
defer func() {
158-
ch <- i.up
159-
ch <- i.totalScrapes
160-
ch <- i.jsonParseFailures
161-
}()
162-
163136
// indices
164137
ilmResp, err := i.fetchAndDecodeIlm()
165138
if err != nil {
166-
i.up.Set(0)
167139
level.Warn(i.logger).Log(
168140
"msg", "failed to fetch and decode ILM stats",
169141
"err", err,
170142
)
171143
return
172144
}
173-
i.totalScrapes.Inc()
174-
i.up.Set(1)
175145

176146
for indexName, indexIlm := range ilmResp.Indices {
177147
ch <- prometheus.MustNewConstMetric(

collector/ilm_indices_test.go

+43-49
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
package collector
1515

1616
import (
17-
"fmt"
17+
"io"
1818
"net/http"
1919
"net/http/httptest"
2020
"net/url"
21+
"os"
22+
"strings"
2123
"testing"
2224

2325
"github.com/go-kit/log"
26+
"github.com/prometheus/client_golang/prometheus/testutil"
2427
)
2528

2629
func TestILMMetrics(t *testing.T) {
@@ -61,57 +64,48 @@ func TestILMMetrics(t *testing.T) {
6164
// }
6265
// '
6366
// curl http://localhost:9200/_all/_ilm/explain
64-
tcs := map[string]string{
65-
"6.6.0": `{
66-
"indices": {
67-
"twitter": { "index": "twitter", "managed": false },
68-
"facebook": {
69-
"index": "facebook",
70-
"managed": true,
71-
"policy": "my_policy",
72-
"lifecycle_date_millis": 1660799138565,
73-
"phase": "new",
74-
"phase_time_millis": 1660799138651,
75-
"action": "complete",
76-
"action_time_millis": 1660799138651,
77-
"step": "complete",
78-
"step_time_millis": 1660799138651
79-
}
80-
}
81-
}`,
67+
tests := []struct {
68+
name string
69+
file string
70+
want string
71+
}{
72+
{
73+
name: "6.6.0",
74+
file: "../fixtures/ilm_indices/6.6.0.json",
75+
want: `
76+
# HELP elasticsearch_ilm_index_status Status of ILM policy for index
77+
# TYPE elasticsearch_ilm_index_status gauge
78+
elasticsearch_ilm_index_status{action="",index="twitter",phase="",step=""} 0
79+
elasticsearch_ilm_index_status{action="complete",index="facebook",phase="new",step="complete"} 1
80+
`,
81+
},
8282
}
83-
for ver, out := range tcs {
84-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
85-
fmt.Fprintln(w, out)
86-
}))
87-
defer ts.Close()
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
f, err := os.Open(tt.file)
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
defer f.Close()
90+
91+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
92+
io.Copy(w, f)
93+
}))
94+
defer ts.Close()
8895

89-
u, err := url.Parse(ts.URL)
90-
if err != nil {
91-
t.Fatalf("Failed to parse URL: %s", err)
92-
}
93-
c := NewIlmIndicies(log.NewNopLogger(), http.DefaultClient, u)
94-
chr, err := c.fetchAndDecodeIlm()
95-
if err != nil {
96-
t.Fatalf("Failed to fetch or decode indices ilm metrics: %s", err)
97-
}
98-
t.Logf("[%s] indices ilm metrics Response: %+v", ver, chr)
96+
u, err := url.Parse(ts.URL)
97+
if err != nil {
98+
t.Fatal(err)
99+
}
99100

100-
if chr.Indices["twitter"].Managed != false {
101-
t.Errorf("Invalid ilm metrics at twitter.managed")
102-
}
103-
if chr.Indices["facebook"].Managed != true {
104-
t.Errorf("Invalid ilm metrics at facebook.managed")
105-
}
106-
if chr.Indices["facebook"].Phase != "new" {
107-
t.Errorf("Invalid ilm metrics at facebook.phase")
108-
}
109-
if chr.Indices["facebook"].Action != "complete" {
110-
t.Errorf("Invalid ilm metrics at facebook.action")
111-
}
112-
if chr.Indices["facebook"].Step != "complete" {
113-
t.Errorf("Invalid ilm metrics at facebook.step")
114-
}
101+
c := NewIlmIndicies(log.NewNopLogger(), http.DefaultClient, u)
102+
if err != nil {
103+
t.Fatal(err)
104+
}
115105

106+
if err := testutil.CollectAndCompare(c, strings.NewReader(tt.want)); err != nil {
107+
t.Fatal(err)
108+
}
109+
})
116110
}
117111
}

fixtures/ilm_indices/6.6.0.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"indices": {
3+
"twitter": {
4+
"index": "twitter",
5+
"managed": false
6+
},
7+
"facebook": {
8+
"index": "facebook",
9+
"managed": true,
10+
"policy": "my_policy",
11+
"lifecycle_date_millis": 1660799138565,
12+
"phase": "new",
13+
"phase_time_millis": 1660799138651,
14+
"action": "complete",
15+
"action_time_millis": 1660799138651,
16+
"step": "complete",
17+
"step_time_millis": 1660799138651
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)