Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавляем новую метрику oneday_elasticsearch_exclude_exists #7

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions collector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,26 @@ func (c *Client) GetSettings(s []string) (map[string]interface{}, error) {

return r, nil
}

func (c *Client) GetClusterSettings() (map[string]interface{}, error) {
c.logger.Debug("Getting cluster settings")
resp, err := c.es.Cluster.GetSettings(
c.es.Cluster.GetSettings.WithIncludeDefaults(true),
c.es.Cluster.GetSettings.WithFilterPath("persistent.cluster.routing.allocation.exclude"),
)
if err != nil {
return nil, fmt.Errorf("error getting response: %s", err)
}
defer resp.Body.Close()

if resp.IsError() {
return nil, fmt.Errorf("request failed: %v", resp.String())
}

var r map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return nil, err
}

return r, nil
}
48 changes: 48 additions & 0 deletions collector/cluster-settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package collector

import (
"fmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)

type ClusterSettingsCollector struct {
client *Client
logger *logrus.Logger

excludeExists *prometheus.Desc
}

func NewClusterSettingsCollector(logger *logrus.Logger, client *Client, labels, labels_group []string, datepattern string,
constLabels prometheus.Labels) *ClusterSettingsCollector {

fmt.Printf("labels: %v\n\n\nconstLabels: %v\n", labels, constLabels)

return &ClusterSettingsCollector{
client: client,
logger: logger,
excludeExists: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "exclude", "exists"),
"Exclude exists in cluster settings", labels, constLabels,
),
}
}

func (c *ClusterSettingsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.excludeExists
}

func (c *ClusterSettingsCollector) Collect(ch chan<- prometheus.Metric) {
settings, err := c.client.GetClusterSettings()
if err != nil {
c.logger.Fatalf("error getting indices settings: %v", err)
}

if len(settings) == 0 {
ch <- prometheus.MustNewConstMetric(c.excludeExists, prometheus.CounterValue, 0, "persistent")
} else {
ch <- prometheus.MustNewConstMetric(c.excludeExists, prometheus.CounterValue, 1, "persistent")
}

}
6 changes: 6 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
labels = []string{"index", "index_group"}
labels_group = []string{"index_group"}
slabels = []string{"repository"}
clabels = []string{"section"}
)

func NewCollector(logger *logrus.Logger, address, project string, repo string, datepattern string, tlsClientConfig *tls.Config) error {
Expand Down Expand Up @@ -54,6 +55,11 @@ func NewCollector(logger *logrus.Logger, address, project string, repo string, d
return fmt.Errorf("error registering indices settings collector: %v", err)
}

err = prometheus.Register(NewClusterSettingsCollector(logger, client, clabels, labels_group, datepattern, constLabels))
if err != nil {
return fmt.Errorf("error registering indices settings collector: %v", err)
}

if repo != "" {
err = prometheus.Register(NewSnapshotCollector(logger, client, repo, slabels, constLabels))
if err != nil {
Expand Down
Loading