Skip to content

Commit

Permalink
Create NEG metrics controller interface
Browse files Browse the repository at this point in the history
Create NEG metrics collector that would periodically emit NEG syncer
metrics.
  • Loading branch information
sawsa307 committed Feb 3, 2023
1 parent e246ee3 commit b5315cb
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/neg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func RegisterMetrics() {
prometheus.MustRegister(SyncerSyncLatency)
prometheus.MustRegister(LastSyncTimestamp)
prometheus.MustRegister(InitializationLatency)

RegisterSyncerMetrics()
})
}

Expand Down
84 changes: 84 additions & 0 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"sync"
"time"

"k8s.io/apimachinery/pkg/util/wait"
negtypes "k8s.io/ingress-gce/pkg/neg/types"
"k8s.io/klog/v2"
)

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 SyncerEPState
syncerEndpointStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap
// syncerEPSStateMap is a map between syncer and SyncerEPSState
syncerEPSStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap
// mu avoid race conditions and ensure correctness of metrics
mu sync.Mutex
// duration between metrics exports
metricsInterval time.Duration
// logger logs message related to NegMetricsCollector
logger klog.Logger
}

// 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),
syncerEndpointStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap),
syncerEPSStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap),
metricsInterval: exportInterval,
logger: logger.WithName("NegMetricsCollector"),
}
}

// FakeSyncerMetrics creates new NegMetricsCollector with fixed 5 second metricsInterval, to be used in tests
func FakeSyncerMetrics() *SyncerMetrics {
return NewNegMetricsCollector(5*time.Second, klog.TODO())
}

func RegisterSyncerMetrics() {
}

func (sm *SyncerMetrics) Run(stopCh <-chan struct{}) {
sm.logger.V(3).Info("Syncer Metrics initialized.", "exportInterval", sm.metricsInterval)
// Compute and export metrics periodically.
go func() {
time.Sleep(sm.metricsInterval)
wait.Until(sm.export, sm.metricsInterval, stopCh)
}()
<-stopCh
}

// export exports syncer metrics.
func (sm *SyncerMetrics) export() {
}

// UpdateSyncer updates the count of sync results based on the result/error of sync
func (sm *SyncerMetrics) UpdateSyncer(key negtypes.NegSyncerKey, syncResult *negtypes.NegSyncResult) {
}

// SetSyncerEPMetrics update the endpoint count based on the endpointStat
func (sm *SyncerMetrics) SetSyncerEPMetrics(key negtypes.NegSyncerKey, endpointStat *negtypes.SyncerEPStat) {
}
36 changes: 36 additions & 0 deletions pkg/neg/types/endpoint_stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

type State string

func (s State) String() string {
return string(s)
}

// SyncerEPStat contains endpoint and endpointslice status related to a syncer
type SyncerEPStat struct {
EndpointStateCount StateCountMap
EndpointSliceStateCount StateCountMap
}

// StateCountMap collect the count of instances in different states
type StateCountMap map[State]int

func NewSyncerEPStat() *SyncerEPStat {
return &SyncerEPStat{
EndpointStateCount: make(map[State]int),
EndpointSliceStateCount: make(map[State]int),
}
}
26 changes: 26 additions & 0 deletions pkg/neg/types/sync_results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

type NegSyncResult struct {
Error error
Result string
}

func NewNegSyncResult(err error, result string) *NegSyncResult {
return &NegSyncResult{
Error: err,
Result: result,
}
}

0 comments on commit b5315cb

Please sign in to comment.