|
| 1 | +// Copyright 2023 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 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "net/http" |
| 21 | + "net/url" |
| 22 | + "path" |
| 23 | + |
| 24 | + "github.com/go-kit/log" |
| 25 | + "github.com/go-kit/log/level" |
| 26 | + "github.com/prometheus/client_golang/prometheus" |
| 27 | +) |
| 28 | + |
| 29 | +type taskByAction struct { |
| 30 | + Type prometheus.ValueType |
| 31 | + Desc *prometheus.Desc |
| 32 | + Value func(action string, count int64) float64 |
| 33 | + Labels func(action string, count int64) []string |
| 34 | +} |
| 35 | + |
| 36 | +var ( |
| 37 | + taskLabels = []string{"cluster", "action"} |
| 38 | +) |
| 39 | + |
| 40 | +// Task Information Struct |
| 41 | +type Task struct { |
| 42 | + logger log.Logger |
| 43 | + client *http.Client |
| 44 | + url *url.URL |
| 45 | + actions string |
| 46 | + |
| 47 | + up prometheus.Gauge |
| 48 | + totalScrapes, jsonParseFailures prometheus.Counter |
| 49 | + |
| 50 | + byActionMetrics []*taskByAction |
| 51 | +} |
| 52 | + |
| 53 | +// NewTask defines Task Prometheus metrics |
| 54 | +func NewTask(logger log.Logger, client *http.Client, url *url.URL, actions string) *Task { |
| 55 | + return &Task{ |
| 56 | + logger: logger, |
| 57 | + client: client, |
| 58 | + url: url, |
| 59 | + actions: actions, |
| 60 | + |
| 61 | + up: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 62 | + Name: prometheus.BuildFQName(namespace, "task_stats", "up"), |
| 63 | + Help: "Was the last scrape of the ElasticSearch Task endpoint successful.", |
| 64 | + }), |
| 65 | + totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{ |
| 66 | + Name: prometheus.BuildFQName(namespace, "task_stats", "total_scrapes"), |
| 67 | + Help: "Current total Elasticsearch snapshots scrapes.", |
| 68 | + }), |
| 69 | + jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{ |
| 70 | + Name: prometheus.BuildFQName(namespace, "task_stats", "json_parse_failures"), |
| 71 | + Help: "Number of errors while parsing JSON.", |
| 72 | + }), |
| 73 | + byActionMetrics: []*taskByAction{ |
| 74 | + { |
| 75 | + Type: prometheus.GaugeValue, |
| 76 | + Desc: prometheus.NewDesc( |
| 77 | + prometheus.BuildFQName(namespace, "task_stats", "action_total"), |
| 78 | + "Number of tasks of a certain action", |
| 79 | + []string{"action"}, nil, |
| 80 | + ), |
| 81 | + Value: func(action string, count int64) float64 { |
| 82 | + return float64(count) |
| 83 | + }, |
| 84 | + Labels: func(action string, count int64) []string { |
| 85 | + return []string{action} |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Describe adds Task metrics descriptions |
| 93 | +func (t *Task) Describe(ch chan<- *prometheus.Desc) { |
| 94 | + for _, metric := range t.byActionMetrics { |
| 95 | + ch <- metric.Desc |
| 96 | + } |
| 97 | + |
| 98 | + ch <- t.up.Desc() |
| 99 | + ch <- t.totalScrapes.Desc() |
| 100 | + ch <- t.jsonParseFailures.Desc() |
| 101 | +} |
| 102 | + |
| 103 | +func (t *Task) fetchAndDecodeAndAggregateTaskStats() (*AggregatedTaskStats, error) { |
| 104 | + u := *t.url |
| 105 | + u.Path = path.Join(u.Path, "/_tasks") |
| 106 | + u.RawQuery = "group_by=none&actions=" + t.actions |
| 107 | + res, err := t.client.Get(u.String()) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("failed to get data stream stats health from %s://%s:%s%s: %s", |
| 110 | + u.Scheme, u.Hostname(), u.Port(), u.Path, err) |
| 111 | + } |
| 112 | + |
| 113 | + defer func() { |
| 114 | + err = res.Body.Close() |
| 115 | + if err != nil { |
| 116 | + level.Warn(t.logger).Log( |
| 117 | + "msg", "failed to close http.Client", |
| 118 | + "err", err, |
| 119 | + ) |
| 120 | + } |
| 121 | + }() |
| 122 | + |
| 123 | + if res.StatusCode != http.StatusOK { |
| 124 | + return nil, fmt.Errorf("HTTP Request to %v failed with code %d", u.String(), res.StatusCode) |
| 125 | + } |
| 126 | + |
| 127 | + bts, err := io.ReadAll(res.Body) |
| 128 | + if err != nil { |
| 129 | + t.jsonParseFailures.Inc() |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + var tr TasksResponse |
| 134 | + if err := json.Unmarshal(bts, &tr); err != nil { |
| 135 | + t.jsonParseFailures.Inc() |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + stats := AggregateTasks(tr) |
| 140 | + return stats, nil |
| 141 | +} |
| 142 | + |
| 143 | +// Collect gets Task metric values |
| 144 | +func (ds *Task) Collect(ch chan<- prometheus.Metric) { |
| 145 | + ds.totalScrapes.Inc() |
| 146 | + defer func() { |
| 147 | + ch <- ds.up |
| 148 | + ch <- ds.totalScrapes |
| 149 | + ch <- ds.jsonParseFailures |
| 150 | + }() |
| 151 | + |
| 152 | + stats, err := ds.fetchAndDecodeAndAggregateTaskStats() |
| 153 | + if err != nil { |
| 154 | + ds.up.Set(0) |
| 155 | + level.Warn(ds.logger).Log( |
| 156 | + "msg", "failed to fetch and decode task stats", |
| 157 | + "err", err, |
| 158 | + ) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + for action, count := range stats.CountByAction { |
| 163 | + for _, metric := range ds.byActionMetrics { |
| 164 | + ch <- prometheus.MustNewConstMetric( |
| 165 | + metric.Desc, |
| 166 | + metric.Type, |
| 167 | + metric.Value(action, count), |
| 168 | + metric.Labels(action, count)..., |
| 169 | + ) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + ds.up.Set(1) |
| 174 | +} |
0 commit comments