You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-11-25 22:41:46 +02:00
sdk/trace: use slices.Grow() to avoid excessive runtime.growslice() (#4818)
* sdk/trace: use slices.Grow() to avoid excessive runtime.growslice()
* go1.20 compat
* update go.mod/sum
* Update CHANGELOG.md
* Revert "update go.mod/sum"
This reverts commit 1e4fcfa86c.
* inline slices.Grow as ensureAttributesCapacity
* fix comment
* add bench
* fix inlining
* Apply suggestions from code review
Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
* add ReportAllocs
* add benchmark variations with lower limit
* bugfix: we always want to set cap, just the value is a min()
* lint
---------
Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
@@ -16,6 +16,7 @@ package trace
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@@ -243,3 +244,31 @@ func TestTruncateAttr(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRecordingSpanSetAttributes(b *testing.B) {
|
||||
var attrs []attribute.KeyValue
|
||||
for i := 0; i < 100; i++ {
|
||||
attr := attribute.String(fmt.Sprintf("hello.attrib%d", i), fmt.Sprintf("goodbye.attrib%d", i))
|
||||
attrs = append(attrs, attr)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
for _, limit := range []bool{false, true} {
|
||||
b.Run(fmt.Sprintf("WithLimit/%t", limit), func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
sl := NewSpanLimits()
|
||||
if limit {
|
||||
sl.AttributeCountLimit = 50
|
||||
}
|
||||
tp := NewTracerProvider(WithSampler(AlwaysSample()), WithSpanLimits(sl))
|
||||
tracer := tp.Tracer("tracer")
|
||||
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, span := tracer.Start(ctx, "span")
|
||||
span.SetAttributes(attrs...)
|
||||
span.End()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user