Skip to content

Commit 5f7e6ad

Browse files
authored
Disable "taking" from reservoir if there is no capacity, regardless of borrow value (#3684)
* do not borrow from reservoir if there is no reservoir capacity * add and edit tests * add changelog
1 parent ad0aa62 commit 5f7e6ad

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1313
- AWS SDK add `rpc.system` attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#3582, #3617)
1414
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
1515

16+
### Fixed
17+
18+
- Prevent taking from reservoir in AWS XRay Remote Sampler when there is zero capacity in `go.opentelemetry.io/contrib/samplers/aws/xray`. (#3684)
19+
1620
## [1.16.0-rc.2/0.41.0-rc.2/0.10.0-rc.2] - 2023-03-23
1721

1822
### Added

samplers/aws/xray/internal/reservoir.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (r *reservoir) take(now time.Time, borrowed bool, itemCost float64) bool {
5959
r.mu.Lock()
6060
defer r.mu.Unlock()
6161

62+
if r.capacity == 0 {
63+
return false
64+
}
65+
6266
if r.lastTick.IsZero() {
6367
r.lastTick = now
6468

samplers/aws/xray/internal/reservoir_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ func TestConsumeFromReservoir(t *testing.T) {
161161
assert.Equal(t, r.quotaBalance, 7.0)
162162
}
163163

164+
func TestZeroCapacityFailBorrow(t *testing.T) {
165+
clock := &mockClock{
166+
nowTime: 1500000000,
167+
}
168+
169+
r := &reservoir{
170+
quota: 0,
171+
capacity: 0,
172+
}
173+
174+
// start with no quota balance
175+
assert.Equal(t, r.quotaBalance, 0.0)
176+
// attempt to borrow from reservoir, and should fail since there is no capacity
177+
assert.False(t, r.take(clock.now(), true, 1.0))
178+
179+
// increase the clock by 5
180+
clock.nowTime = 1500000005
181+
182+
// validate there is still no quota balance
183+
assert.Equal(t, r.quotaBalance, 0.0)
184+
// again, attempt to borrow from reservoir, and should fail since there is no capacity
185+
assert.False(t, r.take(clock.now(), true, 1.0))
186+
}
187+
164188
func TestResetQuotaUsageRotation(t *testing.T) {
165189
clock := &mockClock{
166190
nowTime: 1500000000,

samplers/aws/xray/internal/rule_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func TestConsumeFromReservoirSample(t *testing.T) {
162162
RuleName: "r1",
163163
},
164164
reservoir: &reservoir{
165+
capacity: 10,
165166
quota: 10,
166167
expiresAt: time.Unix(1500000060, 0),
167168
},

0 commit comments

Comments
 (0)