From f217ccd3d84bd403a15838bf7d22350a0c2a3efb Mon Sep 17 00:00:00 2001 From: Drew Gonzales Date: Fri, 20 Sep 2024 11:37:45 -0700 Subject: [PATCH] let's see if this works --- .../pkg/storage/etcd3/metrics/metrics.go | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics/metrics.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics/metrics.go index 1c0307bd913d5..8aa22231d9bb0 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics/metrics.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics/metrics.go @@ -260,18 +260,29 @@ type monitorCollector struct { mutex sync.Mutex monitorGetter func() ([]Monitor, error) + monitors *[]Monitor + + // get storage metric monitor is protected by mutex + monitorMutex sync.Mutex } -func (m *monitorCollector) setGetter(monitorGetter func() ([]Monitor, error)) { - m.mutex.Lock() - defer m.mutex.Unlock() - m.monitorGetter = monitorGetter +func (m *monitorCollector) getMonitors() ([]Monitor, error) { + m.monitorMutex.Lock() + defer m.monitorMutex.Unlock() + if m.monitors == nil { + getters, err := m.monitorGetter() + if err != nil { + return nil, err + } + m.monitors = &getters + } + return *m.monitors, nil } -func (m *monitorCollector) getGetter() func() ([]Monitor, error) { +func (m *monitorCollector) setGetter(monitorGetter func() ([]Monitor, error)) { m.mutex.Lock() defer m.mutex.Unlock() - return m.monitorGetter + m.monitorGetter = monitorGetter } // DescribeWithStability implements compbasemetrics.StableColletor @@ -281,7 +292,7 @@ func (c *monitorCollector) DescribeWithStability(ch chan<- *compbasemetrics.Desc // CollectWithStability implements compbasemetrics.StableColletor func (c *monitorCollector) CollectWithStability(ch chan<- compbasemetrics.Metric) { - monitors, err := c.getGetter()() + monitors, err := c.getMonitors() if err != nil { return } @@ -293,7 +304,6 @@ func (c *monitorCollector) CollectWithStability(ch chan<- compbasemetrics.Metric ctx, cancel := context.WithTimeout(context.Background(), time.Second) metrics, err := m.Monitor(ctx) cancel() - m.Close() if err != nil { klog.InfoS("Failed to get storage metrics", "storage_cluster_id", storageClusterID, "err", err) continue