From 2a08289fa161d69b04280f0dc9b36f0627036c78 Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Wed, 10 Jan 2024 13:41:22 -0800 Subject: [PATCH] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert PajÄ…k Co-authored-by: Damien Mathieu <42@dmathieu.com> --- sdk/trace/span.go | 7 ++++--- sdk/trace/span_test.go | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sdk/trace/span.go b/sdk/trace/span.go index b723de3e8aa..f8c031bd070 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -208,9 +208,10 @@ func (s *recordingSpan) SetStatus(code codes.Code, description string) { s.status = status } -// ensureAttributesCapacity inlines functionality from golang.org/x/exp/slices.Grow +// ensureAttributesCapacity inlines functionality from slices.Grow // so that we can avoid needing to import golang.org/x/exp for go1.20. -// Once support for go1.20 is dropped, we can use slices.Grow in 1.21+ instead. +// Once support for go1.20 is dropped, we can use slices.Grow available since go1.21 instead. +// Tracking issue: https://github.com/open-telemetry/opentelemetry-go/issues/4819. func (s *recordingSpan) ensureAttributesCapacity(minCapacity int) { if n := minCapacity - cap(s.attributes); n > 0 { s.attributes = append(s.attributes[:cap(s.attributes)], make([]attribute.KeyValue, n)...)[:len(s.attributes)] @@ -287,7 +288,7 @@ func (s *recordingSpan) addOverCapAttrs(limit int, attrs []attribute.KeyValue) { // Now that s.attributes is deduplicated, adding unique attributes up to // the capacity of s will not over allocate s.attributes. - if limit-len(s.attributes) > 0 { + if len(s.attributes) > limit { s.ensureAttributesCapacity(limit) } for _, a := range attrs { diff --git a/sdk/trace/span_test.go b/sdk/trace/span_test.go index c1f828097b3..e08817019d3 100644 --- a/sdk/trace/span_test.go +++ b/sdk/trace/span_test.go @@ -255,6 +255,7 @@ func BenchmarkRecordingSpanSetAttributes(b *testing.B) { } ctx := context.Background() + b.ResetTimer() for n := 0; n < b.N; n++ { _, span := tracer.Start(ctx, "span") span.SetAttributes(attrs...)