Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
  • Loading branch information
3 people authored Jan 10, 2024
1 parent af0d2a6 commit 2a08289
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions sdk/trace/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit 2a08289

Please sign in to comment.