|
| 1 | +// Copyright 2025 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "log/slog" |
| 21 | + "net/http" |
| 22 | + "net/url" |
| 23 | + |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + ilmStatusOptions = []string{"STOPPED", "RUNNING", "STOPPING"} |
| 29 | + |
| 30 | + ilmIndexStatus = prometheus.NewDesc( |
| 31 | + prometheus.BuildFQName(namespace, "ilm_index", "status"), |
| 32 | + "Status of ILM policy for index", |
| 33 | + []string{"index", "phase", "action", "step"}, nil) |
| 34 | + |
| 35 | + ilmStatus = prometheus.NewDesc( |
| 36 | + prometheus.BuildFQName(namespace, "ilm", "status"), |
| 37 | + "Current status of ILM. Status can be STOPPED, RUNNING, STOPPING.", |
| 38 | + []string{"operation_mode"}, nil, |
| 39 | + ) |
| 40 | +) |
| 41 | + |
| 42 | +func init() { |
| 43 | + registerCollector("ilm", defaultDisabled, NewILM) |
| 44 | +} |
| 45 | + |
| 46 | +type ILM struct { |
| 47 | + logger *slog.Logger |
| 48 | + hc *http.Client |
| 49 | + u *url.URL |
| 50 | +} |
| 51 | + |
| 52 | +func NewILM(logger *slog.Logger, u *url.URL, hc *http.Client) (Collector, error) { |
| 53 | + return &ILM{ |
| 54 | + logger: logger, |
| 55 | + hc: hc, |
| 56 | + u: u, |
| 57 | + }, nil |
| 58 | +} |
| 59 | + |
| 60 | +type IlmResponse struct { |
| 61 | + Indices map[string]IlmIndexResponse `json:"indices"` |
| 62 | +} |
| 63 | + |
| 64 | +type IlmIndexResponse struct { |
| 65 | + Index string `json:"index"` |
| 66 | + Managed bool `json:"managed"` |
| 67 | + Phase string `json:"phase"` |
| 68 | + Action string `json:"action"` |
| 69 | + Step string `json:"step"` |
| 70 | + StepTimeMillis float64 `json:"step_time_millis"` |
| 71 | +} |
| 72 | + |
| 73 | +type IlmStatusResponse struct { |
| 74 | + OperationMode string `json:"operation_mode"` |
| 75 | +} |
| 76 | + |
| 77 | +func (i *ILM) Update(ctx context.Context, ch chan<- prometheus.Metric) error { |
| 78 | + var ir IlmResponse |
| 79 | + |
| 80 | + indexURL := i.u.ResolveReference(&url.URL{Path: "/_all/_ilm/explain"}) |
| 81 | + |
| 82 | + indexResp, err := getURL(ctx, i.hc, i.logger, indexURL.String()) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to load ILM url: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + if err := json.Unmarshal(indexResp, &ir); err != nil { |
| 88 | + return fmt.Errorf("failed to decode JSON body: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + var isr IlmStatusResponse |
| 92 | + |
| 93 | + indexStatusURL := i.u.ResolveReference(&url.URL{Path: "/_ilm/status"}) |
| 94 | + |
| 95 | + indexStatusResp, err := getURL(ctx, i.hc, i.logger, indexStatusURL.String()) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to load ILM url: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + if err := json.Unmarshal(indexStatusResp, &isr); err != nil { |
| 101 | + return fmt.Errorf("failed to decode JSON body: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + for name, ilm := range ir.Indices { |
| 105 | + ch <- prometheus.MustNewConstMetric( |
| 106 | + ilmIndexStatus, |
| 107 | + prometheus.GaugeValue, |
| 108 | + bool2Float(ilm.Managed), |
| 109 | + name, ilm.Phase, ilm.Action, ilm.Step, |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + for _, status := range ilmStatusOptions { |
| 114 | + statusActive := false |
| 115 | + if isr.OperationMode == status { |
| 116 | + statusActive = true |
| 117 | + } |
| 118 | + |
| 119 | + ch <- prometheus.MustNewConstMetric( |
| 120 | + ilmStatus, |
| 121 | + prometheus.GaugeValue, |
| 122 | + bool2Float(statusActive), |
| 123 | + status, |
| 124 | + ) |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
0 commit comments