Skip to content

Commit

Permalink
Merge pull request #937 from sysadmind/indices-test-refactor
Browse files Browse the repository at this point in the history
Refactor tests for indices collector
  • Loading branch information
SuperQ authored Oct 16, 2024
2 parents 33f42c6 + 08a1cee commit ccd458b
Show file tree
Hide file tree
Showing 10 changed files with 5,920 additions and 136 deletions.
44 changes: 9 additions & 35 deletions collector/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ package collector
import (
"encoding/json"
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/elasticsearch_exporter/pkg/clusterinfo"
"github.com/prometheus/client_golang/prometheus"
"io"
"net/http"
"net/url"
"path"
"sort"
"strconv"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/elasticsearch_exporter/pkg/clusterinfo"
"github.com/prometheus/client_golang/prometheus"
)

type labels struct {
Expand Down Expand Up @@ -64,10 +65,6 @@ type Indices struct {
clusterInfoCh chan *clusterinfo.Response
lastClusterInfo *clusterinfo.Response

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

indexMetrics []*indexMetric
shardMetrics []*shardMetric
aliasMetrics []*aliasMetric
Expand Down Expand Up @@ -129,19 +126,6 @@ func NewIndices(logger log.Logger, client *http.Client, url *url.URL, shards boo
ClusterName: "unknown_cluster",
},

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "index_stats", "up"),
Help: "Was the last scrape of the Elasticsearch index endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "index_stats", "total_scrapes"),
Help: "Current total Elasticsearch index scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "index_stats", "json_parse_failures"),
Help: "Number of errors while parsing JSON.",
}),

indexMetrics: []*indexMetric{
{
Type: prometheus.GaugeValue,
Expand Down Expand Up @@ -1117,9 +1101,10 @@ func (i *Indices) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range i.indexMetrics {
ch <- metric.Desc
}
ch <- i.up.Desc()
ch <- i.totalScrapes.Desc()
ch <- i.jsonParseFailures.Desc()
for _, metric := range i.aliasMetrics {
ch <- metric.Desc
}

}

func (i *Indices) fetchAndDecodeIndexStats() (indexStatsResponse, error) {
Expand All @@ -1139,7 +1124,6 @@ func (i *Indices) fetchAndDecodeIndexStats() (indexStatsResponse, error) {
}

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

Expand Down Expand Up @@ -1179,7 +1163,6 @@ func (i *Indices) fetchAndDecodeAliases() (aliasesResponse, error) {
}

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

Expand Down Expand Up @@ -1217,24 +1200,15 @@ func (i *Indices) queryURL(u *url.URL) ([]byte, error) {

// Collect gets Indices metric values
func (i *Indices) Collect(ch chan<- prometheus.Metric) {
i.totalScrapes.Inc()
defer func() {
ch <- i.up
ch <- i.totalScrapes
ch <- i.jsonParseFailures
}()

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

// Alias stats
if i.aliases {
Expand Down
1,746 changes: 1,645 additions & 101 deletions collector/indices_test.go

Large diffs are not rendered by default.

Loading

0 comments on commit ccd458b

Please sign in to comment.