Skip to content

Commit 3429744

Browse files
vdemeestertekton-robot
authored andcommitted
{taskrun,pipelinerun}metrics: make sure config is up-to-date
This updates some metrics package and struct to be able to keep up-to-date the metrics configuration in the background go routines that are used. Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
1 parent 9cf9672 commit 3429744

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

pkg/pipelinerunmetrics/metrics.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const (
105105
type Recorder struct {
106106
mutex sync.Mutex
107107
initialized bool
108+
cfg *config.Metrics
108109

109110
insertTag func(pipeline,
110111
pipelinerun string) []tag.Mutator
@@ -261,8 +262,8 @@ func viewUnregister() {
261262
runningPRsWaitingOnTaskResolutionView)
262263
}
263264

264-
// MetricsOnStore returns a function that checks if metrics are configured for a config.Store, and registers it if so
265-
func MetricsOnStore(logger *zap.SugaredLogger) func(name string,
265+
// OnStore returns a function that checks if metrics are configured for a config.Store, and registers it if so
266+
func OnStore(logger *zap.SugaredLogger, r *Recorder) func(name string,
266267
value interface{}) {
267268
return func(name string, value interface{}) {
268269
if name == config.GetMetricsConfigName() {
@@ -271,6 +272,8 @@ func MetricsOnStore(logger *zap.SugaredLogger) func(name string,
271272
logger.Error("Failed to do type insertion for extracting metrics config")
272273
return
273274
}
275+
r.updateConfig(cfg)
276+
// Update metrics according to configuration
274277
viewUnregister()
275278
err := viewRegister(cfg)
276279
if err != nil {
@@ -282,8 +285,10 @@ func MetricsOnStore(logger *zap.SugaredLogger) func(name string,
282285
}
283286

284287
func pipelinerunInsertTag(pipeline, pipelinerun string) []tag.Mutator {
285-
return []tag.Mutator{tag.Insert(pipelineTag, pipeline),
286-
tag.Insert(pipelinerunTag, pipelinerun)}
288+
return []tag.Mutator{
289+
tag.Insert(pipelineTag, pipeline),
290+
tag.Insert(pipelinerunTag, pipelinerun),
291+
}
287292
}
288293

289294
func pipelineInsertTag(pipeline, pipelinerun string) []tag.Mutator {
@@ -312,6 +317,13 @@ func getPipelineTagName(pr *v1.PipelineRun) string {
312317
return pipelineName
313318
}
314319

320+
func (r *Recorder) updateConfig(cfg *config.Metrics) {
321+
r.mutex.Lock()
322+
defer r.mutex.Unlock()
323+
324+
r.cfg = cfg
325+
}
326+
315327
// DurationAndCount logs the duration of PipelineRun execution and
316328
// count for number of PipelineRuns succeed or failed
317329
// returns an error if its failed to log the metrics
@@ -351,8 +363,10 @@ func (r *Recorder) DurationAndCount(pr *v1.PipelineRun, beforeCondition *apis.Co
351363

352364
ctx, err := tag.New(
353365
context.Background(),
354-
append([]tag.Mutator{tag.Insert(namespaceTag, pr.Namespace),
355-
tag.Insert(statusTag, status), tag.Insert(reasonTag, reason)}, r.insertTag(pipelineName, pr.Name)...)...)
366+
append([]tag.Mutator{
367+
tag.Insert(namespaceTag, pr.Namespace),
368+
tag.Insert(statusTag, status), tag.Insert(reasonTag, reason),
369+
}, r.insertTag(pipelineName, pr.Name)...)...)
356370
if err != nil {
357371
return err
358372
}

pkg/pipelinerunmetrics/metrics_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestUninitializedMetrics(t *testing.T) {
6969
}
7070
}
7171

72-
func TestMetricsOnStore(t *testing.T) {
72+
func TestOnStore(t *testing.T) {
7373
log := zap.NewExample()
7474
defer log.Sync()
7575
logger := log.Sugar()
@@ -81,7 +81,7 @@ func TestMetricsOnStore(t *testing.T) {
8181
}
8282

8383
// We check that there's no change when incorrect config is passed
84-
MetricsOnStore(logger)(config.GetMetricsConfigName(), &config.Store{})
84+
OnStore(logger, metrics)(config.GetMetricsConfigName(), &config.Store{})
8585
// Comparing function assign to struct with the one which should yield same value
8686
if reflect.ValueOf(metrics.insertTag).Pointer() != reflect.ValueOf(pipelinerunInsertTag).Pointer() {
8787
t.Fatal("metrics recorder shouldn't change during this OnStore call")
@@ -94,7 +94,7 @@ func TestMetricsOnStore(t *testing.T) {
9494
DurationTaskrunType: config.DurationTaskrunTypeHistogram,
9595
DurationPipelinerunType: config.DurationPipelinerunTypeLastValue,
9696
}
97-
MetricsOnStore(logger)(config.GetMetricsConfigName(), cfg)
97+
OnStore(logger, metrics)(config.GetMetricsConfigName(), cfg)
9898
if reflect.ValueOf(metrics.insertTag).Pointer() != reflect.ValueOf(pipelinerunInsertTag).Pointer() {
9999
t.Fatal("metrics recorder shouldn't change during this OnStore call")
100100
}
@@ -105,7 +105,7 @@ func TestMetricsOnStore(t *testing.T) {
105105
DurationTaskrunType: config.DurationTaskrunTypeHistogram,
106106
DurationPipelinerunType: config.DurationPipelinerunTypeLastValue,
107107
}
108-
MetricsOnStore(logger)(config.GetMetricsConfigName(), cfg)
108+
OnStore(logger, metrics)(config.GetMetricsConfigName(), cfg)
109109
if reflect.ValueOf(metrics.insertTag).Pointer() != reflect.ValueOf(nilInsertTag).Pointer() {
110110
t.Fatal("metrics recorder didn't change during OnStore call")
111111
}

pkg/reconciler/pipelinerun/controller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ func NewController(opts *pipeline.Options, clock clock.PassiveClock) func(contex
6262
verificationpolicyInformer := verificationpolicyinformer.Get(ctx)
6363
secretinformer := secretinformer.Get(ctx)
6464
tracerProvider := tracing.New(TracerProviderName, logger.Named("tracing"))
65+
pipelinerunmetricsRecorder := pipelinerunmetrics.Get(ctx)
6566
//nolint:contextcheck // OnStore methods does not support context as a parameter
66-
configStore := config.NewStore(logger.Named("config-store"), pipelinerunmetrics.MetricsOnStore(logger), tracerProvider.OnStore(secretinformer.Lister()))
67+
configStore := config.NewStore(logger.Named("config-store"),
68+
pipelinerunmetrics.OnStore(logger, pipelinerunmetricsRecorder),
69+
tracerProvider.OnStore(secretinformer.Lister()),
70+
)
6771
configStore.WatchConfigs(cmw)
6872

6973
c := &Reconciler{
@@ -76,7 +80,7 @@ func NewController(opts *pipeline.Options, clock clock.PassiveClock) func(contex
7680
customRunLister: customRunInformer.Lister(),
7781
verificationPolicyLister: verificationpolicyInformer.Lister(),
7882
cloudEventClient: cloudeventclient.Get(ctx),
79-
metrics: pipelinerunmetrics.Get(ctx),
83+
metrics: pipelinerunmetricsRecorder,
8084
pvcHandler: volumeclaim.NewPVCHandler(kubeclientset, logger),
8185
resolutionRequester: resolution.NewCRDRequester(resolutionclient.Get(ctx), resolutionInformer.Lister()),
8286
tracerProvider: tracerProvider,

pkg/reconciler/taskrun/controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ func NewController(opts *pipeline.Options, clock clock.PassiveClock) func(contex
6565
secretinformer := secretinformer.Get(ctx)
6666
spireClient := spire.GetControllerAPIClient(ctx)
6767
tracerProvider := tracing.New(TracerProviderName, logger.Named("tracing"))
68+
taskrunmetricsRecorder := taskrunmetrics.Get(ctx)
6869
//nolint:contextcheck // OnStore methods does not support context as a parameter
69-
configStore := config.NewStore(logger.Named("config-store"), taskrunmetrics.MetricsOnStore(logger), spire.OnStore(ctx, logger), tracerProvider.OnStore(secretinformer.Lister()))
70+
configStore := config.NewStore(logger.Named("config-store"),
71+
taskrunmetrics.OnStore(logger, taskrunmetricsRecorder),
72+
spire.OnStore(ctx, logger),
73+
tracerProvider.OnStore(secretinformer.Lister()),
74+
)
7075
configStore.WatchConfigs(cmw)
7176

7277
entrypointCache, err := pod.NewEntrypointCache(kubeclientset)
@@ -84,7 +89,7 @@ func NewController(opts *pipeline.Options, clock clock.PassiveClock) func(contex
8489
limitrangeLister: limitrangeInformer.Lister(),
8590
verificationPolicyLister: verificationpolicyInformer.Lister(),
8691
cloudEventClient: cloudeventclient.Get(ctx),
87-
metrics: taskrunmetrics.Get(ctx),
92+
metrics: taskrunmetricsRecorder,
8893
entrypointCache: entrypointCache,
8994
podLister: podInformer.Lister(),
9095
pvcHandler: volumeclaim.NewPVCHandler(kubeclientset, logger),

pkg/taskrunmetrics/metrics.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ var (
121121
type Recorder struct {
122122
mutex sync.Mutex
123123
initialized bool
124+
cfg *config.Metrics
124125

125126
ReportingPeriod time.Duration
126127

@@ -144,15 +145,15 @@ var (
144145
// to log the TaskRun related metrics
145146
func NewRecorder(ctx context.Context) (*Recorder, error) {
146147
once.Do(func() {
148+
cfg := config.FromContextOrDefaults(ctx)
147149
r = &Recorder{
148150
initialized: true,
151+
cfg: cfg.Metrics,
149152

150153
// Default to reporting metrics every 30s.
151154
ReportingPeriod: 30 * time.Second,
152155
}
153156

154-
cfg := config.FromContextOrDefaults(ctx)
155-
156157
errRegistering = viewRegister(cfg.Metrics)
157158
if errRegistering != nil {
158159
r.initialized = false
@@ -325,16 +326,17 @@ func viewUnregister() {
325326
)
326327
}
327328

328-
// MetricsOnStore returns a function that checks if metrics are configured for a config.Store, and registers it if so
329-
func MetricsOnStore(logger *zap.SugaredLogger) func(name string,
330-
value interface{}) {
329+
// OnStore returns a function that checks if metrics are configured for a config.Store, and registers it if so
330+
func OnStore(logger *zap.SugaredLogger, r *Recorder) func(name string, value interface{}) {
331331
return func(name string, value interface{}) {
332332
if name == config.GetMetricsConfigName() {
333333
cfg, ok := value.(*config.Metrics)
334334
if !ok {
335335
logger.Error("Failed to do type insertion for extracting metrics config")
336336
return
337337
}
338+
r.updateConfig(cfg)
339+
// Update metrics according to the configuration
338340
viewUnregister()
339341
err := viewRegister(cfg)
340342
if err != nil {
@@ -389,6 +391,13 @@ func getTaskTagName(tr *v1.TaskRun) string {
389391
return taskName
390392
}
391393

394+
func (r *Recorder) updateConfig(cfg *config.Metrics) {
395+
r.mutex.Lock()
396+
defer r.mutex.Unlock()
397+
398+
r.cfg = cfg
399+
}
400+
392401
// DurationAndCount logs the duration of TaskRun execution and
393402
// count for number of TaskRuns succeed or failed
394403
// returns an error if its failed to log the metrics
@@ -454,8 +463,7 @@ func (r *Recorder) RunningTaskRuns(ctx context.Context, lister listers.TaskRunLi
454463
return err
455464
}
456465

457-
cfg := config.FromContextOrDefaults(ctx)
458-
addNamespaceLabelToQuotaThrottleMetric := cfg.Metrics != nil && cfg.Metrics.ThrottleWithNamespace
466+
addNamespaceLabelToQuotaThrottleMetric := r.cfg != nil && r.cfg.ThrottleWithNamespace
459467

460468
var runningTrs int
461469
trsThrottledByQuota := map[string]int{}

pkg/taskrunmetrics/metrics_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestUninitializedMetrics(t *testing.T) {
8080
}
8181
}
8282

83-
func TestMetricsOnStore(t *testing.T) {
83+
func TestOnStore(t *testing.T) {
8484
log := zap.NewExample()
8585
defer log.Sync()
8686
logger := log.Sugar()
@@ -92,7 +92,7 @@ func TestMetricsOnStore(t *testing.T) {
9292
}
9393

9494
// We check that there's no change when incorrect config is passed
95-
MetricsOnStore(logger)(config.GetMetricsConfigName(), &config.Store{})
95+
OnStore(logger, metrics)(config.GetMetricsConfigName(), &config.Store{})
9696
// Comparing function assign to struct with the one which should yield same value
9797
if reflect.ValueOf(metrics.insertTaskTag).Pointer() != reflect.ValueOf(taskrunInsertTag).Pointer() {
9898
t.Fatalf("metrics recorder shouldn't change during this OnStore call")
@@ -107,7 +107,7 @@ func TestMetricsOnStore(t *testing.T) {
107107
}
108108

109109
// We test that there's no change when incorrect values in configmap is passed
110-
MetricsOnStore(logger)(config.GetMetricsConfigName(), cfg)
110+
OnStore(logger, metrics)(config.GetMetricsConfigName(), cfg)
111111
// Comparing function assign to struct with the one which should yield same value
112112
if reflect.ValueOf(metrics.insertTaskTag).Pointer() != reflect.ValueOf(taskrunInsertTag).Pointer() {
113113
t.Fatalf("metrics recorder shouldn't change during this OnStore call")
@@ -121,7 +121,7 @@ func TestMetricsOnStore(t *testing.T) {
121121
DurationPipelinerunType: config.DurationPipelinerunTypeLastValue,
122122
}
123123

124-
MetricsOnStore(logger)(config.GetMetricsConfigName(), cfg)
124+
OnStore(logger, metrics)(config.GetMetricsConfigName(), cfg)
125125
if reflect.ValueOf(metrics.insertTaskTag).Pointer() != reflect.ValueOf(nilInsertTag).Pointer() {
126126
t.Fatalf("metrics recorder didn't change during OnStore call")
127127
}

0 commit comments

Comments
 (0)