Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests for ilm_indices #812

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions collector/ilm_indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ type IlmIndiciesCollector struct {
client *http.Client
url *url.URL

up prometheus.Gauge
totalScrapes prometheus.Counter
jsonParseFailures prometheus.Counter

ilmMetric ilmMetric
}

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

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "up"),
Help: "Was the last scrape of the ElasticSearch ILM endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "total_scrapes"),
Help: "Current total ElasticSearch ILM scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "json_parse_failures"),
Help: "Number of errors while parsing JSON.",
}),
ilmMetric: ilmMetric{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
Expand All @@ -100,9 +84,6 @@ func NewIlmIndicies(logger log.Logger, client *http.Client, url *url.URL) *IlmIn
// Describe adds metrics description
func (i *IlmIndiciesCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- i.ilmMetric.Desc
ch <- i.up.Desc()
ch <- i.totalScrapes.Desc()
ch <- i.jsonParseFailures.Desc()
}

func (i *IlmIndiciesCollector) fetchAndDecodeIlm() (IlmResponse, error) {
Expand Down Expand Up @@ -133,12 +114,10 @@ func (i *IlmIndiciesCollector) fetchAndDecodeIlm() (IlmResponse, error) {

bts, err := io.ReadAll(res.Body)
if err != nil {
i.jsonParseFailures.Inc()
return ir, err
}

if err := json.Unmarshal(bts, &ir); err != nil {
i.jsonParseFailures.Inc()
return ir, err
}

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

// Collect pulls metric values from Elasticsearch
func (i *IlmIndiciesCollector) Collect(ch chan<- prometheus.Metric) {
defer func() {
ch <- i.up
ch <- i.totalScrapes
ch <- i.jsonParseFailures
}()

// indices
ilmResp, err := i.fetchAndDecodeIlm()
if err != nil {
i.up.Set(0)
level.Warn(i.logger).Log(
"msg", "failed to fetch and decode ILM stats",
"err", err,
)
return
}
i.totalScrapes.Inc()
i.up.Set(1)

for indexName, indexIlm := range ilmResp.Indices {
ch <- prometheus.MustNewConstMetric(
Expand Down
92 changes: 43 additions & 49 deletions collector/ilm_indices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
package collector

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus/testutil"
)

func TestILMMetrics(t *testing.T) {
Expand Down Expand Up @@ -61,57 +64,48 @@ func TestILMMetrics(t *testing.T) {
// }
// '
// curl http://localhost:9200/_all/_ilm/explain
tcs := map[string]string{
"6.6.0": `{
"indices": {
"twitter": { "index": "twitter", "managed": false },
"facebook": {
"index": "facebook",
"managed": true,
"policy": "my_policy",
"lifecycle_date_millis": 1660799138565,
"phase": "new",
"phase_time_millis": 1660799138651,
"action": "complete",
"action_time_millis": 1660799138651,
"step": "complete",
"step_time_millis": 1660799138651
}
}
}`,
tests := []struct {
name string
file string
want string
}{
{
name: "6.6.0",
file: "../fixtures/ilm_indices/6.6.0.json",
want: `
# HELP elasticsearch_ilm_index_status Status of ILM policy for index
# TYPE elasticsearch_ilm_index_status gauge
elasticsearch_ilm_index_status{action="",index="twitter",phase="",step=""} 0
elasticsearch_ilm_index_status{action="complete",index="facebook",phase="new",step="complete"} 1
`,
},
}
for ver, out := range tcs {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, out)
}))
defer ts.Close()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(tt.file)
if err != nil {
t.Fatal(err)
}
defer f.Close()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, f)
}))
defer ts.Close()

u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
c := NewIlmIndicies(log.NewNopLogger(), http.DefaultClient, u)
chr, err := c.fetchAndDecodeIlm()
if err != nil {
t.Fatalf("Failed to fetch or decode indices ilm metrics: %s", err)
}
t.Logf("[%s] indices ilm metrics Response: %+v", ver, chr)
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}

if chr.Indices["twitter"].Managed != false {
t.Errorf("Invalid ilm metrics at twitter.managed")
}
if chr.Indices["facebook"].Managed != true {
t.Errorf("Invalid ilm metrics at facebook.managed")
}
if chr.Indices["facebook"].Phase != "new" {
t.Errorf("Invalid ilm metrics at facebook.phase")
}
if chr.Indices["facebook"].Action != "complete" {
t.Errorf("Invalid ilm metrics at facebook.action")
}
if chr.Indices["facebook"].Step != "complete" {
t.Errorf("Invalid ilm metrics at facebook.step")
}
c := NewIlmIndicies(log.NewNopLogger(), http.DefaultClient, u)
if err != nil {
t.Fatal(err)
}

if err := testutil.CollectAndCompare(c, strings.NewReader(tt.want)); err != nil {
t.Fatal(err)
}
})
}
}
20 changes: 20 additions & 0 deletions fixtures/ilm_indices/6.6.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"indices": {
"twitter": {
"index": "twitter",
"managed": false
},
"facebook": {
"index": "facebook",
"managed": true,
"policy": "my_policy",
"lifecycle_date_millis": 1660799138565,
"phase": "new",
"phase_time_millis": 1660799138651,
"action": "complete",
"action_time_millis": 1660799138651,
"step": "complete",
"step_time_millis": 1660799138651
}
}
}