|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "path" |
| 9 | + |
| 10 | + "github.com/go-kit/kit/log" |
| 11 | + "github.com/go-kit/kit/log/level" |
| 12 | + "github.com/prometheus/client_golang/prometheus" |
| 13 | +) |
| 14 | + |
| 15 | +// Labels for remote info metrics |
| 16 | +var defaulRemoteInfoLabels = []string{"remote_cluster"} |
| 17 | +var defaultRemoteInfoLabelValues = func(remote_cluster string) []string { |
| 18 | + return []string{ |
| 19 | + remote_cluster, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +type remoteInfoMetric struct { |
| 24 | + Type prometheus.ValueType |
| 25 | + Desc *prometheus.Desc |
| 26 | + Value func(remoteStats RemoteCluster) float64 |
| 27 | + Labels func(remote_cluster string) []string |
| 28 | +} |
| 29 | + |
| 30 | +// RemoteInfo information struct |
| 31 | +type RemoteInfo struct { |
| 32 | + logger log.Logger |
| 33 | + client *http.Client |
| 34 | + url *url.URL |
| 35 | + |
| 36 | + up prometheus.Gauge |
| 37 | + totalScrapes, jsonParseFailures prometheus.Counter |
| 38 | + |
| 39 | + remoteInfoMetrics []*remoteInfoMetric |
| 40 | +} |
| 41 | + |
| 42 | +// NewClusterSettings defines Cluster Settings Prometheus metrics |
| 43 | +func NewRemoteInfo(logger log.Logger, client *http.Client, url *url.URL) *RemoteInfo { |
| 44 | + |
| 45 | + return &RemoteInfo{ |
| 46 | + logger: logger, |
| 47 | + client: client, |
| 48 | + url: url, |
| 49 | + |
| 50 | + up: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 51 | + Name: prometheus.BuildFQName(namespace, "remote_info_stats", "up"), |
| 52 | + Help: "Was the last scrape of the ElasticSearch remote info endpoint successful.", |
| 53 | + }), |
| 54 | + totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{ |
| 55 | + Name: prometheus.BuildFQName(namespace, "remote_info_stats", "total_scrapes"), |
| 56 | + Help: "Current total ElasticSearch remote info scrapes.", |
| 57 | + }), |
| 58 | + jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{ |
| 59 | + Name: prometheus.BuildFQName(namespace, "remote_info_stats", "json_parse_failures"), |
| 60 | + Help: "Number of errors while parsing JSON.", |
| 61 | + }), |
| 62 | + // Send all of the remote metrics |
| 63 | + remoteInfoMetrics: []*remoteInfoMetric{ |
| 64 | + { |
| 65 | + Type: prometheus.GaugeValue, |
| 66 | + Desc: prometheus.NewDesc( |
| 67 | + prometheus.BuildFQName(namespace, "remote_info", "num_nodes_connected"), |
| 68 | + "Number of nodes connected", defaulRemoteInfoLabels, nil, |
| 69 | + ), |
| 70 | + Value: func(remoteStats RemoteCluster) float64 { |
| 71 | + return float64(remoteStats.NumNodesConnected) |
| 72 | + }, |
| 73 | + Labels: defaultRemoteInfoLabelValues, |
| 74 | + }, |
| 75 | + { |
| 76 | + Type: prometheus.GaugeValue, |
| 77 | + Desc: prometheus.NewDesc( |
| 78 | + prometheus.BuildFQName(namespace, "remote_info", "max_connections_per_cluster"), |
| 79 | + "Max connections per cluster", defaulRemoteInfoLabels, nil, |
| 80 | + ), |
| 81 | + Value: func(remoteStats RemoteCluster) float64 { |
| 82 | + return float64(remoteStats.MaxConnectionsPerCluster) |
| 83 | + }, |
| 84 | + Labels: defaultRemoteInfoLabelValues, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (c *RemoteInfo) fetchAndDecodeRemoteInfoStats() (RemoteInfoResponse, error) { |
| 91 | + var rir RemoteInfoResponse |
| 92 | + |
| 93 | + u := *c.url |
| 94 | + u.Path = path.Join(u.Path, "/_remote/info") |
| 95 | + |
| 96 | + res, err := c.client.Get(u.String()) |
| 97 | + if err != nil { |
| 98 | + return rir, fmt.Errorf("failed to get remote info from %s://%s:%s%s: %s", |
| 99 | + u.Scheme, u.Hostname(), u.Port(), u.Path, err) |
| 100 | + } |
| 101 | + |
| 102 | + defer func() { |
| 103 | + err = res.Body.Close() |
| 104 | + if err != nil { |
| 105 | + _ = level.Warn(c.logger).Log( |
| 106 | + "msg", "failed to close http.Client", |
| 107 | + "err", err, |
| 108 | + ) |
| 109 | + } |
| 110 | + }() |
| 111 | + |
| 112 | + if res.StatusCode != http.StatusOK { |
| 113 | + return rir, fmt.Errorf("HTTP Request failed with code %d", res.StatusCode) |
| 114 | + } |
| 115 | + |
| 116 | + if err := json.NewDecoder(res.Body).Decode(&rir); err != nil { |
| 117 | + c.jsonParseFailures.Inc() |
| 118 | + return rir, err |
| 119 | + } |
| 120 | + return rir, nil |
| 121 | +} |
| 122 | + |
| 123 | +// Collect gets remote info values |
| 124 | +func (ri *RemoteInfo) Collect(ch chan<- prometheus.Metric) { |
| 125 | + ri.totalScrapes.Inc() |
| 126 | + defer func() { |
| 127 | + ch <- ri.up |
| 128 | + ch <- ri.totalScrapes |
| 129 | + ch <- ri.jsonParseFailures |
| 130 | + }() |
| 131 | + |
| 132 | + remoteInfoResp, err := ri.fetchAndDecodeRemoteInfoStats() |
| 133 | + if err != nil { |
| 134 | + ri.up.Set(0) |
| 135 | + _ = level.Warn(ri.logger).Log( |
| 136 | + "msg", "failed to fetch and decode remote info", |
| 137 | + "err", err, |
| 138 | + ) |
| 139 | + return |
| 140 | + } |
| 141 | + ri.totalScrapes.Inc() |
| 142 | + ri.up.Set(1) |
| 143 | + |
| 144 | + // Remote Info |
| 145 | + for remote_cluster, remoteInfo := range remoteInfoResp { |
| 146 | + for _, metric := range ri.remoteInfoMetrics { |
| 147 | + ch <- prometheus.MustNewConstMetric( |
| 148 | + metric.Desc, |
| 149 | + metric.Type, |
| 150 | + metric.Value(remoteInfo), |
| 151 | + metric.Labels(remote_cluster)..., |
| 152 | + ) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// Describe add Indices metrics descriptions |
| 158 | +func (ri *RemoteInfo) Describe(ch chan<- *prometheus.Desc) { |
| 159 | + for _, metric := range ri.remoteInfoMetrics { |
| 160 | + ch <- metric.Desc |
| 161 | + } |
| 162 | + ch <- ri.up.Desc() |
| 163 | + ch <- ri.totalScrapes.Desc() |
| 164 | + ch <- ri.jsonParseFailures.Desc() |
| 165 | +} |
0 commit comments