From 393634f9475277d9f7bd2e5f732784168eafe404 Mon Sep 17 00:00:00 2001 From: Jonathan Lee Date: Mon, 27 Mar 2023 15:28:58 -0700 Subject: [PATCH 1/3] do not borrow from reservoir if there is no reservoir capacity --- samplers/aws/xray/internal/reservoir.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samplers/aws/xray/internal/reservoir.go b/samplers/aws/xray/internal/reservoir.go index 40d32f3402e..957b1f976c2 100644 --- a/samplers/aws/xray/internal/reservoir.go +++ b/samplers/aws/xray/internal/reservoir.go @@ -59,6 +59,10 @@ func (r *reservoir) take(now time.Time, borrowed bool, itemCost float64) bool { r.mu.Lock() defer r.mu.Unlock() + if r.capacity == 0 { + return false + } + if r.lastTick.IsZero() { r.lastTick = now From b1b72cdf0c7590cf22d543e440220bac7da79c0f Mon Sep 17 00:00:00 2001 From: Jonathan Lee Date: Thu, 6 Apr 2023 17:26:59 -0700 Subject: [PATCH 2/3] add and edit tests --- samplers/aws/xray/internal/reservoir_test.go | 24 ++++++++++++++++++++ samplers/aws/xray/internal/rule_test.go | 1 + 2 files changed, 25 insertions(+) diff --git a/samplers/aws/xray/internal/reservoir_test.go b/samplers/aws/xray/internal/reservoir_test.go index 72e0f7cd24c..a022d650c77 100644 --- a/samplers/aws/xray/internal/reservoir_test.go +++ b/samplers/aws/xray/internal/reservoir_test.go @@ -161,6 +161,30 @@ func TestConsumeFromReservoir(t *testing.T) { assert.Equal(t, r.quotaBalance, 7.0) } +func TestZeroCapacityFailBorrow(t *testing.T) { + clock := &mockClock{ + nowTime: 1500000000, + } + + r := &reservoir{ + quota: 0, + capacity: 0, + } + + // start with no quota balance + assert.Equal(t, r.quotaBalance, 0.0) + // attempt to borrow from reservoir, and should fail since there is no capacity + assert.False(t, r.take(clock.now(), true, 1.0)) + + // increase the clock by 5 + clock.nowTime = 1500000005 + + // validate there is still no quota balance + assert.Equal(t, r.quotaBalance, 0.0) + // again, attempt to borrow from reservoir, and should fail since there is no capacity + assert.False(t, r.take(clock.now(), true, 1.0)) +} + func TestResetQuotaUsageRotation(t *testing.T) { clock := &mockClock{ nowTime: 1500000000, diff --git a/samplers/aws/xray/internal/rule_test.go b/samplers/aws/xray/internal/rule_test.go index 6bdc726f90b..ad99cc9a166 100644 --- a/samplers/aws/xray/internal/rule_test.go +++ b/samplers/aws/xray/internal/rule_test.go @@ -162,6 +162,7 @@ func TestConsumeFromReservoirSample(t *testing.T) { RuleName: "r1", }, reservoir: &reservoir{ + capacity: 10, quota: 10, expiresAt: time.Unix(1500000060, 0), }, From ea67a86f64cc3e3f6310e92734e9e237957e0920 Mon Sep 17 00:00:00 2001 From: Jonathan Lee Date: Thu, 6 Apr 2023 17:42:59 -0700 Subject: [PATCH 3/3] add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e7903315a6..f839406c088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108) +### Fixed + +- Prevent taking from reservoir in AWS XRay Remote Sampler when there is zero capacity in `go.opentelemetry.io/contrib/samplers/aws/xray`. (#3684) + ## [1.16.0-rc.2/0.41.0-rc.2/0.10.0-rc.2] - 2023-03-23 ### Added