Skip to content

Commit

Permalink
let's see if this works
Browse files Browse the repository at this point in the history
  • Loading branch information
drewgonzales360 committed Sep 24, 2024
1 parent 40c5b2b commit 546d6e1
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func UpdateEtcdDbSize(ep string, size int64) {

// SetStorageMonitorGetter sets monitor getter to allow monitoring etcd stats.
func SetStorageMonitorGetter(getter func() ([]Monitor, error)) {
storageMonitor.monitorGetter = getter
storageMonitor.setGetter(getter)
}

// UpdateLeaseObjectCount sets the etcd_lease_object_counts metric.
Expand Down Expand Up @@ -258,7 +258,31 @@ type StorageMetrics struct {
type monitorCollector struct {
compbasemetrics.BaseStableCollector

mutex sync.Mutex
monitorGetter func() ([]Monitor, error)
monitors *[]Monitor

// get storage metric monitor is protected by mutex
monitorMutex sync.Mutex
}

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) setGetter(monitorGetter func() ([]Monitor, error)) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.monitorGetter = monitorGetter
}

// DescribeWithStability implements compbasemetrics.StableColletor
Expand All @@ -268,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.monitorGetter()
monitors, err := c.getMonitors()
if err != nil {
return
}
Expand All @@ -280,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
Expand Down

0 comments on commit 546d6e1

Please sign in to comment.