-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
util.go
274 lines (243 loc) · 8.73 KB
/
util.go
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
package metrics
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
v1 "k8s.io/api/core/v1"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
var (
invalidMetricNameError = "metric name is invalid: names may only contain alphanumeric characters, '_', or ':'"
invalidMetricLabelrror = "metric label '%s' is invalid: keys may only contain alphanumeric characters, '_', or ':'"
descRegex = regexp.MustCompile(fmt.Sprintf(`Desc{fqName: "%s_%s_(.+?)", help: "(.+?)", constLabels: {`, argoNamespace, workflowsSubsystem))
)
type RealTimeMetric struct {
Func func() float64
}
func ConstructOrUpdateMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
if !IsValidMetricName(metricSpec.Name) {
return nil, fmt.Errorf(invalidMetricNameError)
}
switch metricSpec.GetMetricType() {
case wfv1.MetricTypeGauge:
return constructOrUpdateGaugeMetric(metric, metricSpec)
case wfv1.MetricTypeHistogram:
return constructOrUpdateHistogramMetric(metric, metricSpec)
case wfv1.MetricTypeCounter:
return constructOrUpdateCounterMetric(metric, metricSpec)
default:
return nil, fmt.Errorf("invalid metric spec")
}
}
func ConstructRealTimeGaugeMetric(metricSpec *wfv1.Prometheus, valueFunc func() float64) (prometheus.Metric, error) {
if !IsValidMetricName(metricSpec.Name) {
return nil, fmt.Errorf(invalidMetricNameError)
}
labels := metricSpec.GetMetricLabels()
if err := ValidateMetricLabels(labels); err != nil {
return nil, err
}
gaugeOpts := prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: metricSpec.Name,
Help: metricSpec.Help,
ConstLabels: labels,
}
return prometheus.NewGaugeFunc(gaugeOpts, valueFunc), nil
}
func constructOrUpdateCounterMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
if metric == nil {
labels := metricSpec.GetMetricLabels()
if err := ValidateMetricLabels(labels); err != nil {
return nil, err
}
metric = newCounter(metricSpec.Name, metricSpec.Help, labels)
}
val, err := strconv.ParseFloat(metricSpec.Counter.Value, 64)
if err != nil {
return nil, err
}
counter := metric.(prometheus.Counter)
counter.Add(val)
return counter, nil
}
func constructOrUpdateGaugeMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
if metric == nil {
labels := metricSpec.GetMetricLabels()
if err := ValidateMetricLabels(labels); err != nil {
return nil, err
}
metric = newGauge(metricSpec.Name, metricSpec.Help, labels)
}
val, err := strconv.ParseFloat(metricSpec.Gauge.Value, 64)
if err != nil {
return nil, err
}
gauge := metric.(prometheus.Gauge)
gauge.Set(val)
return gauge, nil
}
func constructOrUpdateHistogramMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
if metric == nil {
labels := metricSpec.GetMetricLabels()
if err := ValidateMetricLabels(labels); err != nil {
return nil, err
}
metric = newHistogram(metricSpec.Name, metricSpec.Help, labels, metricSpec.Histogram.GetBuckets())
}
val, err := strconv.ParseFloat(metricSpec.Histogram.Value, 64)
if err != nil {
return nil, err
}
hist := metric.(prometheus.Histogram)
hist.Observe(val)
return hist, nil
}
func newCounter(name, help string, labels map[string]string) prometheus.Counter {
counterOpts := prometheus.CounterOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: name,
Help: help,
ConstLabels: labels,
}
m := prometheus.NewCounter(counterOpts)
mustBeRecoverable(name, help, m)
return m
}
func newGauge(name, help string, labels map[string]string) prometheus.Gauge {
gaugeOpts := prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: name,
Help: help,
ConstLabels: labels,
}
m := prometheus.NewGauge(gaugeOpts)
mustBeRecoverable(name, help, m)
return m
}
func newHistogram(name, help string, labels map[string]string, buckets []float64) prometheus.Histogram {
histOpts := prometheus.HistogramOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: name,
Help: help,
ConstLabels: labels,
Buckets: buckets,
}
m := prometheus.NewHistogram(histOpts)
mustBeRecoverable(name, help, m)
return m
}
func getWorkflowPhaseGauges() map[wfv1.NodePhase]prometheus.Gauge {
getOptsByPhase := func(phase wfv1.NodePhase) prometheus.GaugeOpts {
return prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: "count",
Help: "Number of Workflows currently accessible by the controller by status (refreshed every 15s)",
ConstLabels: map[string]string{"status": string(phase)},
}
}
return map[wfv1.NodePhase]prometheus.Gauge{
wfv1.NodePending: prometheus.NewGauge(getOptsByPhase(wfv1.NodePending)),
wfv1.NodeRunning: prometheus.NewGauge(getOptsByPhase(wfv1.NodeRunning)),
wfv1.NodeSucceeded: prometheus.NewGauge(getOptsByPhase(wfv1.NodeSucceeded)),
wfv1.NodeFailed: prometheus.NewGauge(getOptsByPhase(wfv1.NodeFailed)),
wfv1.NodeError: prometheus.NewGauge(getOptsByPhase(wfv1.NodeError)),
}
}
func getPodPhaseGauges() map[v1.PodPhase]prometheus.Gauge {
getOptsByPhase := func(phase v1.PodPhase) prometheus.GaugeOpts {
return prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: "pods_count",
Help: "Number of Pods from Workflows currently accessible by the controller by status (refreshed every 15s)",
ConstLabels: map[string]string{"status": string(phase)},
}
}
return map[v1.PodPhase]prometheus.Gauge{
v1.PodPending: prometheus.NewGauge(getOptsByPhase(v1.PodPending)),
v1.PodRunning: prometheus.NewGauge(getOptsByPhase(v1.PodRunning)),
// v1.PodSucceeded: prometheus.NewGauge(getOptsByPhase(v1.PodSucceeded)),
// v1.PodFailed: prometheus.NewGauge(getOptsByPhase(v1.PodFailed)),
}
}
func getErrorCounters() map[ErrorCause]prometheus.Counter {
getOptsByPahse := func(phase ErrorCause) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: "error_count",
Help: "Number of errors encountered by the controller by cause",
ConstLabels: map[string]string{"cause": string(phase)},
}
}
return map[ErrorCause]prometheus.Counter{
ErrorCauseOperationPanic: prometheus.NewCounter(getOptsByPahse(ErrorCauseOperationPanic)),
ErrorCauseCronWorkflowSubmissionError: prometheus.NewCounter(getOptsByPahse(ErrorCauseCronWorkflowSubmissionError)),
ErrorCauseCronWorkflowSpecError: prometheus.NewCounter(getOptsByPahse(ErrorCauseCronWorkflowSpecError)),
}
}
func getWorkersBusy(name string) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: "workers_busy_count",
Help: "Number of workers currently busy",
ConstLabels: map[string]string{"worker_type": name},
})
}
func IsValidMetricName(name string) bool {
return model.IsValidMetricName(model.LabelValue(name))
}
func ValidateMetricValues(metric *wfv1.Prometheus) error {
if metric.Gauge != nil {
if metric.Gauge.Value == "" {
return errors.New("missing gauge.value")
}
if metric.Gauge.Realtime != nil && *metric.Gauge.Realtime {
if strings.Contains(metric.Gauge.Value, "resourcesDuration.") {
return errors.New("'resourcesDuration.*' metrics cannot be used in real-time")
}
}
}
if metric.Counter != nil && metric.Counter.Value == "" {
return errors.New("missing counter.value")
}
if metric.Histogram != nil && metric.Histogram.Value == "" {
return errors.New("missing histogram.value")
}
return nil
}
func ValidateMetricLabels(metrics map[string]string) error {
for name := range metrics {
if !IsValidMetricName(name) {
return fmt.Errorf(invalidMetricLabelrror, name)
}
}
return nil
}
func mustBeRecoverable(name, help string, metric prometheus.Metric) {
recoveredName, recoveredHelp := recoverMetricNameAndHelpFromDesc(metric.Desc().String())
if name != recoveredName {
panic(fmt.Sprintf("unable to recover metric name from desc provided by prometheus: expected '%s' got '%s'", name, recoveredName))
}
if help != recoveredHelp {
panic(fmt.Sprintf("unable to recover metric help from desc provided by prometheus: expected '%s' got '%s'", help, recoveredHelp))
}
}
func recoverMetricNameAndHelpFromDesc(desc string) (string, string) {
finds := descRegex.FindStringSubmatch(desc)
if len(finds) != 3 {
panic(fmt.Sprintf("malformed desc provided by prometheus: '%s' parsed to %v", desc, finds))
}
return finds[1], strings.ReplaceAll(finds[2], `\"`, `"`)
}