-
Notifications
You must be signed in to change notification settings - Fork 105
/
metrics.go
239 lines (203 loc) · 7.77 KB
/
metrics.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
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0
package metrics
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/shipwright-io/build/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
// Labels used in Prometheus metrics
const (
BuildStrategyLabel string = "buildstrategy"
NamespaceLabel string = "namespace"
BuildLabel string = "build"
BuildRunLabel string = "buildrun"
)
var (
buildCount *prometheus.CounterVec
buildRunCount *prometheus.CounterVec
buildRunEstablishDuration *prometheus.HistogramVec
buildRunCompletionDuration *prometheus.HistogramVec
buildRunRampUpDuration *prometheus.HistogramVec
taskRunRampUpDuration *prometheus.HistogramVec
taskRunPodRampUpDuration *prometheus.HistogramVec
buildStrategyLabelEnabled = false
namespaceLabelEnabled = false
buildLabelEnabled = false
buildRunLabelEnabled = false
initialized = false
)
// Optional additional metrics endpoint handlers to be configured
var metricsExtraHandlers = map[string]http.HandlerFunc{}
// InitPrometheus initializes the prometheus stuff
func InitPrometheus(config *config.Config) {
if initialized {
return
}
initialized = true
var buildLabels []string
var buildRunLabels []string
if contains(config.Prometheus.EnabledLabels, BuildStrategyLabel) {
buildLabels = append(buildLabels, BuildStrategyLabel)
buildRunLabels = append(buildRunLabels, BuildStrategyLabel)
buildStrategyLabelEnabled = true
}
if contains(config.Prometheus.EnabledLabels, NamespaceLabel) {
buildLabels = append(buildLabels, NamespaceLabel)
buildRunLabels = append(buildRunLabels, NamespaceLabel)
namespaceLabelEnabled = true
}
if contains(config.Prometheus.EnabledLabels, BuildLabel) {
buildLabels = append(buildLabels, BuildLabel)
buildRunLabels = append(buildRunLabels, BuildLabel)
buildLabelEnabled = true
}
if contains(config.Prometheus.EnabledLabels, BuildRunLabel) {
buildRunLabels = append(buildRunLabels, BuildRunLabel)
buildRunLabelEnabled = true
}
buildCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "build_builds_registered_total",
Help: "Number of total registered Builds.",
},
buildLabels)
buildRunCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "build_buildruns_completed_total",
Help: "Number of total completed BuildRuns.",
},
buildRunLabels)
buildRunEstablishDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "build_buildrun_establish_duration_seconds",
Help: "BuildRun establish duration in seconds.",
Buckets: config.Prometheus.BuildRunEstablishDurationBuckets,
},
buildRunLabels)
buildRunCompletionDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "build_buildrun_completion_duration_seconds",
Help: "BuildRun completion duration in seconds.",
Buckets: config.Prometheus.BuildRunCompletionDurationBuckets,
},
buildRunLabels)
buildRunRampUpDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "build_buildrun_rampup_duration_seconds",
Help: "BuildRun ramp-up duration in seconds (time between buildrun creation and taskrun creation).",
Buckets: config.Prometheus.BuildRunRampUpDurationBuckets,
},
buildRunLabels)
taskRunRampUpDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "build_buildrun_taskrun_rampup_duration_seconds",
Help: "BuildRun taskrun ramp-up duration in seconds (time between taskrun creation and taskrun pod creation).",
Buckets: config.Prometheus.BuildRunRampUpDurationBuckets,
},
buildRunLabels)
taskRunPodRampUpDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "build_buildrun_taskrun_pod_rampup_duration_seconds",
Help: "BuildRun taskrun pod ramp-up duration in seconds (time between pod creation and last init container completion).",
Buckets: config.Prometheus.BuildRunRampUpDurationBuckets,
},
buildRunLabels)
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(
buildCount,
buildRunCount,
buildRunEstablishDuration,
buildRunCompletionDuration,
buildRunRampUpDuration,
taskRunRampUpDuration,
taskRunPodRampUpDuration,
)
}
// ExtraHandlers returns a mapping of paths and their respective
// additional HTTP handlers to be configured at the metrics listener
func ExtraHandlers() map[string]http.HandlerFunc {
return metricsExtraHandlers
}
func contains(slice []string, element string) bool {
for _, candidate := range slice {
if candidate == element {
return true
}
}
return false
}
func createBuildLabels(buildStrategy string, namespace string, build string) prometheus.Labels {
labels := prometheus.Labels{}
if buildStrategyLabelEnabled {
labels[BuildStrategyLabel] = buildStrategy
}
if namespaceLabelEnabled {
labels[NamespaceLabel] = namespace
}
if buildLabelEnabled {
labels[BuildLabel] = build
}
return labels
}
func createBuildRunLabels(buildStrategy string, namespace string, build string, buildRun string) prometheus.Labels {
labels := prometheus.Labels{}
if buildStrategyLabelEnabled {
labels[BuildStrategyLabel] = buildStrategy
}
if namespaceLabelEnabled {
labels[NamespaceLabel] = namespace
}
if buildLabelEnabled {
labels[BuildLabel] = build
}
if buildRunLabelEnabled {
labels[BuildRunLabel] = buildRun
}
return labels
}
// BuildCountInc increases a number of the existing build total count
func BuildCountInc(buildStrategy string, namespace string, build string) {
if buildCount != nil {
buildCount.With(createBuildLabels(buildStrategy, namespace, build)).Inc()
}
}
// BuildRunCountInc increases a number of the existing build run total count
func BuildRunCountInc(buildStrategy string, namespace string, build string, buildRun string) {
if buildRunCount != nil {
buildRunCount.With(createBuildRunLabels(buildStrategy, namespace, build, buildRun)).Inc()
}
}
// BuildRunEstablishObserve sets the build run establish time
func BuildRunEstablishObserve(buildStrategy string, namespace string, build string, buildRun string, duration time.Duration) {
if buildRunEstablishDuration != nil {
buildRunEstablishDuration.With(createBuildRunLabels(buildStrategy, namespace, build, buildRun)).Observe(duration.Seconds())
}
}
// BuildRunCompletionObserve sets the build run completion time
func BuildRunCompletionObserve(buildStrategy string, namespace string, build string, buildRun string, duration time.Duration) {
if buildRunCompletionDuration != nil {
buildRunCompletionDuration.With(createBuildRunLabels(buildStrategy, namespace, build, buildRun)).Observe(duration.Seconds())
}
}
// BuildRunRampUpDurationObserve processes the observation of a new buildrun ramp-up duration
func BuildRunRampUpDurationObserve(buildStrategy string, namespace string, build string, buildRun string, duration time.Duration) {
if buildRunRampUpDuration != nil {
buildRunRampUpDuration.With(createBuildRunLabels(buildStrategy, namespace, build, buildRun)).Observe(duration.Seconds())
}
}
// TaskRunRampUpDurationObserve processes the observation of a new taskrun ramp-up duration
func TaskRunRampUpDurationObserve(buildStrategy string, namespace string, build string, buildRun string, duration time.Duration) {
if taskRunRampUpDuration != nil {
taskRunRampUpDuration.With(createBuildRunLabels(buildStrategy, namespace, build, buildRun)).Observe(duration.Seconds())
}
}
// TaskRunPodRampUpDurationObserve processes the observation of a new taskrun pod ramp-up duration
func TaskRunPodRampUpDurationObserve(buildStrategy string, namespace string, build string, buildRun string, duration time.Duration) {
if taskRunPodRampUpDuration != nil {
taskRunPodRampUpDuration.With(createBuildRunLabels(buildStrategy, namespace, build, buildRun)).Observe(duration.Seconds())
}
}