-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create NEG metrics collector interface
Create NEG metris collector that would periodically emits NEG syncer metrics.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
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) { | ||
} |