|
| 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 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + "net/url" |
| 23 | + |
| 24 | + "github.com/alecthomas/kingpin/v2" |
| 25 | + "github.com/go-kit/log" |
| 26 | + "github.com/go-kit/log/level" |
| 27 | + "github.com/prometheus/client_golang/prometheus" |
| 28 | +) |
| 29 | + |
| 30 | +// filterByTask global required because collector interface doesn't expose any way to take |
| 31 | +// constructor args. |
| 32 | +var actionFilter string |
| 33 | + |
| 34 | +var taskActionDesc = prometheus.NewDesc( |
| 35 | + prometheus.BuildFQName(namespace, "task_stats", "action"), |
| 36 | + "Number of tasks of a certain action", |
| 37 | + []string{"action"}, nil) |
| 38 | + |
| 39 | +func init() { |
| 40 | + kingpin.Flag("tasks.actions", |
| 41 | + "Filter on task actions. Used in same way as Task API actions param"). |
| 42 | + Default("indices:*").StringVar(&actionFilter) |
| 43 | + registerCollector("tasks", defaultDisabled, NewTaskCollector) |
| 44 | +} |
| 45 | + |
| 46 | +// Task Information Struct |
| 47 | +type TaskCollector struct { |
| 48 | + logger log.Logger |
| 49 | + hc *http.Client |
| 50 | + u *url.URL |
| 51 | +} |
| 52 | + |
| 53 | +// NewTaskCollector defines Task Prometheus metrics |
| 54 | +func NewTaskCollector(logger log.Logger, u *url.URL, hc *http.Client) (Collector, error) { |
| 55 | + level.Info(logger).Log("msg", "task collector created", |
| 56 | + "actionFilter", actionFilter, |
| 57 | + ) |
| 58 | + |
| 59 | + return &TaskCollector{ |
| 60 | + logger: logger, |
| 61 | + hc: hc, |
| 62 | + u: u, |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (t *TaskCollector) Update(ctx context.Context, ch chan<- prometheus.Metric) error { |
| 67 | + tasks, err := t.fetchTasks(ctx) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to fetch and decode task stats: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + stats := AggregateTasks(tasks) |
| 73 | + for action, count := range stats.CountByAction { |
| 74 | + ch <- prometheus.MustNewConstMetric( |
| 75 | + taskActionDesc, |
| 76 | + prometheus.GaugeValue, |
| 77 | + float64(count), |
| 78 | + action, |
| 79 | + ) |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (t *TaskCollector) fetchTasks(_ context.Context) (tasksResponse, error) { |
| 85 | + u := t.u.ResolveReference(&url.URL{Path: "_tasks"}) |
| 86 | + q := u.Query() |
| 87 | + q.Set("group_by", "none") |
| 88 | + q.Set("actions", actionFilter) |
| 89 | + u.RawQuery = q.Encode() |
| 90 | + |
| 91 | + var tr tasksResponse |
| 92 | + res, err := t.hc.Get(u.String()) |
| 93 | + if err != nil { |
| 94 | + return tr, fmt.Errorf("failed to get data stream stats health from %s://%s:%s%s: %s", |
| 95 | + u.Scheme, u.Hostname(), u.Port(), u.Path, err) |
| 96 | + } |
| 97 | + |
| 98 | + defer func() { |
| 99 | + err = res.Body.Close() |
| 100 | + if err != nil { |
| 101 | + level.Warn(t.logger).Log( |
| 102 | + "msg", "failed to close http.Client", |
| 103 | + "err", err, |
| 104 | + ) |
| 105 | + } |
| 106 | + }() |
| 107 | + |
| 108 | + if res.StatusCode != http.StatusOK { |
| 109 | + return tr, fmt.Errorf("HTTP Request to %v failed with code %d", u.String(), res.StatusCode) |
| 110 | + } |
| 111 | + |
| 112 | + bts, err := io.ReadAll(res.Body) |
| 113 | + if err != nil { |
| 114 | + return tr, err |
| 115 | + } |
| 116 | + |
| 117 | + err = json.Unmarshal(bts, &tr) |
| 118 | + return tr, err |
| 119 | +} |
| 120 | + |
| 121 | +// tasksResponse is a representation of the Task management API. |
| 122 | +type tasksResponse struct { |
| 123 | + Tasks []taskResponse `json:"tasks"` |
| 124 | +} |
| 125 | + |
| 126 | +// taskResponse is a representation of the individual task item returned by task API endpoint. |
| 127 | +// |
| 128 | +// We only parse a very limited amount of this API for use in aggregation. |
| 129 | +type taskResponse struct { |
| 130 | + Action string `json:"action"` |
| 131 | +} |
| 132 | + |
| 133 | +type aggregatedTaskStats struct { |
| 134 | + CountByAction map[string]int64 |
| 135 | +} |
| 136 | + |
| 137 | +func AggregateTasks(t tasksResponse) aggregatedTaskStats { |
| 138 | + actions := map[string]int64{} |
| 139 | + for _, task := range t.Tasks { |
| 140 | + actions[task.Action]++ |
| 141 | + } |
| 142 | + return aggregatedTaskStats{CountByAction: actions} |
| 143 | +} |
0 commit comments