From 467ff51a80f1aca7e8466bec6133494ba3206601 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 | 29 ++++++++++++++++++++++-- pkg/neg/types/endpoint_stat.go | 11 +++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pkg/neg/metrics/neg_metrics_collector.go b/pkg/neg/metrics/neg_metrics_collector.go index 2b44845a14..169c714e78 100644 --- a/pkg/neg/metrics/neg_metrics_collector.go +++ b/pkg/neg/metrics/neg_metrics_collector.go @@ -21,13 +21,19 @@ import ( "time" "k8s.io/apimachinery/pkg/util/wait" + negtypes "k8s.io/ingress-gce/pkg/neg/types" "k8s.io/klog/v2" ) type SyncerMetricsCollector interface { + SetSyncerEPMetrics(key negtypes.NegSyncerKey, epState *negtypes.SyncerEPStat) } type SyncerMetrics struct { + // 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 @@ -39,8 +45,10 @@ 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{ - metricsInterval: exportInterval, - logger: logger.WithName("NegMetricsCollector"), + syncerEndpointStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap), + syncerEPSStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap), + metricsInterval: exportInterval, + logger: logger.WithName("NegMetricsCollector"), } } @@ -66,3 +74,20 @@ func (sm *SyncerMetrics) Run(stopCh <-chan struct{}) { // export exports syncer metrics. func (sm *SyncerMetrics) export() { } + +// 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..0fb2fcd788 100644 --- a/pkg/neg/types/endpoint_stat.go +++ b/pkg/neg/types/endpoint_stat.go @@ -29,12 +29,23 @@ const ( 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} } +func StateForEPS() []State { + return []State{EPSWithMissingNodeName, EPSWithMissingPod, EPSWithMissingZone, EPSWithMissingField, EPSWithDuplicate, EPSTotal} +} + // SyncerEPStat contains endpoint and endpointslice status related to a syncer type SyncerEPStat struct { EndpointStateCount StateCountMap