Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Re-register metrics only during real updation #8241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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