Skip to content

Commit

Permalink
Merge branch 'main' into processor-example
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Jun 27, 2024
2 parents c758602 + f3a2d96 commit d88cc2f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- Improved performance in all `{Bool,Int64,Float64,String}SliceValue` function of `go.opentelemetry.io/attributes` by reducing the number of allocations. (#5549)
- Retry trace and span ID generation if it generated an invalid one in `go.opentelemetry.io/otel/sdk/trace`. (#5514)
- Log a warning to the OpenTelemetry internal logger when a `Record` in `go.opentelemetry.io/otel/sdk/log` drops an attribute due to a limit being reached. (#5376)
- Identify the `Tracer` returned from the global `TracerProvider` in `go.opentelemetry.io/otel/global` with its schema URL. (#5426)
Expand Down
24 changes: 12 additions & 12 deletions internal/attribute/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ import (
// BoolSliceValue converts a bool slice into an array with same elements as slice.
func BoolSliceValue(v []bool) interface{} {
var zero bool
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]bool), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// Int64SliceValue converts an int64 slice into an array with same elements as slice.
func Int64SliceValue(v []int64) interface{} {
var zero int64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]int64), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// Float64SliceValue converts a float64 slice into an array with same elements as slice.
func Float64SliceValue(v []float64) interface{} {
var zero float64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]float64), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// StringSliceValue converts a string slice into an array with same elements as slice.
func StringSliceValue(v []string) interface{} {
var zero string
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]string), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// AsBoolSlice converts a bool array into a slice into with same elements as array.
Expand Down
39 changes: 39 additions & 0 deletions internal/attribute/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,42 @@ func TestSliceValue(t *testing.T) {
})
}
}

// sync is a global used to ensure the benchmark are not optimized away.
var sync any

func BenchmarkBoolSliceValue(b *testing.B) {
b.ReportAllocs()
s := []bool{true, false, true, false}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = BoolSliceValue(s)
}
}

func BenchmarkInt64SliceValue(b *testing.B) {
b.ReportAllocs()
s := []int64{1, 2, 3, 4}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = Int64SliceValue(s)
}
}

func BenchmarkFloat64SliceValue(b *testing.B) {
b.ReportAllocs()
s := []float64{1.2, 3.4, 5.6, 7.8}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = Float64SliceValue(s)
}
}

func BenchmarkStringSliceValue(b *testing.B) {
b.ReportAllocs()
s := []string{"a", "b", "c", "d"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = StringSliceValue(s)
}
}
21 changes: 21 additions & 0 deletions sdk/log/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package log // import "go.opentelemetry.io/otel/sdk/log"

import (
"context"
"fmt"
"strconv"
"sync"
"testing"
Expand All @@ -17,6 +18,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/noop"
"go.opentelemetry.io/otel/sdk/resource"
)
Expand Down Expand Up @@ -287,3 +289,22 @@ func TestLoggerProviderForceFlush(t *testing.T) {
assert.ErrorIs(t, p.ForceFlush(ctx), assert.AnError, "processor error not returned")
})
}

func BenchmarkLoggerProviderLogger(b *testing.B) {
p := NewLoggerProvider()
names := make([]string, b.N)
for i := 0; i < b.N; i++ {
names[i] = fmt.Sprintf("%d logger", i)
}

b.ResetTimer()
b.ReportAllocs()

loggers := make([]log.Logger, b.N)
for i := 0; i < b.N; i++ {
loggers[i] = p.Logger(names[i])
}

b.StopTimer()
loggers[0].Enabled(context.Background(), log.Record{})
}
55 changes: 48 additions & 7 deletions sdk/log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,55 @@ func TestTruncate(t *testing.T) {
}
}

func BenchmarkWalkAttributes(b *testing.B) {
for _, tt := range []struct {
attrCount int
}{
{attrCount: 1},
{attrCount: 10},
{attrCount: 100},
{attrCount: 1000},
} {
b.Run(fmt.Sprintf("%d attributes", tt.attrCount), func(b *testing.B) {
record := &Record{}
for i := 0; i < tt.attrCount; i++ {
record.SetAttributes(
log.String(fmt.Sprintf("key-%d", tt.attrCount), "value"),
)
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
record.WalkAttributes(func(log.KeyValue) bool {
return true
})
}
})
}
}

func BenchmarkSetAddAttributes(b *testing.B) {
kv := log.String("key", "value")
records := make([]Record, b.N)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
records[i].SetAttributes(kv)
records[i].AddAttributes(kv)
}
b.Run("SetAttributes", func(b *testing.B) {
records := make([]Record, b.N)

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
records[i].SetAttributes(kv)
}
})

b.Run("AddAttributes", func(b *testing.B) {
records := make([]Record, b.N)

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
records[i].AddAttributes(kv)
}
})
}

0 comments on commit d88cc2f

Please sign in to comment.