You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2026-06-03 18:35:08 +02:00
d13f8ecb2d
Fixes https://github.com/open-telemetry/opentelemetry-go/issues/7934 ``` $ go test -run=^$ -bench=BenchmarkSlice goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/attribute cpu: 13th Gen Intel(R) Core(TM) i7-13800H BenchmarkSlice/Len3/Value-20 25297926 52.56 ns/op 144 B/op 1 allocs/op BenchmarkSlice/Len3/KeyValue-20 21315132 55.97 ns/op 144 B/op 1 allocs/op BenchmarkSlice/Len3/AsSlice-20 24214248 50.03 ns/op 144 B/op 1 allocs/op BenchmarkSlice/Len3/String-20 14148270 86.48 ns/op 48 B/op 1 allocs/op BenchmarkSlice/Len3/Emit-20 13605388 85.18 ns/op 48 B/op 1 allocs/op BenchmarkSlice/Len5Nested/Value-20 16086171 71.30 ns/op 240 B/op 1 allocs/op BenchmarkSlice/Len5Nested/KeyValue-20 15547844 75.81 ns/op 240 B/op 1 allocs/op BenchmarkSlice/Len5Nested/AsSlice-20 17806996 66.16 ns/op 240 B/op 1 allocs/op BenchmarkSlice/Len5Nested/String-20 7409064 165.2 ns/op 64 B/op 1 allocs/op BenchmarkSlice/Len5Nested/Emit-20 7666302 161.0 ns/op 64 B/op 1 allocs/op PASS ok go.opentelemetry.io/otel/attribute 12.980s ``` ``` $ go test -run=^$ -bench=BenchmarkHashKVs goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/attribute cpu: 13th Gen Intel(R) Core(TM) i7-13800H BenchmarkHashKVs-20 1268742 940.5 ns/op 0 B/op 0 allocs/op PASS ok go.opentelemetry.io/otel/attribute 1.198s ```
212 lines
4.4 KiB
Go
212 lines
4.4 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package attribute_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
)
|
|
|
|
func TestKeyValueConstructors(t *testing.T) {
|
|
tt := []struct {
|
|
name string
|
|
actual attribute.KeyValue
|
|
expected attribute.KeyValue
|
|
}{
|
|
{
|
|
name: "Bool",
|
|
actual: attribute.Bool("k1", true),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.BoolValue(true),
|
|
},
|
|
},
|
|
{
|
|
name: "Int64",
|
|
actual: attribute.Int64("k1", 123),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.Int64Value(123),
|
|
},
|
|
},
|
|
{
|
|
name: "Float64",
|
|
actual: attribute.Float64("k1", 123.5),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.Float64Value(123.5),
|
|
},
|
|
},
|
|
{
|
|
name: "String",
|
|
actual: attribute.String("k1", "123.5"),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.StringValue("123.5"),
|
|
},
|
|
},
|
|
{
|
|
name: "Int",
|
|
actual: attribute.Int("k1", 123),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.IntValue(123),
|
|
},
|
|
},
|
|
{
|
|
name: "ByteSlice",
|
|
actual: attribute.ByteSlice("k1", []byte{123}),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.ByteSliceValue([]byte{123}),
|
|
},
|
|
},
|
|
{
|
|
name: "Slice",
|
|
actual: attribute.Slice("k1", attribute.BoolValue(true), attribute.IntValue(42)),
|
|
expected: attribute.KeyValue{
|
|
Key: "k1",
|
|
Value: attribute.SliceValue(attribute.BoolValue(true), attribute.IntValue(42)),
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tt {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if diff := cmp.Diff(test.actual, test.expected, cmp.AllowUnexported(attribute.Value{})); diff != "" {
|
|
t.Fatal(diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestKeyValueValid(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
valid bool
|
|
kv attribute.KeyValue
|
|
}{
|
|
{
|
|
desc: "uninitialized KeyValue should be invalid",
|
|
valid: false,
|
|
kv: attribute.KeyValue{},
|
|
},
|
|
{
|
|
desc: "empty key value should be invalid",
|
|
valid: false,
|
|
kv: attribute.Key("").Bool(true),
|
|
},
|
|
{
|
|
desc: "EMPTY value type should be valid",
|
|
valid: true,
|
|
kv: attribute.KeyValue{
|
|
Key: attribute.Key("valid key"),
|
|
// Default type is EMPTY.
|
|
Value: attribute.Value{},
|
|
},
|
|
},
|
|
{
|
|
desc: "non-empty key with BOOL type Value should be valid",
|
|
valid: true,
|
|
kv: attribute.Bool("bool", true),
|
|
},
|
|
{
|
|
desc: "non-empty key with INT64 type Value should be valid",
|
|
valid: true,
|
|
kv: attribute.Int64("int64", 0),
|
|
},
|
|
{
|
|
desc: "non-empty key with FLOAT64 type Value should be valid",
|
|
valid: true,
|
|
kv: attribute.Float64("float64", 0),
|
|
},
|
|
{
|
|
desc: "non-empty key with STRING type Value should be valid",
|
|
valid: true,
|
|
kv: attribute.String("string", ""),
|
|
},
|
|
{
|
|
desc: "non-empty key with BYTESLICE type Value should be valid",
|
|
valid: true,
|
|
kv: attribute.ByteSlice("bytes", []byte{}),
|
|
},
|
|
{
|
|
desc: "non-empty key with SLICE type Value should be valid",
|
|
valid: true,
|
|
kv: attribute.Slice("slice", attribute.StringValue("value")),
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
if got, want := test.kv.Valid(), test.valid; got != want {
|
|
t.Error(test.desc)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIncorrectCast(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
val attribute.Value
|
|
}{
|
|
{
|
|
name: "Float64",
|
|
val: attribute.Float64Value(1.0),
|
|
},
|
|
{
|
|
name: "Int64",
|
|
val: attribute.Int64Value(2),
|
|
},
|
|
{
|
|
name: "String",
|
|
val: attribute.BoolValue(true),
|
|
},
|
|
{
|
|
name: "Float64Slice",
|
|
val: attribute.Float64SliceValue([]float64{1.0}),
|
|
},
|
|
{
|
|
name: "Int64Slice",
|
|
val: attribute.Int64SliceValue([]int64{2}),
|
|
},
|
|
{
|
|
name: "StringSlice",
|
|
val: attribute.BoolSliceValue([]bool{true}),
|
|
},
|
|
{
|
|
name: "ByteSlice",
|
|
val: attribute.ByteSliceValue([]byte{123}),
|
|
},
|
|
{
|
|
name: "Empty",
|
|
val: attribute.Value{},
|
|
},
|
|
{
|
|
name: "Slice",
|
|
val: attribute.SliceValue(attribute.StringValue("value")),
|
|
},
|
|
}
|
|
for _, tt := range testCases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert.NotPanics(t, func() {
|
|
tt.val.AsBool()
|
|
tt.val.AsBoolSlice()
|
|
tt.val.AsFloat64()
|
|
tt.val.AsFloat64Slice()
|
|
tt.val.AsInt64()
|
|
tt.val.AsInt64Slice()
|
|
tt.val.AsInterface()
|
|
tt.val.AsSlice()
|
|
tt.val.AsString()
|
|
tt.val.AsStringSlice()
|
|
tt.val.AsByteSlice()
|
|
})
|
|
})
|
|
}
|
|
}
|