Skip to content

Commit

Permalink
Refactor ilm collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Шабакаев Илья Исмаилович committed Jan 9, 2023
1 parent 75c4d26 commit 8cfda85
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 59 deletions.
24 changes: 22 additions & 2 deletions collector/ilm_indices.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Prometheus Authors
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -16,6 +16,7 @@ package collector
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -45,6 +46,19 @@ type IlmIndiciesCollector struct {
ilmMetric ilmMetric
}

type IlmResponse struct {
Indices map[string]IlmIndexResponse `json:"indices"`
}

type IlmIndexResponse struct {
Index string `json:"index"`
Managed bool `json:"managed"`
Phase string `json:"phase"`
Action string `json:"action"`
Step string `json:"step"`
StepTimeMillis float64 `json:"step_time_millis"`
}

var (
defaultIlmIndicesMappingsLabels = []string{"index", "phase", "action", "step"}
)
Expand Down Expand Up @@ -117,7 +131,13 @@ func (i *IlmIndiciesCollector) fetchAndDecodeIlm() (IlmResponse, error) {
return ir, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode)
}

if err := json.NewDecoder(res.Body).Decode(&ir); err != nil {
bts, err := ioutil.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 Down
27 changes: 0 additions & 27 deletions collector/ilm_indices_response.go

This file was deleted.

2 changes: 1 addition & 1 deletion collector/ilm_indices_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Prometheus Authors
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
19 changes: 10 additions & 9 deletions collector/ilm_status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Prometheus Authors
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -49,6 +49,10 @@ type IlmStatusCollector struct {
metric ilmStatusMetric
}

type IlmStatusResponse struct {
OperationMode string `json:"operation_mode"`
}

// NewIlmStatus defines Indices IndexIlms Prometheus metrics
func NewIlmStatus(logger log.Logger, client *http.Client, url *url.URL) *IlmStatusCollector {
subsystem := "ilm"
Expand Down Expand Up @@ -95,7 +99,10 @@ func (im *IlmStatusCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- im.jsonParseFailures.Desc()
}

func (im *IlmStatusCollector) getAndParseURL(u *url.URL) (*IlmStatusResponse, error) {
func (im *IlmStatusCollector) getAndParseURL() (*IlmStatusResponse, error) {
u := *im.url
u.Path = path.Join(im.url.Path, "/_ilm/status")

res, err := im.client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("failed to get from %s://%s:%s%s: %s",
Expand Down Expand Up @@ -127,12 +134,6 @@ func (im *IlmStatusCollector) getAndParseURL(u *url.URL) (*IlmStatusResponse, er
return &imr, nil
}

func (im *IlmStatusCollector) fetchAndDecodeIlm() (*IlmStatusResponse, error) {
u := *im.url
u.Path = path.Join(u.Path, "/_ilm/status")
return im.getAndParseURL(&u)
}

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

Expand All @@ -143,7 +144,7 @@ func (im *IlmStatusCollector) Collect(ch chan<- prometheus.Metric) {
ch <- im.jsonParseFailures
}()

indicesIlmsResponse, err := im.fetchAndDecodeIlm()
indicesIlmsResponse, err := im.getAndParseURL()
if err != nil {
im.up.Set(0)
_ = level.Warn(im.logger).Log(
Expand Down
19 changes: 0 additions & 19 deletions collector/ilm_status_response.go

This file was deleted.

2 changes: 1 addition & 1 deletion collector/ilm_status_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Prometheus Authors
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down

0 comments on commit 8cfda85

Please sign in to comment.