|
| 1 | +/* |
| 2 | +Copyright 2024 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pipelinerunmetrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/tektoncd/chains/pkg/chains" |
| 24 | + "go.opencensus.io/stats" |
| 25 | + "go.opencensus.io/stats/view" |
| 26 | + "knative.dev/pkg/logging" |
| 27 | + "knative.dev/pkg/metrics" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + sgCount = stats.Float64(chains.PipelineRunSignedName, |
| 32 | + chains.PipelineRunSignedDesc, |
| 33 | + stats.UnitDimensionless) |
| 34 | + |
| 35 | + sgCountView *view.View |
| 36 | + |
| 37 | + plCount = stats.Float64(chains.PipelineRunUploadedName, |
| 38 | + chains.PipelineRunUploadedDesc, |
| 39 | + stats.UnitDimensionless) |
| 40 | + |
| 41 | + plCountView *view.View |
| 42 | + |
| 43 | + stCount = stats.Float64(chains.PipelineRunStoredName, |
| 44 | + chains.PipelineRunStoredDesc, |
| 45 | + stats.UnitDimensionless) |
| 46 | + |
| 47 | + stCountView *view.View |
| 48 | + |
| 49 | + mrCount = stats.Float64(chains.PipelineRunMarkedName, |
| 50 | + chains.PipelineRunMarkedDesc, |
| 51 | + stats.UnitDimensionless) |
| 52 | + |
| 53 | + mrCountView *view.View |
| 54 | +) |
| 55 | + |
| 56 | +// Recorder holds keys for Tekton metrics |
| 57 | +type Recorder struct { |
| 58 | + initialized bool |
| 59 | +} |
| 60 | + |
| 61 | +// We cannot register the view multiple times, so NewRecorder lazily |
| 62 | +// initializes this singleton and returns the same recorder across any |
| 63 | +// subsequent invocations. |
| 64 | +var ( |
| 65 | + once sync.Once |
| 66 | + r *Recorder |
| 67 | +) |
| 68 | + |
| 69 | +// NewRecorder creates a new metrics recorder instance |
| 70 | +// to log the PipelineRun related metrics |
| 71 | +func NewRecorder(ctx context.Context) (*Recorder, error) { |
| 72 | + var errRegistering error |
| 73 | + logger := logging.FromContext(ctx) |
| 74 | + once.Do(func() { |
| 75 | + r = &Recorder{ |
| 76 | + initialized: true, |
| 77 | + } |
| 78 | + errRegistering = viewRegister() |
| 79 | + if errRegistering != nil { |
| 80 | + r.initialized = false |
| 81 | + logger.Errorf("View Register Failed ", r.initialized) |
| 82 | + return |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + return r, errRegistering |
| 87 | +} |
| 88 | + |
| 89 | +func viewRegister() error { |
| 90 | + sgCountView = &view.View{ |
| 91 | + Description: sgCount.Description(), |
| 92 | + Measure: sgCount, |
| 93 | + Aggregation: view.Count(), |
| 94 | + } |
| 95 | + |
| 96 | + plCountView = &view.View{ |
| 97 | + Description: plCount.Description(), |
| 98 | + Measure: plCount, |
| 99 | + Aggregation: view.Count(), |
| 100 | + } |
| 101 | + |
| 102 | + stCountView = &view.View{ |
| 103 | + Description: stCount.Description(), |
| 104 | + Measure: stCount, |
| 105 | + Aggregation: view.Count(), |
| 106 | + } |
| 107 | + |
| 108 | + mrCountView = &view.View{ |
| 109 | + Description: mrCount.Description(), |
| 110 | + Measure: mrCount, |
| 111 | + Aggregation: view.Count(), |
| 112 | + } |
| 113 | + return view.Register( |
| 114 | + sgCountView, |
| 115 | + plCountView, |
| 116 | + stCountView, |
| 117 | + mrCountView, |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +func (r *Recorder) RecordCountMetrics(ctx context.Context, metricType string) { |
| 122 | + logger := logging.FromContext(ctx) |
| 123 | + if !r.initialized { |
| 124 | + logger.Errorf("Ignoring the metrics recording as recorder not initialized ") |
| 125 | + return |
| 126 | + } |
| 127 | + switch mt := metricType; mt { |
| 128 | + case chains.SignedMessagesCount: |
| 129 | + r.countMetrics(ctx, sgCount) |
| 130 | + case chains.PayloadUploadeCount: |
| 131 | + r.countMetrics(ctx, plCount) |
| 132 | + case chains.SignsStoredCount: |
| 133 | + r.countMetrics(ctx, stCount) |
| 134 | + case chains.MarkedAsSignedCount: |
| 135 | + r.countMetrics(ctx, mrCount) |
| 136 | + default: |
| 137 | + logger.Errorf("Ignoring the metrics recording as valid Metric type matching %v was not found", mt) |
| 138 | + } |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +func (r *Recorder) countMetrics(ctx context.Context, measure *stats.Float64Measure) { |
| 143 | + metrics.Record(ctx, measure.M(1)) |
| 144 | +} |
0 commit comments