From 813bcc264862fb226f58623c6dffb6e377bd0324 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 21 Sep 2024 03:57:30 +0000 Subject: [PATCH] fix(deps): update patch digest dependencies --- go.mod | 6 +- go.sum | 12 ++-- .../client_golang/prometheus/histogram.go | 10 +-- .../rancher/lasso/pkg/cache/metrics.go | 67 +++++++++++++++++++ .../lasso/pkg/cache/sharedinformerfactory.go | 21 +++++- .../rancher/lasso/pkg/metrics/metrics.go | 44 ++++++++++-- .../rancher/lasso/pkg/metrics/register.go | 4 ++ vendor/modules.txt | 6 +- 8 files changed, 145 insertions(+), 25 deletions(-) create mode 100644 vendor/github.com/rancher/lasso/pkg/cache/metrics.go diff --git a/go.mod b/go.mod index 726a9add2b..9a971a0cc0 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/longhorn/longhorn-instance-manager v1.8.0-dev-20240908 github.com/longhorn/longhorn-share-manager v1.7.0-rc1 github.com/pkg/errors v0.9.1 - github.com/prometheus/client_golang v1.20.3 + github.com/prometheus/client_golang v1.20.4 github.com/rancher/dynamiclistener v0.6.0 github.com/rancher/go-rancher v0.1.1-0.20220412083059-ff12399dd57b github.com/rancher/wrangler/v3 v3.0.0 @@ -92,7 +92,7 @@ require ( k8s.io/kubernetes v1.31.1 k8s.io/metrics v0.31.1 k8s.io/mount-utils v0.31.1 - k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3 + k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 sigs.k8s.io/controller-runtime v0.19.0 ) @@ -178,7 +178,7 @@ require ( github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/rancher/lasso v0.0.0-20240911173354-6b2576c47b34 + github.com/rancher/lasso v0.0.0-20240920064621-23f2a336c9b7 github.com/robfig/cron/v3 v3.0.1 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect diff --git a/go.sum b/go.sum index 8d41b0bf74..1e2127227f 100644 --- a/go.sum +++ b/go.sum @@ -216,8 +216,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= -github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4= -github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= +github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= @@ -231,8 +231,8 @@ github.com/rancher/dynamiclistener v0.6.0 h1:M7x8Nq+GY0UORULANuW/AH1ocnyZaqlmTuv github.com/rancher/dynamiclistener v0.6.0/go.mod h1:7VNEQhAwzbYJ08S1MYb6B4vili6K7CcrG4cNZXq1j+s= github.com/rancher/go-rancher v0.1.1-0.20220412083059-ff12399dd57b h1:so40GMVZOZkQeIbAzaZRq6wDrMErvRLuXNsGTRZUpg8= github.com/rancher/go-rancher v0.1.1-0.20220412083059-ff12399dd57b/go.mod h1:7oQvGNiJsGvrUgB+7AH8bmdzuR0uhULfwKb43Ht0hUk= -github.com/rancher/lasso v0.0.0-20240911173354-6b2576c47b34 h1:cqnEBYJLe1khkRnxbe5FiRe2asnufFYurObKiQNuVbI= -github.com/rancher/lasso v0.0.0-20240911173354-6b2576c47b34/go.mod h1:+GJaJqWpk1MZYCDrMC+4Jee0M/aScoru5T8V2A//rgg= +github.com/rancher/lasso v0.0.0-20240920064621-23f2a336c9b7 h1:+kWiVtcUSkxOxnW1oIdmuV/MIFzkjacVlkMhZauGp6k= +github.com/rancher/lasso v0.0.0-20240920064621-23f2a336c9b7/go.mod h1:+GJaJqWpk1MZYCDrMC+4Jee0M/aScoru5T8V2A//rgg= github.com/rancher/wrangler/v3 v3.0.0 h1:IHHCA+vrghJDPxjtLk4fmeSCFhNe9fFzLFj3m2B0YpA= github.com/rancher/wrangler/v3 v3.0.0/go.mod h1:Dfckuuq7MJk2JWVBDywRlZXMxEyPxHy4XqGrPEzu5Eg= github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= @@ -457,8 +457,8 @@ k8s.io/metrics v0.31.1 h1:h4I4dakgh/zKflWYAOQhwf0EXaqy8LxAIyE/GBvxqRc= k8s.io/metrics v0.31.1/go.mod h1:JuH1S9tJiH9q1VCY0yzSCawi7kzNLsDzlWDJN4xR+iA= k8s.io/mount-utils v0.31.1 h1:f8UrH9kRynljmdNGM6BaCvFUON5ZPKDgE+ltmYqI4wA= k8s.io/mount-utils v0.31.1/go.mod h1:HV/VYBUGqYUj4vt82YltzpWvgv8FPg0G9ItyInT3NPU= -k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3 h1:b2FmK8YH+QEwq/Sy2uAEhmqL5nPfGYbJOcaqjeYYZoA= -k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 h1:MDF6h2H/h4tbzmtIKTuctcwZmY0tY9mD9fNT47QO6HI= +k8s.io/utils v0.0.0-20240921022957-49e7df575cb6/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3 h1:2770sDpzrjjsAtVhSeUFseziht227YAWYHLGNM8QPwY= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3/go.mod h1:Ve9uj1L+deCXFrPOk1LpFXqTg7LCFzFso6PA48q/XZw= sigs.k8s.io/controller-runtime v0.19.0 h1:nWVM7aq+Il2ABxwiCizrVDSlmDcshi9llbaFbC0ji/Q= diff --git a/vendor/github.com/prometheus/client_golang/prometheus/histogram.go b/vendor/github.com/prometheus/client_golang/prometheus/histogram.go index 8a4f49a4c5..519db348a7 100644 --- a/vendor/github.com/prometheus/client_golang/prometheus/histogram.go +++ b/vendor/github.com/prometheus/client_golang/prometheus/histogram.go @@ -844,9 +844,7 @@ func (h *histogram) Write(out *dto.Metric) error { }} } - // If exemplars are not configured, the cap will be 0. - // So append is not needed in this case. - if cap(h.nativeExemplars.exemplars) > 0 { + if h.nativeExemplars.isEnabled() { h.nativeExemplars.Lock() his.Exemplars = append(his.Exemplars, h.nativeExemplars.exemplars...) h.nativeExemplars.Unlock() @@ -1665,6 +1663,10 @@ type nativeExemplars struct { exemplars []*dto.Exemplar } +func (n *nativeExemplars) isEnabled() bool { + return n.ttl != -1 +} + func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars { if ttl == 0 { ttl = 5 * time.Minute @@ -1686,7 +1688,7 @@ func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars { } func (n *nativeExemplars) addExemplar(e *dto.Exemplar) { - if n.ttl == -1 { + if !n.isEnabled() { return } diff --git a/vendor/github.com/rancher/lasso/pkg/cache/metrics.go b/vendor/github.com/rancher/lasso/pkg/cache/metrics.go new file mode 100644 index 0000000000..2c342f2db2 --- /dev/null +++ b/vendor/github.com/rancher/lasso/pkg/cache/metrics.go @@ -0,0 +1,67 @@ +package cache + +import ( + "context" + "maps" + "time" + + "github.com/rancher/lasso/pkg/metrics" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +const ( + defaultCacheMetricsCollectionPeriod = 1 * time.Minute +) + +func (f *sharedCacheFactory) collectMetrics() sharedCacheFactoryMetrics { + // f.lock prevents concurrent read and write to the f.caches map + // Listing the cache store could be slow, so here we get a local copy of the map to minimize the locking time + f.lock.RLock() + caches := maps.Clone(f.caches) + f.lock.RUnlock() + + gvks := make(map[schema.GroupVersionKind]int) + for gvk, c := range caches { + gvks[gvk] = len(c.GetStore().List()) + } + return sharedCacheFactoryMetrics{ + gvks: gvks, + } +} + +type sharedCacheFactoryMetrics struct { + // gvks is the total count of cache items by GroupVersionKind + gvks map[schema.GroupVersionKind]int +} + +func (f *sharedCacheFactory) startMetricsCollection(ctx context.Context) { + go func() { + contextID := metrics.ContextID(ctx) + timer := time.NewTimer(f.metricsCollectionPeriod) + defer timer.Stop() + for { + factoryMetrics := f.collectMetrics() + f.recordMetricsForContext(factoryMetrics, contextID) + + timer.Reset(f.metricsCollectionPeriod) + select { + case <-ctx.Done(): + f.cleanupMetricsForContext(factoryMetrics, contextID) + return + case <-timer.C: + } + } + }() +} + +func (f *sharedCacheFactory) recordMetricsForContext(fm sharedCacheFactoryMetrics, contextID string) { + for gvk, count := range fm.gvks { + metrics.IncTotalCachedObjects(contextID, gvk, count) + } +} + +func (f *sharedCacheFactory) cleanupMetricsForContext(fm sharedCacheFactoryMetrics, contextID string) { + for gvk := range fm.gvks { + metrics.DelTotalCachedObjects(contextID, gvk) + } +} diff --git a/vendor/github.com/rancher/lasso/pkg/cache/sharedinformerfactory.go b/vendor/github.com/rancher/lasso/pkg/cache/sharedinformerfactory.go index 36294840d2..4cdd3f0a87 100644 --- a/vendor/github.com/rancher/lasso/pkg/cache/sharedinformerfactory.go +++ b/vendor/github.com/rancher/lasso/pkg/cache/sharedinformerfactory.go @@ -24,10 +24,14 @@ type SharedCacheFactoryOptions struct { KindNamespace map[schema.GroupVersionKind]string KindTweakList map[schema.GroupVersionKind]TweakListOptionsFunc HealthCallback func(healthy bool) + + // Determines how often metrics are gathered about how many resources are + // cached by gvk across all caches in the sharedCacheFactory + MetricsCollectionPeriod time.Duration } type sharedCacheFactory struct { - lock sync.Mutex + lock sync.RWMutex tweakList TweakListOptionsFunc defaultResync time.Duration @@ -40,6 +44,9 @@ type sharedCacheFactory struct { caches map[schema.GroupVersionKind]cache.SharedIndexInformer startedCaches map[schema.GroupVersionKind]bool + + metricsCollectionStarted bool + metricsCollectionPeriod time.Duration } // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options. @@ -47,7 +54,6 @@ func NewSharedCachedFactory(sharedClientFactory client.SharedClientFactory, opts opts = applyDefaults(opts) factory := &sharedCacheFactory{ - lock: sync.Mutex{}, tweakList: opts.DefaultTweakList, defaultResync: opts.DefaultResync, defaultNamespace: opts.DefaultNamespace, @@ -60,6 +66,7 @@ func NewSharedCachedFactory(sharedClientFactory client.SharedClientFactory, opts healthcheck: healthcheck{ callback: opts.HealthCallback, }, + metricsCollectionPeriod: opts.MetricsCollectionPeriod, } return factory @@ -71,6 +78,10 @@ func applyDefaults(opts *SharedCacheFactoryOptions) *SharedCacheFactoryOptions { newOpts = *opts } + if newOpts.MetricsCollectionPeriod == 0 { + newOpts.MetricsCollectionPeriod = defaultCacheMetricsCollectionPeriod + } + return &newOpts } @@ -106,6 +117,11 @@ func (f *sharedCacheFactory) Start(ctx context.Context) error { } } + if metrics.Enabled() && !f.metricsCollectionStarted { + f.startMetricsCollection(ctx) + f.metricsCollectionStarted = true + } + return nil } @@ -116,7 +132,6 @@ func (f *sharedCacheFactory) WaitForCacheSync(ctx context.Context) map[schema.Gr informers := map[schema.GroupVersionKind]cache.SharedIndexInformer{} for informerType, informer := range f.caches { - metrics.IncTotalCachedObjects(informerType.Group, informerType.Version, informerType.Kind, float64(len(informer.GetStore().List()))) if f.startedCaches[informerType] { informers[informerType] = informer } diff --git a/vendor/github.com/rancher/lasso/pkg/metrics/metrics.go b/vendor/github.com/rancher/lasso/pkg/metrics/metrics.go index 80440f077e..c16eaac026 100644 --- a/vendor/github.com/rancher/lasso/pkg/metrics/metrics.go +++ b/vendor/github.com/rancher/lasso/pkg/metrics/metrics.go @@ -1,9 +1,11 @@ package metrics import ( + "context" "strconv" "github.com/prometheus/client_golang/prometheus" + "k8s.io/apimachinery/pkg/runtime/schema" ) var prometheusMetrics = false @@ -14,11 +16,25 @@ const ( handlerNameLabel = "handler_name" hasErrorLabel = "has_error" + contextLabel = "ctx" groupLabel = "group" versionLabel = "version" kindLabel = "kind" ) +type contextIDKey struct{} + +// WithContextID stores an identifier within the Context for later use when collecting metrics +func WithContextID(ctx context.Context, id string) context.Context { + return context.WithValue(ctx, contextIDKey{}, id) +} + +// ContextID extracts the identifier previously set by WithContextID, returning an empty string otherwise +func ContextID(ctx context.Context) string { + id, _ := ctx.Value(contextIDKey{}).(string) + return id +} + var ( // https://prometheus.io/docs/practices/instrumentation/#use-labels explains logic of having 1 total_requests // counter with code label vs a counter for each code @@ -37,7 +53,7 @@ var ( Name: "total_cached_object", Help: "Total count of cached objects", }, - []string{groupLabel, versionLabel, kindLabel}, + []string{contextLabel, groupLabel, versionLabel, kindLabel}, ) // reconcileTime is a prometheus histogram metric exposes the duration of reconciliations per controller. @@ -61,15 +77,31 @@ func IncTotalHandlerExecutions(controllerName, handlerName string, hasError bool } } -func IncTotalCachedObjects(group, version, kind string, val float64) { +// IncTotalCachedObjects sets the cached items count for the specified context and GroupVersionKind +func IncTotalCachedObjects(ctxID string, gvk schema.GroupVersionKind, count int) { if prometheusMetrics { TotalCachedObjects.With( prometheus.Labels{ - groupLabel: group, - versionLabel: version, - kindLabel: kind, + contextLabel: ctxID, + groupLabel: gvk.Group, + versionLabel: gvk.Version, + kindLabel: gvk.Kind, + }, + ).Set(float64(count)) + } +} + +// DelTotalCachedObjects deletes the cached items count metric matching the provided content and GroupVersionKind +func DelTotalCachedObjects(ctxID string, gvk schema.GroupVersionKind) { + if prometheusMetrics { + TotalCachedObjects.Delete( + prometheus.Labels{ + contextLabel: ctxID, + groupLabel: gvk.Group, + versionLabel: gvk.Version, + kindLabel: gvk.Kind, }, - ).Set(val) + ) } } diff --git a/vendor/github.com/rancher/lasso/pkg/metrics/register.go b/vendor/github.com/rancher/lasso/pkg/metrics/register.go index 987d031a7e..8b1451720f 100644 --- a/vendor/github.com/rancher/lasso/pkg/metrics/register.go +++ b/vendor/github.com/rancher/lasso/pkg/metrics/register.go @@ -16,6 +16,10 @@ func init() { } } +func Enabled() bool { + return prometheusMetrics +} + // MustRegisterWithWorkqueue registers all metrics, including Kubernetes internal // workqueue metrics, with the provided registerer func MustRegisterWithWorkqueue(registerer prometheus.Registerer) { diff --git a/vendor/modules.txt b/vendor/modules.txt index 9e3d83e4f9..014bc912f8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -354,7 +354,7 @@ github.com/pmezard/go-difflib/difflib # github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 ## explicit; go 1.14 github.com/power-devops/perfstat -# github.com/prometheus/client_golang v1.20.3 +# github.com/prometheus/client_golang v1.20.4 ## explicit; go 1.20 github.com/prometheus/client_golang/internal/github.com/golang/gddo/httputil github.com/prometheus/client_golang/internal/github.com/golang/gddo/httputil/header @@ -390,7 +390,7 @@ github.com/rancher/dynamiclistener/storage/memory ## explicit github.com/rancher/go-rancher/api github.com/rancher/go-rancher/client -# github.com/rancher/lasso v0.0.0-20240911173354-6b2576c47b34 +# github.com/rancher/lasso v0.0.0-20240920064621-23f2a336c9b7 ## explicit; go 1.22.0 github.com/rancher/lasso/pkg/cache github.com/rancher/lasso/pkg/client @@ -1578,7 +1578,7 @@ k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1 # k8s.io/mount-utils v0.31.1 => k8s.io/mount-utils v0.31.1 ## explicit; go 1.22.0 k8s.io/mount-utils -# k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3 +# k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 ## explicit; go 1.18 k8s.io/utils/buffer k8s.io/utils/clock