Skip to content

Commit

Permalink
Add metrics for chains to fix issue#1028
Browse files Browse the repository at this point in the history
  • Loading branch information
sudhishmk committed Jan 29, 2024
1 parent ae8465f commit 890253f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pkg/chains/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 The Tekton Authors
Copyright 2024 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
Expand Down
18 changes: 7 additions & 11 deletions pkg/chains/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Signer interface {
}

type MetricsRecorder interface {
RecordCountMetrics(MetricType string, ctx context.Context)
RecordCountMetrics(ctx context.Context, MetricType string)
}

type ObjectSigner struct {
Expand Down Expand Up @@ -180,8 +180,7 @@ func (o *ObjectSigner) Sign(ctx context.Context, tektonObj objects.TektonObject)
logger.Error(err)
continue
}
logger.Debugf("Recording count of signed messages for %v", obj)
measureMetrics(SignedMessagesCount, o.Recorder, ctx)
measureMetrics(ctx, SignedMessagesCount, o.Recorder)

// Now store those!
for _, backend := range sets.List[string](signableType.StorageBackend(cfg)) {
Expand All @@ -197,8 +196,7 @@ func (o *ObjectSigner) Sign(ctx context.Context, tektonObj objects.TektonObject)
logger.Error(err)
merr = multierror.Append(merr, err)
} else {
logger.Debugf("Recording count of stored payload for %v", obj)
measureMetrics(SignsStoredCount, o.Recorder, ctx)
measureMetrics(ctx, SignsStoredCount, o.Recorder)
}
}

Expand All @@ -215,8 +213,7 @@ func (o *ObjectSigner) Sign(ctx context.Context, tektonObj objects.TektonObject)
} else {
logger.Infof("Uploaded entry to %s with index %d", cfg.Transparency.URL, *entry.LogIndex)
extraAnnotations[ChainsTransparencyAnnotation] = fmt.Sprintf("%s/api/v1/log/entries?logIndex=%d", cfg.Transparency.URL, *entry.LogIndex)
logger.Debugf("Recording count of uploaded artifacts for %v", obj)
measureMetrics(PayloadUploadeCount, o.Recorder, ctx)
measureMetrics(ctx, PayloadUploadeCount, o.Recorder)
}
}

Expand All @@ -234,14 +231,13 @@ func (o *ObjectSigner) Sign(ctx context.Context, tektonObj objects.TektonObject)
if err := MarkSigned(ctx, tektonObj, o.Pipelineclientset, extraAnnotations); err != nil {
return err
}
logger.Debugf("Recording count of marked payload for %v", tektonObj)
measureMetrics(MarkedAsSignedCount, o.Recorder, ctx)
measureMetrics(ctx, MarkedAsSignedCount, o.Recorder)
return nil
}

func measureMetrics(metrictype string, mtr MetricsRecorder, ctx context.Context) {
func measureMetrics(ctx context.Context, metrictype string, mtr MetricsRecorder) {
if mtr != nil {
mtr.RecordCountMetrics(metrictype, ctx)
mtr.RecordCountMetrics(ctx, metrictype)
}

}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pipelinerunmetrics/fake/fake.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Tekton Authors
Copyright 2024 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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipelinerunmetrics/injection.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Tekton Authors
Copyright 2024 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.
Expand Down Expand Up @@ -44,7 +44,7 @@ func WithClient(ctx context.Context) context.Context {
func Get(ctx context.Context) *Recorder {
untyped := ctx.Value(RecorderKey{})
if untyped == nil {
logging.FromContext(ctx).Panic("Unable to fetch *pipelinerunmetrics.Recorder from context.")
logging.FromContext(ctx).Errorf("Unable to fetch *pipelinerunmetrics.Recorder from context.")
}
return untyped.(*Recorder)
}
4 changes: 2 additions & 2 deletions pkg/pipelinerunmetrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Tekton Authors
Copyright 2024 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.
Expand Down Expand Up @@ -118,7 +118,7 @@ func viewRegister() error {
)
}

func (r *Recorder) RecordCountMetrics(metricType string, ctx context.Context) {
func (r *Recorder) RecordCountMetrics(ctx context.Context, metricType string) {
logger := logging.FromContext(ctx)
if !r.initialized {
logger.Errorf("Ignoring the metrics recording as recorder not initialized ")
Expand Down
18 changes: 9 additions & 9 deletions pkg/pipelinerunmetrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Tekton Authors
Copyright 2024 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.
Expand Down Expand Up @@ -30,16 +30,16 @@ func TestUninitializedMetrics(t *testing.T) {
metrics := &Recorder{}
ctx := context.Background()

metrics.RecordCountMetrics(chains.SignedMessagesCount, ctx)
metrics.RecordCountMetrics(ctx, chains.SignedMessagesCount)
metricstest.CheckStatsNotReported(t, chains.PipelineRunSignedName)

metrics.RecordCountMetrics(chains.PayloadUploadeCount, ctx)
metrics.RecordCountMetrics(ctx, chains.PayloadUploadeCount)
metricstest.CheckStatsNotReported(t, chains.PipelineRunUploadedName)

metrics.RecordCountMetrics(chains.SignsStoredCount, ctx)
metrics.RecordCountMetrics(ctx, chains.SignsStoredCount)
metricstest.CheckStatsNotReported(t, chains.PipelineRunStoredName)

metrics.RecordCountMetrics(chains.MarkedAsSignedCount, ctx)
metrics.RecordCountMetrics(ctx, chains.MarkedAsSignedCount)
metricstest.CheckStatsNotReported(t, chains.PipelineRunMarkedName)
}

Expand All @@ -50,13 +50,13 @@ func TestCountMetrics(t *testing.T) {

rec := Get(ctx)

rec.RecordCountMetrics(chains.SignedMessagesCount, ctx)
rec.RecordCountMetrics(ctx, chains.SignedMessagesCount)
metricstest.CheckCountData(t, chains.PipelineRunSignedName, map[string]string{}, 1)
rec.RecordCountMetrics(chains.PayloadUploadeCount, ctx)
rec.RecordCountMetrics(ctx, chains.PayloadUploadeCount)
metricstest.CheckCountData(t, chains.PipelineRunUploadedName, map[string]string{}, 1)
rec.RecordCountMetrics(chains.SignsStoredCount, ctx)
rec.RecordCountMetrics(ctx, chains.SignsStoredCount)
metricstest.CheckCountData(t, chains.PipelineRunStoredName, map[string]string{}, 1)
rec.RecordCountMetrics(chains.MarkedAsSignedCount, ctx)
rec.RecordCountMetrics(ctx, chains.MarkedAsSignedCount)
metricstest.CheckCountData(t, chains.PipelineRunMarkedName, map[string]string{}, 1)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/taskrunmetrics/fake/fake.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Tekton Authors
Copyright 2024 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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/taskrunmetrics/injection.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Tekton Authors
Copyright 2024 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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/taskrunmetrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Tekton Authors
Copyright 2024 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.
Expand Down Expand Up @@ -119,7 +119,7 @@ func viewRegister() error {
)
}

func (r *Recorder) RecordCountMetrics(metricType string, ctx context.Context) {
func (r *Recorder) RecordCountMetrics(ctx context.Context, metricType string) {
logger := logging.FromContext(ctx)

if !r.initialized {
Expand Down
18 changes: 9 additions & 9 deletions pkg/taskrunmetrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Tekton Authors
Copyright 2024 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.
Expand Down Expand Up @@ -30,16 +30,16 @@ func TestUninitializedMetrics(t *testing.T) {
metrics := &Recorder{}
ctx := context.Background()

metrics.RecordCountMetrics(chains.SignedMessagesCount, ctx)
metrics.RecordCountMetrics(ctx, chains.SignedMessagesCount)
metricstest.CheckStatsNotReported(t, chains.TaskRunSignedName)

metrics.RecordCountMetrics(chains.PayloadUploadeCount, ctx)
metrics.RecordCountMetrics(ctx, chains.PayloadUploadeCount)
metricstest.CheckStatsNotReported(t, chains.TaskRunUploadedName)

metrics.RecordCountMetrics(chains.SignsStoredCount, ctx)
metrics.RecordCountMetrics(ctx, chains.SignsStoredCount)
metricstest.CheckStatsNotReported(t, chains.TaskRunStoredName)

metrics.RecordCountMetrics(chains.MarkedAsSignedCount, ctx)
metrics.RecordCountMetrics(ctx, chains.MarkedAsSignedCount)
metricstest.CheckStatsNotReported(t, chains.TaskRunMarkedName)
}

Expand All @@ -50,13 +50,13 @@ func TestCountMetrics(t *testing.T) {

rec := Get(ctx)

rec.RecordCountMetrics(chains.SignedMessagesCount, ctx)
rec.RecordCountMetrics(ctx, chains.SignedMessagesCount)
metricstest.CheckCountData(t, chains.TaskRunSignedName, map[string]string{}, 1)
rec.RecordCountMetrics(chains.PayloadUploadeCount, ctx)
rec.RecordCountMetrics(ctx, chains.PayloadUploadeCount)
metricstest.CheckCountData(t, chains.TaskRunUploadedName, map[string]string{}, 1)
rec.RecordCountMetrics(chains.SignsStoredCount, ctx)
rec.RecordCountMetrics(ctx, chains.SignsStoredCount)
metricstest.CheckCountData(t, chains.TaskRunStoredName, map[string]string{}, 1)
rec.RecordCountMetrics(chains.MarkedAsSignedCount, ctx)
rec.RecordCountMetrics(ctx, chains.MarkedAsSignedCount)
metricstest.CheckCountData(t, chains.TaskRunMarkedName, map[string]string{}, 1)
}

Expand Down

0 comments on commit 890253f

Please sign in to comment.