2020-05-14 01:06:03 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 08:05:28 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-05-14 01:06:03 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
package attribute_test
|
2020-05-14 01:06:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-10-13 16:34:02 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-18 05:25:03 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-05-14 01:06:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValue(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
k := attribute.Key("test")
|
2020-05-14 01:06:03 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2021-02-18 19:59:37 +02:00
|
|
|
value attribute.Value
|
|
|
|
wantType attribute.Type
|
2020-05-14 01:06:03 +02:00
|
|
|
wantValue interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Key.Bool() correctly returns keys's internal bool value",
|
|
|
|
value: k.Bool(true).Value,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.BOOL,
|
2020-05-14 01:06:03 +02:00
|
|
|
wantValue: true,
|
|
|
|
},
|
2021-08-12 17:05:42 +02:00
|
|
|
{
|
|
|
|
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},
|
|
|
|
},
|
2020-05-14 01:06:03 +02:00
|
|
|
{
|
|
|
|
name: "Key.Int64() correctly returns keys's internal int64 value",
|
|
|
|
value: k.Int64(42).Value,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.INT64,
|
2020-05-14 01:06:03 +02:00
|
|
|
wantValue: int64(42),
|
|
|
|
},
|
2024-09-13 09:11:50 +02:00
|
|
|
{
|
|
|
|
name: "Key.Int64() correctly returns negative keys's internal int64 value",
|
|
|
|
value: k.Int64(-42).Value,
|
|
|
|
wantType: attribute.INT64,
|
|
|
|
wantValue: int64(-42),
|
|
|
|
},
|
2020-05-14 01:06:03 +02:00
|
|
|
{
|
2021-08-12 17:05:42 +02:00
|
|
|
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},
|
2020-05-14 01:06:03 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Key.Int() correctly returns keys's internal signed integral value",
|
2021-08-12 17:05:42 +02:00
|
|
|
value: k.Int(42).Value,
|
|
|
|
wantType: attribute.INT64,
|
|
|
|
wantValue: int64(42),
|
2020-05-14 01:06:03 +02:00
|
|
|
},
|
2020-06-25 00:58:08 +02:00
|
|
|
{
|
2021-08-12 17:05:42 +02:00
|
|
|
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},
|
2020-06-25 00:58:08 +02:00
|
|
|
},
|
2021-08-12 17:05:42 +02:00
|
|
|
{
|
|
|
|
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"},
|
|
|
|
},
|
2020-05-14 01:06:03 +02:00
|
|
|
} {
|
|
|
|
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)
|
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
if testcase.wantType == attribute.INVALID {
|
2020-10-06 02:09:03 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
got := testcase.value.AsInterface()
|
|
|
|
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
|
|
|
|
t.Errorf("+got, -want: %s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 16:34:02 +02:00
|
|
|
|
2024-03-06 12:11:16 +02:00
|
|
|
func TestEquivalence(t *testing.T) {
|
2022-10-13 16:34:02 +02:00
|
|
|
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"}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-06 12:11:16 +02:00
|
|
|
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", p[0].Value.Type())
|
|
|
|
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("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()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2022-10-13 16:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|