Skip to content

Commit

Permalink
Create NEG metrics collector interface
Browse files Browse the repository at this point in the history
Create NEG metris collector that would periodically emits NEG syncer
metrics.
  • Loading branch information
sawsa307 committed Feb 5, 2023
1 parent e246ee3 commit 3926dfc
Show file tree
Hide file tree
Showing 2 changed files with 70 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
68 changes: 68 additions & 0 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
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"
"k8s.io/klog/v2"
)

type SyncerMetricsCollector interface {
}

type SyncerMetrics struct {
// 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{
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())
}

// RegisterSyncerMetrics registers syncer related metrics
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() {
}

0 comments on commit 3926dfc

Please sign in to comment.