Skip to content
Open
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
163 changes: 163 additions & 0 deletions collector/ilm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2021 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

type ilmMetric struct {
Type prometheus.ValueType
Desc *prometheus.Desc
Value func(val int) float64
Labels func(ilmIndex string, ilmPhase string, ilmAction string, ilmStep string) []string
}

// Index Lifecycle Management information object
type Ilm struct {
logger log.Logger
client *http.Client
url *url.URL

up prometheus.Gauge
totalScrapes prometheus.Counter
jsonParseFailures prometheus.Counter

ilmMetric ilmMetric
}

// NewIlm defines Index Lifecycle Management Prometheus metrics
func NewIlm(logger log.Logger, client *http.Client, url *url.URL) *Ilm {
return &Ilm{
logger: logger,
client: client,
url: url,

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "ilm", "up"),
Help: "Was the last scrape of the ElasticSearch ILM endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "ilm", "total_scrapes"),
Help: "Current total ElasticSearch ILM scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "ilm", "json_parse_failures"),
Help: "Number of errors while parsing JSON.",
}),
ilmMetric: ilmMetric{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "ilm", "index_status"),
"Status of ILM policy for index",
[]string{"index", "phase", "action", "step"}, nil),
Value: func(val int) float64 {
return float64(val)
},
Labels: func(ilmIndex string, ilmPhase string, ilmAction string, ilmStep string) []string {
return []string{ilmIndex, ilmPhase, ilmAction, ilmStep}
},
},
}
}

// Describe adds metrics description
func (i *Ilm) Describe(ch chan<- *prometheus.Desc) {
ch <- i.ilmMetric.Desc
ch <- i.up.Desc()
ch <- i.totalScrapes.Desc()
ch <- i.jsonParseFailures.Desc()
}

// Bool2int translates boolean variable to its integer alternative
func (i *Ilm) Bool2int(managed bool) int {
if managed {
return 1
} else {
return 0
}
}

func (i *Ilm) fetchAndDecodeIlm() (IlmResponse, error) {
var ir IlmResponse

u := *i.url
u.Path = path.Join(u.Path, "/_all/_ilm/explain")

res, err := i.client.Get(u.String())
if err != nil {
return ir, fmt.Errorf("failed to get index stats from %s://%s:%s%s: %s",
u.Scheme, u.Hostname(), u.Port(), u.Path, err)
}

defer func() {
err = res.Body.Close()
if err != nil {
_ = level.Warn(i.logger).Log(
"msg", "failed to close http.Client",
"err", err,
)
}
}()

if res.StatusCode != http.StatusOK {
return ir, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode)
}

if err := json.NewDecoder(res.Body).Decode(&ir); err != nil {
i.jsonParseFailures.Inc()
return ir, err
}

return ir, nil
}

// Collect pulls metric values from Elasticsearch
func (i *Ilm) Collect(ch chan<- prometheus.Metric) {
defer func() {
ch <- i.up
ch <- i.totalScrapes
ch <- i.jsonParseFailures
}()

// indices
ilmResp, err := i.fetchAndDecodeIlm()
if err != nil {
i.up.Set(0)
_ = level.Warn(i.logger).Log(
"msg", "failed to fetch and decode ILM stats",
"err", err,
)
return
}
i.totalScrapes.Inc()
i.up.Set(1)

for indexName, indexIlm := range ilmResp.Indices {
ch <- prometheus.MustNewConstMetric(
i.ilmMetric.Desc,
i.ilmMetric.Type,
i.ilmMetric.Value(i.Bool2int(indexIlm.Managed)),
i.ilmMetric.Labels(indexName, indexIlm.Phase, indexIlm.Action, indexIlm.Step)...,
)
}
}
26 changes: 26 additions & 0 deletions collector/ilm_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

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"`
}
56 changes: 56 additions & 0 deletions collector/ilm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2021 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"fmt"
"github.com/go-kit/kit/log"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

func TestIlm(t *testing.T) {
ti := map[string]string{
"7.3.2": `{"indices":{"foo_1":{"index":"foo_1","managed":true,"policy":"foo_policy","lifecycle_date_millis":1575630854324,"phase":"hot","phase_time_millis":1575605054674,"action":"complete","action_time_millis":1575630855862,"step":"complete","step_time_millis":1575630855862,"phase_execution":{"policy":"foo_policy","phase_definition":{"min_age":"0ms","actions":{"rollover":{"max_size":"15gb","max_age":"1d"},"set_priority":{"priority":100}}},"version":7,"modified_date_in_millis":1573070716617}}}}`,
}
for ver, out := range ti {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, out)
}))
defer ts.Close()

u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
i := NewIlm(log.NewNopLogger(), http.DefaultClient, u)
ilm, err := i.fetchAndDecodeIlm()
if err != nil {
t.Fatalf("Failed to fetch or decode ILM stats: %s", err)
}
t.Logf("[%s] ILM Response: %+v", ver, ilm)
for ilmIndex, ilmStats := range ilm.Indices {
t.Logf(
"Index: %s - Managed: %t - Action: %s - Phase: %s - Step: %s",
ilmIndex,
ilmStats.Managed,
ilmStats.Action,
ilmStats.Phase,
ilmStats.Step,
)
}
}
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func main() {
esExportSnapshots = kingpin.Flag("es.snapshots",
"Export stats for the cluster snapshots.").
Default("false").Envar("ES_SNAPSHOTS").Bool()
esExportIlm = kingpin.Flag("es.ilm",
"Export stats for Index Lifecycle Management").
Default("false").Envar("ES_ILM").Bool()
esClusterInfoInterval = kingpin.Flag("es.clusterinfo.interval",
"Cluster info update interval for the cluster label").
Default("5m").Envar("ES_CLUSTERINFO_INTERVAL").Duration()
Expand Down Expand Up @@ -157,6 +160,10 @@ func main() {
prometheus.MustRegister(collector.NewIndicesMappings(logger, httpClient, esURL))
}

if *esExportIlm {
prometheus.MustRegister(collector.NewIlm(logger, httpClient, esURL))
}

// create a http server
server := &http.Server{}

Expand Down