Skip to content

Commit 5500ee6

Browse files
Шабакаев Илья Исмаиловичiishabakaev
authored andcommitted
Refactor ilm collectors
Signed-off-by: Шабакаев Илья Исмаилович <[email protected]> Signed-off-by: Шабакаев Илья Исмаилович <[email protected]>
1 parent 75c4d26 commit 5500ee6

File tree

6 files changed

+33
-58
lines changed

6 files changed

+33
-58
lines changed

collector/ilm_indices.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Prometheus Authors
1+
// Copyright 2023 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -16,6 +16,7 @@ package collector
1616
import (
1717
"encoding/json"
1818
"fmt"
19+
"io/ioutil"
1920
"net/http"
2021
"net/url"
2122
"path"
@@ -45,6 +46,19 @@ type IlmIndiciesCollector struct {
4546
ilmMetric ilmMetric
4647
}
4748

49+
type IlmResponse struct {
50+
Indices map[string]IlmIndexResponse `json:"indices"`
51+
}
52+
53+
type IlmIndexResponse struct {
54+
Index string `json:"index"`
55+
Managed bool `json:"managed"`
56+
Phase string `json:"phase"`
57+
Action string `json:"action"`
58+
Step string `json:"step"`
59+
StepTimeMillis float64 `json:"step_time_millis"`
60+
}
61+
4862
var (
4963
defaultIlmIndicesMappingsLabels = []string{"index", "phase", "action", "step"}
5064
)
@@ -117,7 +131,13 @@ func (i *IlmIndiciesCollector) fetchAndDecodeIlm() (IlmResponse, error) {
117131
return ir, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode)
118132
}
119133

120-
if err := json.NewDecoder(res.Body).Decode(&ir); err != nil {
134+
bts, err := ioutil.ReadAll(res.Body)
135+
if err != nil {
136+
i.jsonParseFailures.Inc()
137+
return ir, err
138+
}
139+
140+
if err := json.Unmarshal(bts, &ir); err != nil {
121141
i.jsonParseFailures.Inc()
122142
return ir, err
123143
}

collector/ilm_indices_response.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

collector/ilm_indices_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Prometheus Authors
1+
// Copyright 2023 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at

collector/ilm_status.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Prometheus Authors
1+
// Copyright 2023 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -49,6 +49,10 @@ type IlmStatusCollector struct {
4949
metric ilmStatusMetric
5050
}
5151

52+
type IlmStatusResponse struct {
53+
OperationMode string `json:"operation_mode"`
54+
}
55+
5256
// NewIlmStatus defines Indices IndexIlms Prometheus metrics
5357
func NewIlmStatus(logger log.Logger, client *http.Client, url *url.URL) *IlmStatusCollector {
5458
subsystem := "ilm"
@@ -95,7 +99,10 @@ func (im *IlmStatusCollector) Describe(ch chan<- *prometheus.Desc) {
9599
ch <- im.jsonParseFailures.Desc()
96100
}
97101

98-
func (im *IlmStatusCollector) getAndParseURL(u *url.URL) (*IlmStatusResponse, error) {
102+
func (im *IlmStatusCollector) fetchAndDecodeIlm() (*IlmStatusResponse, error) {
103+
u := *im.url
104+
u.Path = path.Join(im.url.Path, "/_ilm/status")
105+
99106
res, err := im.client.Get(u.String())
100107
if err != nil {
101108
return nil, fmt.Errorf("failed to get from %s://%s:%s%s: %s",
@@ -127,12 +134,6 @@ func (im *IlmStatusCollector) getAndParseURL(u *url.URL) (*IlmStatusResponse, er
127134
return &imr, nil
128135
}
129136

130-
func (im *IlmStatusCollector) fetchAndDecodeIlm() (*IlmStatusResponse, error) {
131-
u := *im.url
132-
u.Path = path.Join(u.Path, "/_ilm/status")
133-
return im.getAndParseURL(&u)
134-
}
135-
136137
// Collect gets all indices Ilms metric values
137138
func (im *IlmStatusCollector) Collect(ch chan<- prometheus.Metric) {
138139

collector/ilm_status_response.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

collector/ilm_status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Prometheus Authors
1+
// Copyright 2023 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at

0 commit comments

Comments
 (0)