Skip to content

Refactor tests for indices collector #937

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

Merged
merged 1 commit into from
Oct 16, 2024
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
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
Loading