Skip to content

Commit

Permalink
Merge pull request #814 from sysadmind/refactor-tests-indices-mappings
Browse files Browse the repository at this point in the history
Refactor tests for indices_mappings
  • Loading branch information
SuperQ authored Dec 2, 2023
2 parents dc2704a + 74d8ca4 commit 3c7b5c7
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 330 deletions.
30 changes: 0 additions & 30 deletions collector/indices_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ type IndicesMappings struct {
client *http.Client
url *url.URL

up prometheus.Gauge
totalScrapes, jsonParseFailures prometheus.Counter

metrics []*indicesMappingsMetric
}

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

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "up"),
Help: "Was the last scrape of the Elasticsearch Indices Mappings endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "scrapes_total"),
Help: "Current total Elasticsearch Indices Mappings scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "json_parse_failures_total"),
Help: "Number of errors while parsing JSON.",
}),
metrics: []*indicesMappingsMetric{
{
Type: prometheus.GaugeValue,
Expand Down Expand Up @@ -117,10 +102,6 @@ func (im *IndicesMappings) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range im.metrics {
ch <- metric.Desc
}

ch <- im.up.Desc()
ch <- im.totalScrapes.Desc()
ch <- im.jsonParseFailures.Desc()
}

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

var imr IndicesMappingsResponse
if err := json.Unmarshal(body, &imr); err != nil {
im.jsonParseFailures.Inc()
return nil, err
}

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

// Collect gets all indices mappings metric values
func (im *IndicesMappings) Collect(ch chan<- prometheus.Metric) {

im.totalScrapes.Inc()
defer func() {
ch <- im.up
ch <- im.totalScrapes
ch <- im.jsonParseFailures
}()

indicesMappingsResponse, err := im.fetchAndDecodeIndicesMappings()
if err != nil {
im.up.Set(0)
level.Warn(im.logger).Log(
"msg", "failed to fetch and decode cluster mappings stats",
"err", err,
)
return
}
im.up.Set(1)

for _, metric := range im.metrics {
for indexName, mappings := range *indicesMappingsResponse {
Expand Down
Loading

0 comments on commit 3c7b5c7

Please sign in to comment.