Skip to content

Commit

Permalink
Re-register metrics only during real updation
Browse files Browse the repository at this point in the history
Before this, metrics were beign registered and unregisterd even
when anything in configMap used to change including revision, etc.
  • Loading branch information
khrm committed Sep 5, 2024
1 parent a4b4fe0 commit 2916734
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
29 changes: 27 additions & 2 deletions pkg/pipelinerunmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ import (
"sync"
"time"

"encoding/hex"

"golang.org/x/crypto/blake2b"

"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"

"go.uber.org/zap"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/labels"

"knative.dev/pkg/apis"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
Expand Down Expand Up @@ -111,6 +119,8 @@ type Recorder struct {
pipelinerun string) []tag.Mutator

ReportingPeriod time.Duration

hash string
}

// We cannot register the view multiple times, so NewRecorder lazily
Expand Down Expand Up @@ -272,7 +282,10 @@ func OnStore(logger *zap.SugaredLogger, r *Recorder) func(name string,
logger.Error("Failed to do type insertion for extracting metrics config")
return
}
r.updateConfig(cfg)
updated := r.updateConfig(cfg)
if !updated {
return
}
// Update metrics according to configuration
viewUnregister()
err := viewRegister(cfg)
Expand Down Expand Up @@ -317,11 +330,23 @@ func getPipelineTagName(pr *v1.PipelineRun) string {
return pipelineName
}

func (r *Recorder) updateConfig(cfg *config.Metrics) {
func (r *Recorder) updateConfig(cfg *config.Metrics) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
var hash string
if cfg != nil {
s := fmt.Sprintf("%v", *cfg)
sum := blake2b.Sum256([]byte(s))
hash = hex.EncodeToString(sum[:])
}

if r.hash == hash {
return false
}

r.cfg = cfg

return true
}

// DurationAndCount logs the duration of PipelineRun execution and
Expand Down
31 changes: 29 additions & 2 deletions pkg/taskrunmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ import (
"sync"
"time"

"encoding/hex"

"golang.org/x/crypto/blake2b"

"github.com/pkg/errors"

"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/pod"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"

"go.uber.org/zap"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"knative.dev/pkg/apis"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
Expand Down Expand Up @@ -130,6 +139,8 @@ type Recorder struct {

insertPipelineTag func(pipeline,
pipelinerun string) []tag.Mutator

hash string
}

// We cannot register the view multiple times, so NewRecorder lazily
Expand Down Expand Up @@ -335,7 +346,10 @@ func OnStore(logger *zap.SugaredLogger, r *Recorder) func(name string, value int
logger.Error("Failed to do type insertion for extracting metrics config")
return
}
r.updateConfig(cfg)
updated := r.updateConfig(cfg)
if !updated {
return
}
// Update metrics according to the configuration
viewUnregister()
err := viewRegister(cfg)
Expand Down Expand Up @@ -400,11 +414,24 @@ func getTaskTagName(tr *v1.TaskRun) string {
return taskName
}

func (r *Recorder) updateConfig(cfg *config.Metrics) {
func (r *Recorder) updateConfig(cfg *config.Metrics) bool {
r.mutex.Lock()
defer r.mutex.Unlock()

var hash string
if cfg != nil {
s := fmt.Sprintf("%v", *cfg)
sum := blake2b.Sum256([]byte(s))
hash = hex.EncodeToString(sum[:])
}

if r.hash == hash {
return false
}

r.cfg = cfg

return true
}

// DurationAndCount logs the duration of TaskRun execution and
Expand Down

0 comments on commit 2916734

Please sign in to comment.