1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
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 snapshotMetric struct {
Type prometheus.ValueType
Desc *prometheus.Desc
Value func(snapshotStats SnapshotStatDataResponse) float64
Labels func(repositoryName string, snapshotStats SnapshotStatDataResponse) []string
}
type repositoryMetric struct {
Type prometheus.ValueType
Desc *prometheus.Desc
Value func(snapshotsStats SnapshotStatsResponse) float64
Labels func(repositoryName string) []string
}
var (
defaultSnapshotLabels = []string{"repository", "state", "version"}
defaultSnapshotLabelValues = func(repositoryName string, snapshotStats SnapshotStatDataResponse) []string {
return []string{repositoryName, snapshotStats.State, snapshotStats.Version}
}
defaultSnapshotRepositoryLabels = []string{"repository"}
defaultSnapshotRepositoryLabelValues = func(repositoryName string) []string {
return []string{repositoryName}
}
)
// Snapshots information struct
type Snapshots struct {
logger log.Logger
client *http.Client
url *url.URL
up prometheus.Gauge
totalScrapes, jsonParseFailures prometheus.Counter
snapshotMetrics []*snapshotMetric
repositoryMetrics []*repositoryMetric
}
// NewSnapshots defines Snapshots Prometheus metrics
func NewSnapshots(logger log.Logger, client *http.Client, url *url.URL) *Snapshots {
return &Snapshots{
logger: logger,
client: client,
url: url,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "snapshot_stats", "up"),
Help: "Was the last scrape of the ElasticSearch snapshots endpoint successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "snapshot_stats", "total_scrapes"),
Help: "Current total ElasticSearch snapshots scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, "snapshot_stats", "json_parse_failures"),
Help: "Number of errors while parsing JSON.",
}),
snapshotMetrics: []*snapshotMetric{
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_number_of_indices"),
"Number of indices in the last snapshot",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(len(snapshotStats.Indices))
},
Labels: defaultSnapshotLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_start_time_timestamp"),
"Last snapshot start timestamp",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(snapshotStats.StartTimeInMillis / 1000)
},
Labels: defaultSnapshotLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_end_time_timestamp"),
"Last snapshot end timestamp",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(snapshotStats.EndTimeInMillis / 1000)
},
Labels: defaultSnapshotLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_number_of_failures"),
"Last snapshot number of failures",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(len(snapshotStats.Failures))
},
Labels: defaultSnapshotLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_total_shards"),
"Last snapshot total shards",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(snapshotStats.Shards.Total)
},
Labels: defaultSnapshotLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_failed_shards"),
"Last snapshot failed shards",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(snapshotStats.Shards.Failed)
},
Labels: defaultSnapshotLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "snapshot_successful_shards"),
"Last snapshot successful shards",
defaultSnapshotLabels, nil,
),
Value: func(snapshotStats SnapshotStatDataResponse) float64 {
return float64(snapshotStats.Shards.Successful)
},
Labels: defaultSnapshotLabelValues,
},
},
repositoryMetrics: []*repositoryMetric{
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "number_of_snapshots"),
"Number of snapshots in a repository",
defaultSnapshotRepositoryLabels, nil,
),
Value: func(snapshotsStats SnapshotStatsResponse) float64 {
return float64(len(snapshotsStats.Snapshots))
},
Labels: defaultSnapshotRepositoryLabelValues,
},
{
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "snapshot_stats", "oldest_snapshot_timestamp"),
"Timestamp of the oldest snapshot",
defaultSnapshotRepositoryLabels, nil,
),
Value: func(snapshotsStats SnapshotStatsResponse) float64 {
if len(snapshotsStats.Snapshots) == 0 {
return 0
}
return float64(snapshotsStats.Snapshots[0].StartTimeInMillis / 1000)
},
Labels: defaultSnapshotRepositoryLabelValues,
},
},
}
}
// Describe add Snapshots metrics descriptions
func (s *Snapshots) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range s.snapshotMetrics {
ch <- metric.Desc
}
ch <- s.up.Desc()
ch <- s.totalScrapes.Desc()
ch <- s.jsonParseFailures.Desc()
}
func (s *Snapshots) getAndParseURL(u *url.URL, data interface{}) error {
res, err := s.client.Get(u.String())
if err != nil {
return fmt.Errorf("failed to get 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(s.logger).Log(
"msg", "failed to close http.Client",
"err", err,
)
}
}()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP Request failed with code %d", res.StatusCode)
}
if err := json.NewDecoder(res.Body).Decode(data); err != nil {
s.jsonParseFailures.Inc()
return err
}
return nil
}
func (s *Snapshots) fetchAndDecodeSnapshotsStats() (map[string]SnapshotStatsResponse, error) {
mssr := make(map[string]SnapshotStatsResponse)
u := *s.url
u.Path = path.Join(u.Path, "/_snapshot")
var srr SnapshotRepositoriesResponse
err := s.getAndParseURL(&u, &srr)
if err != nil {
return nil, err
}
for repository := range srr {
u := *s.url
u.Path = path.Join(u.Path, "/_snapshot", repository, "/_all")
var ssr SnapshotStatsResponse
err := s.getAndParseURL(&u, &ssr)
if err != nil {
continue
}
mssr[repository] = ssr
}
return mssr, nil
}
// Collect gets Snapshots metric values
func (s *Snapshots) Collect(ch chan<- prometheus.Metric) {
s.totalScrapes.Inc()
defer func() {
ch <- s.up
ch <- s.totalScrapes
ch <- s.jsonParseFailures
}()
// indices
snapshotsStatsResp, err := s.fetchAndDecodeSnapshotsStats()
if err != nil {
s.up.Set(0)
_ = level.Warn(s.logger).Log(
"msg", "failed to fetch and decode snapshot stats",
"err", err,
)
return
}
s.up.Set(1)
// Snapshots stats
for repositoryName, snapshotStats := range snapshotsStatsResp {
for _, metric := range s.repositoryMetrics {
ch <- prometheus.MustNewConstMetric(
metric.Desc,
metric.Type,
metric.Value(snapshotStats),
metric.Labels(repositoryName)...,
)
}
if len(snapshotStats.Snapshots) == 0 {
continue
}
lastSnapshot := snapshotStats.Snapshots[len(snapshotStats.Snapshots)-1]
for _, metric := range s.snapshotMetrics {
ch <- prometheus.MustNewConstMetric(
metric.Desc,
metric.Type,
metric.Value(lastSnapshot),
metric.Labels(repositoryName, lastSnapshot)...,
)
}
}
}
|