From 617949327f84df51b1af333f2ba029d371dfcc91 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Fri, 3 Feb 2023 19:43:30 +0000 Subject: [PATCH] Collect state counts for endpoint and endpoint slices Define SyncerEPStat to store the count of endpoint and endpoint slices in different states. Define the states for endpoint and endpoint slices. Collect state counts in neg metrics collector. --- pkg/neg/metrics/neg_metrics_collector.go | 30 +++++++++++++++++++++--- pkg/neg/types/endpoint_stat.go | 27 ++++++++++----------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/pkg/neg/metrics/neg_metrics_collector.go b/pkg/neg/metrics/neg_metrics_collector.go index 7cec9491c9..b8b8f81dd1 100644 --- a/pkg/neg/metrics/neg_metrics_collector.go +++ b/pkg/neg/metrics/neg_metrics_collector.go @@ -27,11 +27,16 @@ import ( type SyncerMetricsCollector interface { UpdateSyncer(key negtypes.NegSyncerKey, result *negtypes.NegSyncResult) + SetSyncerEPMetrics(key negtypes.NegSyncerKey, epState *negtypes.SyncerEPStat) } type SyncerMetrics struct { // syncerStatusMap tracks the status of each syncer syncerStatusMap map[negtypes.NegSyncerKey]string + // syncerEndpointStateMap is a map between syncer and endpoint state counts + syncerEndpointStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap + // syncerEPSStateMap is a map between syncer and endpoint slice state counts + syncerEPSStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap // mu avoid race conditions and ensure correctness of metrics mu sync.Mutex // duration between metrics exports @@ -43,9 +48,11 @@ type SyncerMetrics struct { // NewNEGMetricsCollector initializes SyncerMetrics and starts a go routine to compute and export metrics periodically. func NewNegMetricsCollector(exportInterval time.Duration, logger klog.Logger) *SyncerMetrics { return &SyncerMetrics{ - syncerStatusMap: make(map[negtypes.NegSyncerKey]string), - metricsInterval: exportInterval, - logger: logger.WithName("NegMetricsCollector"), + syncerStatusMap: make(map[negtypes.NegSyncerKey]string), + syncerEndpointStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap), + syncerEPSStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap), + metricsInterval: exportInterval, + logger: logger.WithName("NegMetricsCollector"), } } @@ -82,3 +89,20 @@ func (sm *SyncerMetrics) UpdateSyncer(key negtypes.NegSyncerKey, syncResult *neg } sm.syncerStatusMap[key] = syncResult.Result } + +// SetSyncerEPMetrics update the endpoint count based on the endpointStat +func (sm *SyncerMetrics) SetSyncerEPMetrics(key negtypes.NegSyncerKey, endpointStat *negtypes.SyncerEPStat) { + sm.mu.Lock() + defer sm.mu.Unlock() + if sm.syncerEndpointStateMap == nil { + sm.syncerEndpointStateMap = make(map[negtypes.NegSyncerKey]negtypes.StateCountMap) + sm.logger.V(3).Info("Syncer Metrics failed to initialize correctly, reinitializing syncerEPStateMap: %v", sm.syncerEndpointStateMap) + } + sm.syncerEndpointStateMap[key] = endpointStat.EndpointStateCount + + if sm.syncerEPSStateMap == nil { + sm.syncerEPSStateMap = make(map[negtypes.NegSyncerKey]negtypes.StateCountMap) + sm.logger.V(3).Info("Syncer Metrics failed to initialize correctly, reinitializing syncerEPSStateMap: %v", sm.syncerEPSStateMap) + } + sm.syncerEPSStateMap[key] = endpointStat.EndpointSliceStateCount +} diff --git a/pkg/neg/types/endpoint_stat.go b/pkg/neg/types/endpoint_stat.go index 6ed9467901..70e26ae612 100644 --- a/pkg/neg/types/endpoint_stat.go +++ b/pkg/neg/types/endpoint_stat.go @@ -18,23 +18,22 @@ package types type State string -func (s State) String() string { - return string(s) -} - const ( - EPMissingNodeName = State("endpointMissingNodeName") - EPMissingPod = State("endpointMissingPod") - EPMissingZone = State("endpointMissingZone") - EPMissingField = State("endpointMissingField") - EPDuplicate = State("endpointDuplicate") - EPTotal = State("endpointTotal") + EPMissingNodeName = State("EndpointMissingNodeName") + EPMissingPod = State("EndpointMissingPod") + EPMissingZone = State("EndpointMissingZone") + EPMissingField = State("EndpointMissingField") + EPDuplicate = State("EndpointDuplicate") + EPTotal = State("EndpointTotal") + + EPSWithMissingNodeName = State("EndpointsliceWithMissingNodeNameEP") + EPSWithMissingPod = State("EndpointsliceWithMissingPodEP") + EPSWithMissingZone = State("EndpointsliceWithMissingZoneEP") + EPSWithMissingField = State("EndpointsliceWithMissingFieldEP") + EPSWithDuplicate = State("EndpointsliceWithDuplicateEP") + EPSTotal = State("EndpointsliceTotal") ) -func StateForEP() []State { - return []State{EPMissingNodeName, EPMissingPod, EPMissingZone, EPMissingField, EPDuplicate, EPTotal} -} - // SyncerEPStat contains endpoint and endpointslice status related to a syncer type SyncerEPStat struct { EndpointStateCount StateCountMap