Skip to content

Commit

Permalink
add a get() method to the minTracker struct
Browse files Browse the repository at this point in the history
  • Loading branch information
adel121 committed Nov 7, 2023
1 parent 081c5bc commit 62f5276
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/util/kubernetes/autoscalers/datadogexternal.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (p *Processor) queryDatadogExternal(ddQueries []string, timeWindow time.Dur
newVal, err := strconv.Atoi(queryLimits.Remaining)
if err == nil {
getMinRemainingRequestsTracker().update(newVal)
rateLimitsRemainingMin.Set(float64(minRemainingRequestsTracker.val), queryEndpoint, le.JoinLeaderLabel)
rateLimitsRemainingMin.Set(float64(minRemainingRequestsTracker.get()), queryEndpoint, le.JoinLeaderLabel)
}

return processedMetrics, nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/kubernetes/autoscalers/datadogexternal_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (mt *minTracker) update(newVal int) {
mt.timestamp = time.Now()
}
}

func (mt *minTracker) get() int {
return mt.val
}
28 changes: 14 additions & 14 deletions pkg/util/kubernetes/autoscalers/datadogexternal_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@ import (
"github.com/stretchr/testify/assert"
)

func TestUpdateMinimumRemainingRequests(t *testing.T) {
func TestUpdateMinTracker(t *testing.T) {
expiryDuration := 60 * time.Second

mrr := newMinTracker(expiryDuration)
mt := newMinTracker(expiryDuration)

// Should update
mrr.update(10)
assert.Equal(t, mrr.val, 10)
mt.update(10)
assert.Equal(t, mt.get(), 10)

// Should not update, since value didn't expire yet
mrr.update(11)
assert.Equal(t, mrr.val, 10)
mt.update(11)
assert.Equal(t, mt.get(), 10)

// simulate waiting half the expirationDuration
mrr.timestamp = time.Now().Add(-expiryDuration / 2)
mt.timestamp = time.Now().Add(-expiryDuration / 2)

// Should not update
mrr.update(199)
assert.Equal(t, mrr.val, 10)
mt.update(199)
assert.Equal(t, mt.get(), 10)

// Shoud update, even if value didn't expire because new value is lower
mrr.update(5)
assert.Equal(t, mrr.val, 5)
mt.update(5)
assert.Equal(t, mt.get(), 5)

// Change timestamp to simulate expiration
mrr.timestamp = time.Now().Add(-2 * expiryDuration)
mt.timestamp = time.Now().Add(-2 * expiryDuration)

// Shoud update because current value has expired
mrr.update(100)
assert.Equal(t, mrr.val, 100)
mt.update(100)
assert.Equal(t, mt.get(), 100)
}

0 comments on commit 62f5276

Please sign in to comment.