-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
295 lines (248 loc) · 7.06 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
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package taskrun
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1alpha1"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"knative.dev/pkg/metrics"
)
var (
trDuration = stats.Float64(
"taskrun_duration_seconds",
"The taskrun's execution time in seconds",
stats.UnitDimensionless)
trDistribution = view.Distribution(10, 30, 60, 300, 900, 1800, 3600, 5400, 10800, 21600, 43200, 86400)
prTRDuration = stats.Float64(
"pipelinerun_taskrun_duration_seconds",
"The pipelinerun's taskrun execution time in seconds",
stats.UnitDimensionless)
prTRLatencyDistribution = view.Distribution(10, 30, 60, 300, 900, 1800, 3600, 5400, 10800, 21600, 43200, 86400)
trCount = stats.Float64("taskrun_count",
"number of taskruns",
stats.UnitDimensionless)
runningTRsCount = stats.Float64("running_taskruns_count",
"Number of taskruns executing currently",
stats.UnitDimensionless)
podLatency = stats.Float64("taskruns_pod_latency",
"scheduling latency for the taskruns pods",
stats.UnitMilliseconds)
)
type Recorder struct {
initialized bool
task tag.Key
taskRun tag.Key
namespace tag.Key
status tag.Key
pipeline tag.Key
pipelineRun tag.Key
pod tag.Key
}
// NewRecorder creates a new metrics recorder instance
// to log the TaskRun related metrics
func NewRecorder() (*Recorder, error) {
r := &Recorder{
initialized: true,
}
task, err := tag.NewKey("task")
if err != nil {
return nil, err
}
r.task = task
taskRun, err := tag.NewKey("taskrun")
if err != nil {
return nil, err
}
r.taskRun = taskRun
namespace, err := tag.NewKey("namespace")
if err != nil {
return nil, err
}
r.namespace = namespace
status, err := tag.NewKey("status")
if err != nil {
return nil, err
}
r.status = status
pipeline, err := tag.NewKey("pipeline")
if err != nil {
return nil, err
}
r.pipeline = pipeline
pipelineRun, err := tag.NewKey("pipelinerun")
if err != nil {
return nil, err
}
r.pipelineRun = pipelineRun
pod, err := tag.NewKey("pod")
if err != nil {
return nil, err
}
r.pod = pod
err = view.Register(
&view.View{
Description: trDuration.Description(),
Measure: trDuration,
Aggregation: trDistribution,
TagKeys: []tag.Key{r.task, r.taskRun, r.namespace, r.status},
},
&view.View{
Description: prTRDuration.Description(),
Measure: prTRDuration,
Aggregation: prTRLatencyDistribution,
TagKeys: []tag.Key{r.task, r.taskRun, r.namespace, r.status, r.pipeline, r.pipelineRun},
},
&view.View{
Description: trCount.Description(),
Measure: trCount,
Aggregation: view.Count(),
TagKeys: []tag.Key{r.status},
},
&view.View{
Description: runningTRsCount.Description(),
Measure: runningTRsCount,
Aggregation: view.LastValue(),
},
&view.View{
Description: podLatency.Description(),
Measure: podLatency,
Aggregation: view.LastValue(),
TagKeys: []tag.Key{r.task, r.taskRun, r.namespace, r.pod},
},
)
if err != nil {
r.initialized = false
return r, err
}
return r, nil
}
// DurationAndCount logs the duration of TaskRun execution and
// count for number of TaskRuns succeed or failed
// returns an error if its failed to log the metrics
func (r *Recorder) DurationAndCount(tr *v1alpha1.TaskRun) error {
if !r.initialized {
return fmt.Errorf("ignoring the metrics recording for %s , failed to initialize the metrics recorder", tr.Name)
}
duration := time.Since(tr.Status.StartTime.Time)
if tr.Status.CompletionTime != nil {
duration = tr.Status.CompletionTime.Sub(tr.Status.StartTime.Time)
}
taskName := "anonymous"
if tr.Spec.TaskRef != nil {
taskName = tr.Spec.TaskRef.Name
}
status := "success"
if tr.Status.Conditions[0].Status == corev1.ConditionFalse {
status = "failed"
}
if ok, pipeline, pipelinerun := tr.IsPartOfPipeline(); ok {
ctx, err := tag.New(
context.Background(),
tag.Insert(r.task, taskName),
tag.Insert(r.taskRun, tr.Name),
tag.Insert(r.namespace, tr.Namespace),
tag.Insert(r.status, status),
tag.Insert(r.pipeline, pipeline),
tag.Insert(r.pipelineRun, pipelinerun),
)
if err != nil {
return err
}
stats.Record(ctx, prTRDuration.M(float64(duration/time.Second)))
metrics.Record(ctx, trCount.M(1))
return nil
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.task, taskName),
tag.Insert(r.taskRun, tr.Name),
tag.Insert(r.namespace, tr.Namespace),
tag.Insert(r.status, status),
)
if err != nil {
return err
}
metrics.Record(ctx, trDuration.M(float64(duration/time.Second)))
metrics.Record(ctx, trCount.M(1))
return nil
}
// RunningTaskRuns logs the number of TaskRuns running right now
// returns an error if its failed to log the metrics
func (r *Recorder) RunningTaskRuns(lister listers.TaskRunLister) error {
if !r.initialized {
return errors.New("ignoring the metrics recording, failed to initialize the metrics recorder")
}
trs, err := lister.List(labels.Everything())
if err != nil {
return err
}
var runningTrs int
for _, pr := range trs {
if !pr.IsDone() {
runningTrs++
}
}
ctx, err := tag.New(
context.Background(),
)
if err != nil {
return err
}
metrics.Record(ctx, runningTRsCount.M(float64(runningTrs)))
return nil
}
// RecordPodLatency logs the duration required to schedule the pod for TaskRun
// returns an error if its failed to log the metrics
func (r *Recorder) RecordPodLatency(pod *corev1.Pod, tr *v1alpha1.TaskRun) error {
if !r.initialized {
return errors.New("ignoring the metrics recording for pod , failed to initialize the metrics recorder")
}
scheduledTime := getScheduledTime(pod)
if scheduledTime.IsZero() {
return errors.New("pod has never got scheduled")
}
latency := scheduledTime.Sub(pod.CreationTimestamp.Time)
taskName := "anonymous"
if tr.Spec.TaskRef != nil {
taskName = tr.Spec.TaskRef.Name
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.task, taskName),
tag.Insert(r.taskRun, tr.Name),
tag.Insert(r.namespace, tr.Namespace),
tag.Insert(r.pod, pod.Name),
)
if err != nil {
return err
}
metrics.Record(ctx, podLatency.M(float64(latency)))
return nil
}
func getScheduledTime(pod *corev1.Pod) metav1.Time {
for _, c := range pod.Status.Conditions {
if c.Type == corev1.PodScheduled {
return c.LastTransitionTime
}
}
return metav1.Time{}
}