@@ -19,135 +19,62 @@ package tektonchain
1919import (
2020 "context"
2121 "fmt"
22+ "sync"
2223 "time"
2324
2425 "github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
25- "go.opencensus .io/stats "
26- "go.opencensus .io/stats/view "
27- "go.opencensus .io/tag "
26+ "go.opentelemetry .io/otel "
27+ "go.opentelemetry .io/otel/attribute "
28+ "go.opentelemetry .io/otel/metric "
2829 "go.uber.org/zap"
29- "knative.dev/pkg/metrics"
3030)
3131
3232var (
33- rReconcileCount = stats . Float64 ( "chains_reconciled" ,
34- "metrics of chains reconciled with labels" ,
35- stats . UnitDimensionless )
33+ rReconcileCount metric. Int64Counter
34+ once sync. Once
35+ errInitMetrics error
3636)
3737
38+ func initMetrics () error {
39+ meter := otel .Meter ("github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektonchain" )
40+ var err error
41+ rReconcileCount , err = meter .Int64Counter (
42+ "tekton_operator_lifecycle_chains_reconciled_total" ,
43+ metric .WithDescription ("metrics of chains reconciled with labels" ),
44+ )
45+ if err != nil {
46+ return fmt .Errorf ("failed to create chains_reconciled counter: %w" , err )
47+ }
48+ return nil
49+ }
50+
3851// Recorder holds keys for Tekton metrics
3952type Recorder struct {
40- initialized bool
41- version tag.Key
42- taskrunFormat tag.Key
43- taskrunStorage tag.Key
44- taskrunSigner tag.Key
45- pipelinerunFormat tag.Key
46- pipelinerunStorage tag.Key
47- pipelinerunSigner tag.Key
48- ociFormat tag.Key
49- ociStorage tag.Key
50- ociSigner tag.Key
51-
53+ initialized bool
5254 ReportingPeriod time.Duration
5355}
5456
5557// NewRecorder creates a new metrics recorder instance
56- // to log the PipelineRun related metrics
5758func NewRecorder () (* Recorder , error ) {
58- r := & Recorder {
59- initialized : true ,
60-
61- // Default to 30s intervals.
62- ReportingPeriod : 30 * time .Second ,
63- }
64-
65- version , err := tag .NewKey ("version" )
66- if err != nil {
67- return nil , err
68- }
69- r .version = version
70-
71- taskrunFormat , err := tag .NewKey ("taskrun_format" )
72- if err != nil {
73- return nil , err
74- }
75- r .taskrunFormat = taskrunFormat
76-
77- taskrunStorage , err := tag .NewKey ("taskrun_storage" )
78- if err != nil {
79- return nil , err
80- }
81- r .taskrunStorage = taskrunStorage
82-
83- taskrunSigner , err := tag .NewKey ("taskrun_signer" )
84- if err != nil {
85- return nil , err
86- }
87- r .taskrunSigner = taskrunSigner
88-
89- pipelinerunFormat , err := tag .NewKey ("pipelinerun_format" )
90- if err != nil {
91- return nil , err
59+ once .Do (func () {
60+ errInitMetrics = initMetrics ()
61+ })
62+ if errInitMetrics != nil {
63+ return nil , errInitMetrics
9264 }
93- r .pipelinerunFormat = pipelinerunFormat
9465
95- pipelinerunStorage , err := tag .NewKey ("pipelinerun_storage" )
96- if err != nil {
97- return nil , err
98- }
99- r .pipelinerunStorage = pipelinerunStorage
100-
101- pipelinerunSigner , err := tag .NewKey ("pipelinerun_signer" )
102- if err != nil {
103- return nil , err
104- }
105- r .pipelinerunSigner = pipelinerunSigner
106-
107- ociFormat , err := tag .NewKey ("oci_format" )
108- if err != nil {
109- return nil , err
110- }
111- r .ociFormat = ociFormat
112-
113- ociStorage , err := tag .NewKey ("oci_storage" )
114- if err != nil {
115- return nil , err
116- }
117- r .ociStorage = ociStorage
118-
119- ociSigner , err := tag .NewKey ("oci_signer" )
120- if err != nil {
121- return nil , err
122- }
123- r .ociSigner = ociSigner
124-
125- err = view .Register (
126- & view.View {
127- Description : rReconcileCount .Description (),
128- Measure : rReconcileCount ,
129- Aggregation : view .Count (),
130- TagKeys : []tag.Key {r .version ,
131- r .taskrunFormat , r .taskrunStorage , r .taskrunSigner ,
132- r .pipelinerunFormat , r .pipelinerunStorage , r .pipelinerunSigner ,
133- r .ociFormat , r .ociStorage , r .ociSigner },
134- },
135- )
136-
137- if err != nil {
138- r .initialized = false
139- return r , err
66+ r := & Recorder {
67+ initialized : true ,
68+ ReportingPeriod : 30 * time .Second ,
14069 }
141-
14270 return r , nil
14371}
14472
145- // Logs when chains is reconciled with version and
146- // config labels.
73+ // Count logs when chains is reconciled with version and config labels.
14774func (r * Recorder ) Count (version string , spec v1alpha1.TektonChainSpec ) error {
14875 if ! r .initialized {
14976 return fmt .Errorf (
150- "ignoring the metrics recording for pipelinee failed to initialize the metrics recorder" )
77+ "failed to initialize metrics recorder for chains " )
15178 }
15279
15380 var taskrunStorage , pipelinerunStorage , ociStorage string
@@ -161,25 +88,20 @@ func (r *Recorder) Count(version string, spec v1alpha1.TektonChainSpec) error {
16188 ociStorage = * spec .ArtifactsOCIStorage
16289 }
16390
164- ctx , err := tag .New (
165- context .Background (),
166- tag .Insert (r .version , version ),
167- tag .Insert (r .taskrunFormat , spec .ArtifactsTaskRunFormat ),
168- tag .Insert (r .taskrunStorage , taskrunStorage ),
169- tag .Insert (r .taskrunSigner , spec .ArtifactsTaskRunSigner ),
170- tag .Insert (r .pipelinerunFormat , spec .ArtifactsPipelineRunFormat ),
171- tag .Insert (r .pipelinerunStorage , pipelinerunStorage ),
172- tag .Insert (r .pipelinerunSigner , spec .ArtifactsPipelineRunSigner ),
173- tag .Insert (r .ociFormat , spec .ArtifactsOCIFormat ),
174- tag .Insert (r .ociStorage , ociStorage ),
175- tag .Insert (r .ociSigner , spec .ArtifactsOCISigner ),
91+ rReconcileCount .Add (context .Background (), 1 ,
92+ metric .WithAttributes (
93+ attribute .String ("version" , version ),
94+ attribute .String ("taskrun_format" , spec .ArtifactsTaskRunFormat ),
95+ attribute .String ("taskrun_storage" , taskrunStorage ),
96+ attribute .String ("taskrun_signer" , spec .ArtifactsTaskRunSigner ),
97+ attribute .String ("pipelinerun_format" , spec .ArtifactsPipelineRunFormat ),
98+ attribute .String ("pipelinerun_storage" , pipelinerunStorage ),
99+ attribute .String ("pipelinerun_signer" , spec .ArtifactsPipelineRunSigner ),
100+ attribute .String ("oci_format" , spec .ArtifactsOCIFormat ),
101+ attribute .String ("oci_storage" , ociStorage ),
102+ attribute .String ("oci_signer" , spec .ArtifactsOCISigner ),
103+ ),
176104 )
177-
178- if err != nil {
179- return err
180- }
181-
182- metrics .Record (ctx , rReconcileCount .M (1 ))
183105 return nil
184106}
185107
@@ -192,7 +114,7 @@ func (m *Recorder) LogMetricsWithSpec(version string, spec v1alpha1.TektonChainS
192114
193115func (m * Recorder ) LogMetrics (status , version string , logger * zap.SugaredLogger ) {
194116 var newSpec v1alpha1.TektonChainSpec
195- err := m .Count (status , newSpec )
117+ err := m .Count (version , newSpec )
196118 if err != nil {
197119 logger .Warnf ("%v: Failed to log the metrics : %v" , resourceKind , err )
198120 }
0 commit comments