2020-04-23 21:10:58 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
package attribute_test
|
2020-04-23 21:10:58 +02:00
|
|
|
|
|
|
|
import (
|
2023-04-04 17:06:10 +02:00
|
|
|
"reflect"
|
2020-08-11 20:00:23 +02:00
|
|
|
"regexp"
|
2020-04-23 21:10:58 +02:00
|
|
|
"testing"
|
|
|
|
|
2023-04-04 17:06:10 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-23 21:10:58 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-02-18 19:59:37 +02:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-04-23 21:10:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testCase struct {
|
2021-02-18 19:59:37 +02:00
|
|
|
kvs []attribute.KeyValue
|
2020-08-11 20:00:23 +02:00
|
|
|
|
|
|
|
keyRe *regexp.Regexp
|
|
|
|
|
2020-04-23 21:10:58 +02:00
|
|
|
encoding string
|
2020-08-11 20:00:23 +02:00
|
|
|
fullEnc string
|
2020-04-23 21:10:58 +02:00
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
func expect(enc string, kvs ...attribute.KeyValue) testCase {
|
2020-04-23 21:10:58 +02:00
|
|
|
return testCase{
|
|
|
|
kvs: kvs,
|
|
|
|
encoding: enc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
func expectFiltered(enc, filter, fullEnc string, kvs ...attribute.KeyValue) testCase {
|
2020-08-11 20:00:23 +02:00
|
|
|
return testCase{
|
|
|
|
kvs: kvs,
|
|
|
|
keyRe: regexp.MustCompile(filter),
|
|
|
|
encoding: enc,
|
|
|
|
fullEnc: fullEnc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:10:58 +02:00
|
|
|
func TestSetDedup(t *testing.T) {
|
|
|
|
cases := []testCase{
|
2021-02-18 19:59:37 +02:00
|
|
|
expect("A=B", attribute.String("A", "2"), attribute.String("A", "B")),
|
|
|
|
expect("A=B", attribute.String("A", "2"), attribute.Int("A", 1), attribute.String("A", "B")),
|
|
|
|
expect("A=B", attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
|
|
|
|
|
|
|
|
expect("A=B,C=D", attribute.String("A", "1"), attribute.String("C", "D"), attribute.String("A", "B")),
|
|
|
|
expect("A=B,C=D", attribute.String("A", "2"), attribute.String("A", "B"), attribute.String("C", "D")),
|
|
|
|
expect("A=B,C=D", attribute.Float64("C", 1.2), attribute.String("A", "2"), attribute.String("A", "B"), attribute.String("C", "D")),
|
|
|
|
expect("A=B,C=D", attribute.String("C", "D"), attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
|
|
|
|
expect("A=B,C=D", attribute.String("A", "B"), attribute.String("C", "D"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
|
|
|
|
expect("A=B,C=D", attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B"), attribute.String("C", "D")),
|
2020-04-23 21:10:58 +02:00
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
enc := attribute.DefaultEncoder()
|
2020-04-23 21:10:58 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
s2d := map[string][]attribute.Distinct{}
|
|
|
|
d2s := map[attribute.Distinct][]string{}
|
2020-04-23 21:10:58 +02:00
|
|
|
|
|
|
|
for _, tc := range cases {
|
2021-02-18 19:59:37 +02:00
|
|
|
cpy := make([]attribute.KeyValue, len(tc.kvs))
|
2020-04-23 21:10:58 +02:00
|
|
|
copy(cpy, tc.kvs)
|
2021-02-18 19:59:37 +02:00
|
|
|
sl := attribute.NewSet(cpy...)
|
2020-04-23 21:10:58 +02:00
|
|
|
|
|
|
|
// Ensure that the input was reordered but no elements went missing.
|
|
|
|
require.ElementsMatch(t, tc.kvs, cpy)
|
|
|
|
|
|
|
|
str := sl.Encoded(enc)
|
|
|
|
equ := sl.Equivalent()
|
|
|
|
|
|
|
|
s2d[str] = append(s2d[str], equ)
|
|
|
|
d2s[equ] = append(d2s[equ], str)
|
|
|
|
|
|
|
|
require.Equal(t, tc.encoding, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
for s, d := range s2d {
|
|
|
|
// No other Distinct values are equal to this.
|
|
|
|
for s2, d2 := range s2d {
|
|
|
|
if s2 == s {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, elt := range d {
|
|
|
|
for _, otherDistinct := range d2 {
|
|
|
|
require.NotEqual(t, otherDistinct, elt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, strings := range d2s {
|
|
|
|
if strings[0] == s {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, otherString := range strings {
|
|
|
|
require.NotEqual(t, otherString, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for d, s := range d2s {
|
|
|
|
// No other Distinct values are equal to this.
|
|
|
|
for d2, s2 := range d2s {
|
|
|
|
if d2 == d {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, elt := range s {
|
|
|
|
for _, otherDistinct := range s2 {
|
|
|
|
require.NotEqual(t, otherDistinct, elt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, distincts := range s2d {
|
|
|
|
if distincts[0] == d {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, otherDistinct := range distincts {
|
|
|
|
require.NotEqual(t, otherDistinct, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 20:00:23 +02:00
|
|
|
|
|
|
|
func TestUniqueness(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
short := []attribute.KeyValue{
|
|
|
|
attribute.String("A", "0"),
|
|
|
|
attribute.String("B", "2"),
|
|
|
|
attribute.String("A", "1"),
|
2020-08-11 20:00:23 +02:00
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
long := []attribute.KeyValue{
|
|
|
|
attribute.String("B", "2"),
|
|
|
|
attribute.String("C", "5"),
|
|
|
|
attribute.String("B", "2"),
|
|
|
|
attribute.String("C", "1"),
|
|
|
|
attribute.String("A", "4"),
|
|
|
|
attribute.String("C", "3"),
|
|
|
|
attribute.String("A", "1"),
|
2020-08-11 20:00:23 +02:00
|
|
|
}
|
|
|
|
cases := []testCase{
|
|
|
|
expectFiltered("A=1", "^A$", "B=2", short...),
|
|
|
|
expectFiltered("B=2", "^B$", "A=1", short...),
|
|
|
|
expectFiltered("A=1,B=2", "^A|B$", "", short...),
|
|
|
|
expectFiltered("", "^C", "A=1,B=2", short...),
|
|
|
|
|
|
|
|
expectFiltered("A=1,C=3", "A|C", "B=2", long...),
|
|
|
|
expectFiltered("B=2,C=3", "C|B", "A=1", long...),
|
|
|
|
expectFiltered("C=3", "C", "A=1,B=2", long...),
|
|
|
|
expectFiltered("", "D", "A=1,B=2,C=3", long...),
|
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
enc := attribute.DefaultEncoder()
|
2020-08-11 20:00:23 +02:00
|
|
|
|
|
|
|
for _, tc := range cases {
|
2021-02-18 19:59:37 +02:00
|
|
|
cpy := make([]attribute.KeyValue, len(tc.kvs))
|
2020-08-11 20:00:23 +02:00
|
|
|
copy(cpy, tc.kvs)
|
2022-04-18 16:31:31 +02:00
|
|
|
distinct, uniq := attribute.NewSetWithFiltered(cpy, func(attr attribute.KeyValue) bool {
|
|
|
|
return tc.keyRe.MatchString(string(attr.Key))
|
2020-08-11 20:00:23 +02:00
|
|
|
})
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
full := attribute.NewSet(uniq...)
|
2020-08-11 20:00:23 +02:00
|
|
|
|
|
|
|
require.Equal(t, tc.encoding, distinct.Encoded(enc))
|
|
|
|
require.Equal(t, tc.fullEnc, full.Encoded(enc))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookup(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
set := attribute.NewSet(attribute.Int("C", 3), attribute.Int("A", 1), attribute.Int("B", 2))
|
2020-08-11 20:00:23 +02:00
|
|
|
|
|
|
|
value, has := set.Value("C")
|
|
|
|
require.True(t, has)
|
|
|
|
require.Equal(t, int64(3), value.AsInt64())
|
|
|
|
|
|
|
|
value, has = set.Value("B")
|
|
|
|
require.True(t, has)
|
|
|
|
require.Equal(t, int64(2), value.AsInt64())
|
|
|
|
|
|
|
|
value, has = set.Value("A")
|
|
|
|
require.True(t, has)
|
|
|
|
require.Equal(t, int64(1), value.AsInt64())
|
|
|
|
|
2022-08-25 04:42:28 +02:00
|
|
|
_, has = set.Value("D")
|
2020-08-11 20:00:23 +02:00
|
|
|
require.False(t, has)
|
|
|
|
}
|
2023-04-04 17:06:10 +02:00
|
|
|
|
|
|
|
func TestZeroSetExportedMethodsNoPanic(t *testing.T) {
|
|
|
|
rType := reflect.TypeOf((*attribute.Set)(nil))
|
|
|
|
rVal := reflect.ValueOf(&attribute.Set{})
|
|
|
|
for n := 0; n < rType.NumMethod(); n++ {
|
|
|
|
mType := rType.Method(n)
|
|
|
|
if !mType.IsExported() {
|
|
|
|
t.Logf("ignoring unexported %s", mType.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Run(mType.Name, func(t *testing.T) {
|
|
|
|
m := rVal.MethodByName(mType.Name)
|
|
|
|
if !m.IsValid() {
|
|
|
|
t.Errorf("unknown method: %s", mType.Name)
|
|
|
|
}
|
|
|
|
assert.NotPanics(t, func() { _ = m.Call(args(mType)) })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func args(m reflect.Method) []reflect.Value {
|
|
|
|
numIn := m.Type.NumIn() - 1 // Do not include the receiver arg.
|
|
|
|
if numIn <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if m.Type.IsVariadic() {
|
|
|
|
numIn--
|
|
|
|
}
|
|
|
|
out := make([]reflect.Value, numIn)
|
|
|
|
for i := range out {
|
|
|
|
aType := m.Type.In(i + 1) // Skip receiver arg.
|
|
|
|
out[i] = reflect.New(aType).Elem()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|