Skip to content

Commit

Permalink
Collect state counts for endpoint and endpoint slices
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sawsa307 committed Feb 5, 2023
1 parent 17aeb81 commit 467ff51
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
29 changes: 27 additions & 2 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
}
}

Expand All @@ -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
}
11 changes: 11 additions & 0 deletions pkg/neg/types/endpoint_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 467ff51

Please sign in to comment.