-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathmetrics.go
317 lines (274 loc) · 8.17 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package metrics
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"k8s.io/apimachinery/pkg/labels"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
)
var prCount = stats.Float64("pipelines_as_code_pipelinerun_count",
"number of pipelineruns by pipelines as code",
stats.UnitDimensionless)
var prDurationCount = stats.Float64("pipelines_as_code_pipelinerun_duration_seconds_sum",
"number of seconds all pipelineruns completed in by pipelines as code",
stats.UnitDimensionless)
var runningPRCount = stats.Float64("pipelines_as_code_running_pipelineruns_count",
"number of running pipelineruns by pipelines as code",
stats.UnitDimensionless)
var gitProviderAPIRequestCount = stats.Int64(
"pipelines_as_code_git_provider_api_request_count",
"number of API requests from pipelines as code to git providers",
stats.UnitDimensionless,
)
// Recorder holds keys for metrics.
type Recorder struct {
initialized bool
provider tag.Key
eventType tag.Key
namespace tag.Key
repository tag.Key
status tag.Key
reason tag.Key
ReportingPeriod time.Duration
}
var (
Once sync.Once
R *Recorder
ErrRegistering error
)
// NewRecorder creates a new metrics recorder instance
// to log the PAC PipelineRun related metrics.
func NewRecorder() (*Recorder, error) {
Once.Do(func() {
R = &Recorder{
initialized: true,
// Default to 30s intervals.
ReportingPeriod: 30 * time.Second,
}
provider, errRegistering := tag.NewKey("provider")
if errRegistering != nil {
ErrRegistering = errRegistering
return
}
R.provider = provider
eventType, errRegistering := tag.NewKey("event-type")
if errRegistering != nil {
ErrRegistering = errRegistering
return
}
R.eventType = eventType
namespace, errRegistering := tag.NewKey("namespace")
if errRegistering != nil {
ErrRegistering = errRegistering
return
}
R.namespace = namespace
repository, errRegistering := tag.NewKey("repository")
if errRegistering != nil {
ErrRegistering = errRegistering
return
}
R.repository = repository
status, errRegistering := tag.NewKey("status")
if errRegistering != nil {
ErrRegistering = errRegistering
return
}
R.status = status
reason, errRegistering := tag.NewKey("reason")
if errRegistering != nil {
ErrRegistering = errRegistering
return
}
R.reason = reason
var (
prCountView = &view.View{
Description: prCount.Description(),
Measure: prCount,
Aggregation: view.Count(),
TagKeys: []tag.Key{R.provider, R.eventType, R.namespace, R.repository},
}
prDurationView = &view.View{
Description: prDurationCount.Description(),
Measure: prDurationCount,
Aggregation: view.Sum(),
TagKeys: []tag.Key{R.namespace, R.repository, R.status, R.reason},
}
runningPRView = &view.View{
Description: runningPRCount.Description(),
Measure: runningPRCount,
Aggregation: view.LastValue(),
TagKeys: []tag.Key{R.namespace, R.repository},
}
gitProviderAPIRequestView = &view.View{
Description: gitProviderAPIRequestCount.Description(),
Measure: gitProviderAPIRequestCount,
Aggregation: view.Count(),
TagKeys: []tag.Key{R.provider, R.eventType, R.namespace, R.repository},
}
)
view.Unregister(prCountView, prDurationView, runningPRView, gitProviderAPIRequestView)
errRegistering = view.Register(prCountView, prDurationView, runningPRView, gitProviderAPIRequestView)
if errRegistering != nil {
ErrRegistering = errRegistering
R.initialized = false
return
}
})
return R, ErrRegistering
}
func (r Recorder) assertInitialized() error {
if !r.initialized {
return fmt.Errorf(
"ignoring the metrics recording for pipelineruns, failed to initialize the metrics recorder")
}
return nil
}
// Count logs number of times a pipelinerun is ran for a provider.
func (r *Recorder) Count(provider, event, namespace, repository string) error {
if err := r.assertInitialized(); err != nil {
return err
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.provider, provider),
tag.Insert(r.eventType, event),
tag.Insert(r.namespace, namespace),
tag.Insert(r.repository, repository),
)
if err != nil {
return err
}
metrics.Record(ctx, prCount.M(1))
return nil
}
// CountPRDuration collects duration taken by a pipelinerun in seconds accumulate them in prDurationCount.
func (r *Recorder) CountPRDuration(namespace, repository, status, reason string, duration time.Duration) error {
if err := r.assertInitialized(); err != nil {
return err
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.namespace, namespace),
tag.Insert(r.repository, repository),
tag.Insert(r.status, status),
tag.Insert(r.reason, reason),
)
if err != nil {
return err
}
metrics.Record(ctx, prDurationCount.M(duration.Seconds()))
return nil
}
// RunningPipelineRuns emits the number of running PipelineRuns for a repository and namespace.
func (r *Recorder) RunningPipelineRuns(namespace, repository string, runningPRs float64) error {
if err := r.assertInitialized(); err != nil {
return err
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.namespace, namespace),
tag.Insert(r.repository, repository),
)
if err != nil {
return err
}
metrics.Record(ctx, runningPRCount.M(runningPRs))
return nil
}
func (r *Recorder) EmitRunningPRsMetrics(prl []*tektonv1.PipelineRun) error {
if len(prl) == 0 {
return nil
}
// bifurcate PipelineRuns based on their namespace and repository
runningPRs := map[string]int{}
completedPRsKeys := map[string]struct{}{}
for _, pr := range prl {
// Check if PipelineRun has Repository annotation it means PR is created by PAC.
if repository, ok := pr.GetAnnotations()[keys.Repository]; ok {
key := fmt.Sprintf("%s/%s", pr.GetNamespace(), repository)
// check if PipelineRun is running.
if !pr.IsDone() {
runningPRs[key]++
} else {
// add it in completed, and we don't want completed PipelineRuns count.
completedPRsKeys[key] = struct{}{}
}
}
}
for k, v := range runningPRs {
nsKeys := strings.Split(k, "/")
if err := r.RunningPipelineRuns(nsKeys[0], nsKeys[1], float64(v)); err != nil {
return err
}
}
// report zero for the keys which aren't in runningPRs.
for key := range completedPRsKeys {
// if key isn't there in runningPRs then it should be reported 0
// otherwise it was reported in previous loop.
if _, ok := runningPRs[key]; !ok {
nsKeys := strings.Split(key, "/")
if err := r.RunningPipelineRuns(nsKeys[0], nsKeys[1], 0); err != nil {
return err
}
}
}
return nil
}
// ReportRunningPipelineRuns reports running PipelineRuns on our configured ReportingPeriod
// until the context is cancelled.
func (r *Recorder) ReportRunningPipelineRuns(ctx context.Context, lister listers.PipelineRunLister) {
logger := logging.FromContext(ctx)
for {
delay := time.NewTimer(r.ReportingPeriod)
select {
case <-ctx.Done():
// When the context is cancelled, stop reporting.
if !delay.Stop() {
<-delay.C
}
return
case <-delay.C:
prl, err := lister.List(labels.Everything())
if err != nil {
logger.Warnf("Failed to list PipelineRuns : %v", err)
continue
}
// Every 30s surface a metric for the number of running pipelines.
if err := r.EmitRunningPRsMetrics(prl); err != nil {
logger.Warnf("Failed to log the metrics : %v", err)
}
}
}
}
func (r *Recorder) ReportGitProviderAPIUsage(provider, event, namespace, repository string) error {
if err := r.assertInitialized(); err != nil {
return err
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.provider, provider),
tag.Insert(r.eventType, event),
tag.Insert(r.namespace, namespace),
tag.Insert(r.repository, repository),
)
if err != nil {
return err
}
metrics.Record(ctx, gitProviderAPIRequestCount.M(1))
return nil
}
func ResetRecorder() {
Once = sync.Once{}
R = nil
ErrRegistering = nil
}