From 77e310eb56174b10954eebfaedeed69921172bb0 Mon Sep 17 00:00:00 2001 From: Andrew Bernat Date: Thu, 18 Jul 2024 18:37:28 -0700 Subject: [PATCH] Add test for cloudwatch2 counter that demonstrates potential slice bug with label-values. Label-values are stored in a Counter as a string slice and copyied over to a new Counter when With(lvs ...string) is invoked. The slice bug can present when the same Counter is copied twice with With() and each invocation does not resize the capacity of the slice. --- metrics/cloudwatch2/cloudwatch2_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/metrics/cloudwatch2/cloudwatch2_test.go b/metrics/cloudwatch2/cloudwatch2_test.go index 841874324..e1e24d4bc 100644 --- a/metrics/cloudwatch2/cloudwatch2_test.go +++ b/metrics/cloudwatch2/cloudwatch2_test.go @@ -2,6 +2,7 @@ package cloudwatch2 import ( "context" + "fmt" "strings" "testing" @@ -137,3 +138,17 @@ func TestSend(t *testing.T) { } } } + +func TestCounterLabelValues(t *testing.T) { + c1 := (&Counter{}).With("a", "1", "b", "2", "c", "3", "d", "4") + c2 := c1.With("e", "5").(*Counter) + c3 := c2.With("f", "6", "g", "7").(*Counter) + c4 := c2.With("h", "8").(*Counter) + + if fmt.Sprintf("%v", c3.lvs) != "[a 1 b 2 c 3 d 4 e 5 f 6 g 7]" { + t.Errorf("expected label values a-g; got %v\n", c3.lvs) + } + if fmt.Sprintf("%v", c4.lvs) != "[a 1 b 2 c 3 d 4 e 5 h 8]" { + t.Errorf("expected label values a-e,h; got %v\n", c4.lvs) + } +}