1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00
Files
opentelemetry-go/attribute/value_test.go
T
Nesterov Yehor 5e9a80b3ce attribute: add BYTESLICE type support (#7948)
Fixes #7933 

Add BYTES type to https://pkg.go.dev/go.opentelemetry.io/otel/attribute

- Introduces BYTES type and byte 
- Adds Bytes / BytesValue constructors
- Implements hashing support
- Adds base64 representation in Emit()
- Adds test coverage for constructors, hashing, and set equality

```
$ go test -run=^$ -bench=BenchmarkByteSlice
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/attribute
cpu: 13th Gen Intel(R) Core(TM) i7-13800H
BenchmarkByteSlice/Value-20             149529567                7.993 ns/op           0 B/op          0 allocs/op
BenchmarkByteSlice/KeyValue-20          136973736                8.768 ns/op           0 B/op          0 allocs/op
BenchmarkByteSlice/AsByteSlice-20       562915658                2.120 ns/op           0 B/op          0 allocs/op
BenchmarkByteSlice/Emit-20              29149410                40.26 ns/op           16 B/op          1 allocs/op
PASS
```

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Robert Pająk <pellared@hotmail.com>
2026-04-07 09:39:32 +02:00

338 lines
9.1 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 TestValue(t *testing.T) {
k := attribute.Key("test")
for _, testcase := range []struct {
name string
value attribute.Value
wantType attribute.Type
wantValue any
}{
{
name: "Key.Bool() correctly returns keys's internal bool value",
value: k.Bool(true).Value,
wantType: attribute.BOOL,
wantValue: true,
},
{
name: "Key.BoolSlice() correctly returns keys's internal []bool value",
value: k.BoolSlice([]bool{true, false, true}).Value,
wantType: attribute.BOOLSLICE,
wantValue: []bool{true, false, true},
},
{
name: "Key.Int64() correctly returns keys's internal int64 value",
value: k.Int64(42).Value,
wantType: attribute.INT64,
wantValue: int64(42),
},
{
name: "Key.Int64() correctly returns negative keys's internal int64 value",
value: k.Int64(-42).Value,
wantType: attribute.INT64,
wantValue: int64(-42),
},
{
name: "Key.Int64Slice() correctly returns keys's internal []int64 value",
value: k.Int64Slice([]int64{42, -3, 12}).Value,
wantType: attribute.INT64SLICE,
wantValue: []int64{42, -3, 12},
},
{
name: "Key.Int() correctly returns keys's internal signed integral value",
value: k.Int(42).Value,
wantType: attribute.INT64,
wantValue: int64(42),
},
{
name: "Key.IntSlice() correctly returns keys's internal []int64 value",
value: k.IntSlice([]int{42, -3, 12}).Value,
wantType: attribute.INT64SLICE,
wantValue: []int64{42, -3, 12},
},
{
name: "Key.Float64() correctly returns keys's internal float64 value",
value: k.Float64(42.1).Value,
wantType: attribute.FLOAT64,
wantValue: 42.1,
},
{
name: "Key.Float64Slice() correctly returns keys's internal []float64 value",
value: k.Float64Slice([]float64{42, -3, 12}).Value,
wantType: attribute.FLOAT64SLICE,
wantValue: []float64{42, -3, 12},
},
{
name: "Key.String() correctly returns keys's internal string value",
value: k.String("foo").Value,
wantType: attribute.STRING,
wantValue: "foo",
},
{
name: "Key.StringSlice() correctly returns keys's internal []string value",
value: k.StringSlice([]string{"forty-two", "negative three", "twelve"}).Value,
wantType: attribute.STRINGSLICE,
wantValue: []string{"forty-two", "negative three", "twelve"},
},
{
name: "Key.ByteSlice() correctly returns keys's internal []byte value",
value: k.ByteSlice([]byte("hello world")).Value,
wantType: attribute.BYTESLICE,
wantValue: []byte("hello world"),
},
{
name: "empty value",
value: attribute.Value{},
wantType: attribute.EMPTY,
wantValue: nil,
},
} {
t.Logf("Running test case %s", testcase.name)
if testcase.value.Type() != testcase.wantType {
t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType)
}
got := testcase.value.AsInterface()
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
t.Errorf("+got, -want: %s", diff)
}
}
}
func TestEquivalence(t *testing.T) {
pairs := [][2]attribute.KeyValue{
{
attribute.Bool("Bool", true),
attribute.Bool("Bool", true),
},
{
attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
},
{
attribute.Int("Int", 34),
attribute.Int("Int", 34),
},
{
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
},
{
attribute.Int64("Int64", 98),
attribute.Int64("Int64", 98),
},
{
attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
},
{
attribute.Float64("Float64", 19.09),
attribute.Float64("Float64", 19.09),
},
{
attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
},
{
attribute.String("String", "string value"),
attribute.String("String", "string value"),
},
{
attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
},
{
attribute.ByteSlice("ByteSlice", []byte("one")),
attribute.ByteSlice("ByteSlice", []byte("one")),
},
{
attribute.KeyValue{Key: "Empty"},
attribute.KeyValue{Key: "Empty"},
},
}
t.Run("Distinct", func(t *testing.T) {
for _, p := range pairs {
s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
m := map[attribute.Distinct]struct{}{s0.Equivalent(): {}}
_, ok := m[s1.Equivalent()]
assert.Truef(
t,
ok,
"Distinct comparison of %s type: not equivalent: %s != %s",
p[0].Value.Type(),
s0.Encoded(attribute.DefaultEncoder()),
s1.Encoded(attribute.DefaultEncoder()),
)
}
})
t.Run("Equality operator", func(t *testing.T) {
// Maintain backwards compatibility.
for _, p := range pairs {
if p[0] != p[1] {
t.Errorf("Expected %v to be equal to %v", p[0], p[1])
}
}
})
t.Run("Set", func(t *testing.T) {
// Maintain backwards compatibility.
for _, p := range pairs {
s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
m := map[attribute.Set]struct{}{s0: {}}
_, ok := m[s1]
assert.Truef(
t,
ok,
"Set comparison of %s type: not equivalent: %s != %s",
p[0].Value.Type(),
s0.Encoded(attribute.DefaultEncoder()),
s1.Encoded(attribute.DefaultEncoder()),
)
}
})
}
func TestNotEquivalence(t *testing.T) {
pairs := [][2]attribute.KeyValue{
{
attribute.Int("Key", 0),
attribute.Bool("Key", false),
},
{
attribute.Bool("Bool", true),
attribute.Bool("Bool", false),
},
{
attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
attribute.BoolSlice("BoolSlice", []bool{true, true, true}),
},
{
attribute.Int("Int", 34),
attribute.Int("Int", 32),
},
{
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
attribute.IntSlice("IntSlice", []int{312, 2, -2}),
},
{
attribute.Int64("Int64", 98),
attribute.Int64("Int64", 97),
},
{
attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 1}),
},
{
attribute.Float64("Float64", 19.09),
attribute.Float64("Float64", 22.09),
},
{
attribute.ByteSlice("ByteSlice", []byte("bytes value")),
attribute.ByteSlice("ByteSlice", []byte("another value")),
},
{
attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 5}),
},
{
attribute.String("String", "string value"),
attribute.String("String", "another value"),
},
{
attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
attribute.StringSlice("StringSlice", []string{"one", "two"}),
},
{
attribute.KeyValue{Key: "Empty"},
attribute.String("Empty", ""),
},
}
t.Run("Distinct", func(t *testing.T) {
for _, p := range pairs {
s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
m := map[attribute.Distinct]struct{}{s0.Equivalent(): {}}
_, ok := m[s1.Equivalent()]
assert.Falsef(
t,
ok,
"Distinct comparison of %s type: equivalent: %s == %s",
p[0].Value.Type(),
s0.Encoded(attribute.DefaultEncoder()),
s1.Encoded(attribute.DefaultEncoder()),
)
}
})
t.Run("Equality operator", func(t *testing.T) {
// Maintain backwards compatibility.
for _, p := range pairs {
if p[0] == p[1] {
t.Errorf("Expected %v to not be equal to %v", p[0], p[1])
}
}
})
t.Run("Set", func(t *testing.T) {
// Maintain backwards compatibility.
for _, p := range pairs {
s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
m := map[attribute.Set]struct{}{s0: {}}
_, ok := m[s1]
assert.Falsef(
t,
ok,
"Set comparison of %s type: equivalent: %s == %s",
p[0].Value.Type(),
s0.Encoded(attribute.DefaultEncoder()),
s1.Encoded(attribute.DefaultEncoder()),
)
}
})
}
func TestAsSlice(t *testing.T) {
bs1 := []bool{true, false, true}
kv := attribute.BoolSlice("BoolSlice", bs1)
bs2 := kv.Value.AsBoolSlice()
assert.Equal(t, bs1, bs2)
i64s1 := []int64{12, 1298, -219, 2}
kv = attribute.Int64Slice("Int64Slice", i64s1)
i64s2 := kv.Value.AsInt64Slice()
assert.Equal(t, i64s1, i64s2)
is1 := []int{12, 1298, -219, 2}
kv = attribute.IntSlice("IntSlice", is1)
i64s2 = kv.Value.AsInt64Slice()
assert.Equal(t, i64s1, i64s2)
fs1 := []float64{12398.1, -37.1713873737, 3}
kv = attribute.Float64Slice("Float64Slice", fs1)
fs2 := kv.Value.AsFloat64Slice()
assert.Equal(t, fs1, fs2)
ss1 := []string{"one", "two", "three"}
kv = attribute.StringSlice("StringSlice", ss1)
ss2 := kv.Value.AsStringSlice()
assert.Equal(t, ss1, ss2)
b1 := []byte("one")
kv = attribute.ByteSlice("ByteSlice", b1)
b2 := kv.Value.AsByteSlice()
assert.Equal(t, b1, b2)
}