Skip to content

Commit eb48df8

Browse files
committed
Improve metrics testability
1 parent 0bcb2bb commit eb48df8

File tree

5 files changed

+942
-590
lines changed

5 files changed

+942
-590
lines changed

cluster-autoscaler/core/scaledown/nodeevaltracker/max_node_skip_eval_time.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@ import (
2222
"k8s.io/autoscaler/cluster-autoscaler/metrics"
2323
)
2424

25+
type metricObserver interface {
26+
ObserveMaxNodeSkipEvalDurationSeconds(duration time.Duration)
27+
}
28+
2529
// MaxNodeSkipEvalTime is a time tracker for the biggest evaluation time of a node during ScaleDown
2630
type MaxNodeSkipEvalTime struct {
2731
// lastEvalTime is the time of previous currentlyUnneededNodeNames parsing
2832
lastEvalTime time.Time
2933
// nodeNamesWithTimeStamps is maps of nodeNames with their time of last successful evaluation
3034
nodeNamesWithTimeStamps map[string]time.Time
35+
36+
metrics metricObserver
3137
}
3238

3339
// NewMaxNodeSkipEvalTime returns LongestNodeScaleDownEvalTime with lastEvalTime set to currentTime
3440
func NewMaxNodeSkipEvalTime(currentTime time.Time) *MaxNodeSkipEvalTime {
35-
return &MaxNodeSkipEvalTime{lastEvalTime: currentTime}
41+
return newMaxNodeSkipEvalTime(currentTime, metrics.DefaultMetrics)
42+
}
43+
44+
// NewMaxNodeSkipEvalTime returns LongestNodeScaleDownEvalTime with lastEvalTime set to currentTime
45+
func newMaxNodeSkipEvalTime(currentTime time.Time, metrics metricObserver) *MaxNodeSkipEvalTime {
46+
return &MaxNodeSkipEvalTime{lastEvalTime: currentTime, metrics: metrics}
3647
}
3748

3849
// Retrieves the time of the last evaluation of a node.
@@ -65,6 +76,6 @@ func (l *MaxNodeSkipEvalTime) Update(nodeNames []string, currentTime time.Time)
6576
l.lastEvalTime = currentTime
6677
minimumTime := l.getMin()
6778
longestDuration := currentTime.Sub(minimumTime)
68-
metrics.ObserveMaxNodeSkipEvalDurationSeconds(longestDuration)
79+
l.metrics.ObserveMaxNodeSkipEvalDurationSeconds(longestDuration)
6980
return longestDuration
7081
}

cluster-autoscaler/core/scaledown/nodeevaltracker/max_node_skip_eval_time_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ import (
2121
"time"
2222

2323
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/mock"
2425
)
2526

27+
type mockMetrics struct {
28+
mock.Mock
29+
}
30+
31+
func (m *mockMetrics) ObserveMaxNodeSkipEvalDurationSeconds(duration time.Duration) {
32+
m.Called(duration)
33+
}
34+
2635
func TestMaxNodeSkipEvalTime(t *testing.T) {
2736
type testCase struct {
2837
name string
@@ -66,11 +75,15 @@ func TestMaxNodeSkipEvalTime(t *testing.T) {
6675
t.Run(tc.name, func(t *testing.T) {
6776
t.Parallel()
6877
timestamp := start
69-
maxNodeSkipEvalTime := NewMaxNodeSkipEvalTime(start)
78+
mockMetrics := &mockMetrics{}
79+
maxNodeSkipEvalTime := newMaxNodeSkipEvalTime(start, mockMetrics)
80+
mockMetrics.On("ObserveMaxNodeSkipEvalDurationSeconds", mock.Anything).Return()
81+
7082
for i := 0; i < len(tc.unprocessedNodes); i++ {
7183
timestamp = timestamp.Add(1 * time.Second)
7284
assert.Equal(t, time.Duration(tc.wantMaxSkipEvalTimeSeconds[i])*time.Second, maxNodeSkipEvalTime.Update(tc.unprocessedNodes[i], timestamp))
7385
assert.Equal(t, len(tc.unprocessedNodes[i]), len(maxNodeSkipEvalTime.nodeNamesWithTimeStamps))
86+
mockMetrics.AssertCalled(t, "ObserveMaxNodeSkipEvalDurationSeconds", time.Duration(tc.wantMaxSkipEvalTimeSeconds[i])*time.Second)
7487
}
7588
})
7689
}

0 commit comments

Comments
 (0)