forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
105 lines (90 loc) · 2.8 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
package metrics
import (
"fmt"
"strconv"
"github.com/prometheus/client_golang/prometheus"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
)
type RealTimeMetric struct {
Func func() float64
}
func ConstructOrUpdateMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
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 {
gaugeOpts := prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: metricSpec.Name,
Help: metricSpec.Help,
ConstLabels: metricSpec.GetMetricLabels(),
}
return prometheus.NewGaugeFunc(gaugeOpts, valueFunc)
}
func constructOrUpdateCounterMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
counterOpts := prometheus.CounterOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: metricSpec.Name,
Help: metricSpec.Help,
ConstLabels: metricSpec.GetMetricLabels(),
}
val, err := strconv.ParseFloat(metricSpec.Counter.Value, 64)
if err != nil {
return nil, err
}
if metric == nil {
metric = prometheus.NewCounter(counterOpts)
}
counter := metric.(prometheus.Counter)
counter.Add(val)
return counter, nil
}
func constructOrUpdateGaugeMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
gaugeOpts := prometheus.GaugeOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: metricSpec.Name,
Help: metricSpec.Help,
ConstLabels: metricSpec.GetMetricLabels(),
}
val, err := strconv.ParseFloat(metricSpec.Gauge.Value, 64)
if err != nil {
return nil, err
}
if metric == nil {
metric = prometheus.NewGauge(gaugeOpts)
}
gauge := metric.(prometheus.Gauge)
gauge.Set(val)
return gauge, nil
}
func constructOrUpdateHistogramMetric(metric prometheus.Metric, metricSpec *wfv1.Prometheus) (prometheus.Metric, error) {
histOpts := prometheus.HistogramOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: metricSpec.Name,
Help: metricSpec.Help,
ConstLabels: metricSpec.GetMetricLabels(),
Buckets: metricSpec.Histogram.Buckets,
}
val, err := strconv.ParseFloat(metricSpec.Histogram.Value, 64)
if err != nil {
return nil, err
}
if metric == nil {
metric = prometheus.NewHistogram(histOpts)
}
hist := metric.(prometheus.Histogram)
hist.Observe(val)
return hist, nil
}