-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/util/kubernetes][CONTINT-477] add rate_limit_queries_remaining_m…
…in telemetry in cluster agent external metrics server (#20497) * add rate_limit_queries_remaining_min telemetry in cluster agent external metrics server * added release note * add mutex lock to mrr struct * improve unit test * add a get() method to the minTracker struct * add lock on the get method
- Loading branch information
Showing
4 changed files
with
134 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,47 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2017-present Datadog, Inc. | ||
|
||
//go:build kubeapiserver | ||
|
||
package autoscalers | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type minTracker struct { | ||
sync.Mutex | ||
val int | ||
timestamp time.Time | ||
expiryDuration time.Duration | ||
} | ||
|
||
func newMinTracker(expiryDuration time.Duration) *minTracker { | ||
return &minTracker{ | ||
val: -1, | ||
timestamp: time.Now(), | ||
expiryDuration: expiryDuration, | ||
} | ||
} | ||
|
||
func (mt *minTracker) update(newVal int) { | ||
mt.Lock() | ||
defer mt.Unlock() | ||
|
||
isSet := mt.val >= 0 | ||
hasExpired := time.Since(mt.timestamp) > mt.expiryDuration | ||
|
||
if newVal <= mt.val || !isSet || hasExpired { | ||
mt.val = newVal | ||
mt.timestamp = time.Now() | ||
} | ||
} | ||
|
||
func (mt *minTracker) get() int { | ||
mt.Lock() | ||
defer mt.Unlock() | ||
return mt.val | ||
} |
47 changes: 47 additions & 0 deletions
47
pkg/util/kubernetes/autoscalers/datadogexternal_util_test.go
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,47 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2017-present Datadog, Inc. | ||
|
||
//go:build kubeapiserver | ||
|
||
package autoscalers | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpdateMinTracker(t *testing.T) { | ||
expiryDuration := 60 * time.Second | ||
|
||
mt := newMinTracker(expiryDuration) | ||
|
||
// Should update | ||
mt.update(10) | ||
assert.Equal(t, mt.get(), 10) | ||
|
||
// Should not update, since value didn't expire yet | ||
mt.update(11) | ||
assert.Equal(t, mt.get(), 10) | ||
|
||
// simulate waiting half the expirationDuration | ||
mt.timestamp = time.Now().Add(-expiryDuration / 2) | ||
|
||
// Should not update | ||
mt.update(199) | ||
assert.Equal(t, mt.get(), 10) | ||
|
||
// Shoud update, even if value didn't expire because new value is lower | ||
mt.update(5) | ||
assert.Equal(t, mt.get(), 5) | ||
|
||
// Change timestamp to simulate expiration | ||
mt.timestamp = time.Now().Add(-2 * expiryDuration) | ||
|
||
// Shoud update because current value has expired | ||
mt.update(100) | ||
assert.Equal(t, mt.get(), 100) | ||
} |
11 changes: 11 additions & 0 deletions
11
releasenotes-dca/notes/add-rate_limit_queries_remaining_min_telemetry-233fcfdbc0fe3822.yaml
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,11 @@ | ||
# Each section from every release note are combined when the | ||
# CHANGELOG-DCA.rst is rendered. So the text needs to be worded so that | ||
# it does not depend on any information only available in another | ||
# section. This may mean repeating some details, but each section | ||
# must be readable independently of the other. | ||
# | ||
# Each section note must be formatted as reStructuredText. | ||
--- | ||
features: | ||
- | | ||
Report `rate_limit_queries_remaining_min` telemetry from `external-metrics` server. |