-
Notifications
You must be signed in to change notification settings - Fork 186
/
metrics.go
112 lines (91 loc) · 2.43 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
/*
Copyright 2021 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 tektonpipeline
import (
"context"
"fmt"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.uber.org/zap"
"knative.dev/pkg/metrics"
)
var (
pReconcileCount = stats.Float64("pipeline_reconcile_count",
"number of pipeline install",
stats.UnitDimensionless)
)
// Recorder holds keys for Tekton metrics
type Recorder struct {
initialized bool
status tag.Key
version tag.Key
ReportingPeriod time.Duration
}
// NewRecorder creates a new metrics recorder instance
// to log the PipelineRun related metrics
func NewRecorder() (*Recorder, error) {
r := &Recorder{
initialized: true,
// Default to 30s intervals.
ReportingPeriod: 30 * time.Second,
}
status, err := tag.NewKey("status")
if err != nil {
return nil, err
}
r.status = status
version, err := tag.NewKey("version")
if err != nil {
return nil, err
}
r.version = version
err = view.Register(
&view.View{
Description: pReconcileCount.Description(),
Measure: pReconcileCount,
Aggregation: view.Count(),
TagKeys: []tag.Key{r.status, r.version},
},
)
if err != nil {
r.initialized = false
return r, err
}
return r, nil
}
// Count logs number of times a component (pipeline/trigger atm)
// has been installed or failed to install.
func (r *Recorder) Count(status, version string) error {
if !r.initialized {
return fmt.Errorf(
"ignoring the metrics recording for pipelinee failed to initialize the metrics recorder")
}
ctx, err := tag.New(
context.Background(),
tag.Insert(r.status, status),
tag.Insert(r.version, version),
)
if err != nil {
return err
}
metrics.Record(ctx, pReconcileCount.M(1))
return nil
}
func (m *Recorder) LogMetrics(status, version string, logger *zap.SugaredLogger) {
err := m.Count(status, version)
if err != nil {
logger.Warnf("%v: Failed to log the metrics : %v", resourceKind, err)
}
}