2020-03-24 07:41:10 +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
|
2019-10-17 07:49:58 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-25 01:40:49 +02:00
|
|
|
"strings"
|
2019-10-17 07:49:58 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2019-10-17 07:49:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeyValueConstructors(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
2021-02-18 19:59:37 +02:00
|
|
|
actual attribute.KeyValue
|
|
|
|
expected attribute.KeyValue
|
2019-10-17 07:49:58 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Bool",
|
2021-02-18 19:59:37 +02:00
|
|
|
actual: attribute.Bool("k1", true),
|
|
|
|
expected: attribute.KeyValue{
|
2019-10-31 00:01:19 +02:00
|
|
|
Key: "k1",
|
2021-02-18 19:59:37 +02:00
|
|
|
Value: attribute.BoolValue(true),
|
2019-10-17 07:49:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Int64",
|
2021-02-18 19:59:37 +02:00
|
|
|
actual: attribute.Int64("k1", 123),
|
|
|
|
expected: attribute.KeyValue{
|
2019-10-31 00:01:19 +02:00
|
|
|
Key: "k1",
|
2021-02-18 19:59:37 +02:00
|
|
|
Value: attribute.Int64Value(123),
|
2019-10-17 07:49:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Float64",
|
2021-02-18 19:59:37 +02:00
|
|
|
actual: attribute.Float64("k1", 123.5),
|
|
|
|
expected: attribute.KeyValue{
|
2019-10-31 00:01:19 +02:00
|
|
|
Key: "k1",
|
2021-02-18 19:59:37 +02:00
|
|
|
Value: attribute.Float64Value(123.5),
|
2019-10-17 07:49:58 +02:00
|
|
|
},
|
|
|
|
},
|
2019-10-31 18:31:46 +02:00
|
|
|
{
|
|
|
|
name: "String",
|
2021-02-18 19:59:37 +02:00
|
|
|
actual: attribute.String("k1", "123.5"),
|
|
|
|
expected: attribute.KeyValue{
|
2019-10-31 18:31:46 +02:00
|
|
|
Key: "k1",
|
2021-02-18 19:59:37 +02:00
|
|
|
Value: attribute.StringValue("123.5"),
|
2019-10-31 18:31:46 +02:00
|
|
|
},
|
|
|
|
},
|
2019-10-17 07:49:58 +02:00
|
|
|
{
|
|
|
|
name: "Int",
|
2021-02-18 19:59:37 +02:00
|
|
|
actual: attribute.Int("k1", 123),
|
|
|
|
expected: attribute.KeyValue{
|
2019-10-31 00:01:19 +02:00
|
|
|
Key: "k1",
|
2021-02-18 19:59:37 +02:00
|
|
|
Value: attribute.IntValue(123),
|
2019-10-17 07:49:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tt {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
if diff := cmp.Diff(test.actual, test.expected, cmp.AllowUnexported(attribute.Value{})); diff != "" {
|
2019-10-17 07:49:58 +02:00
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 01:40:49 +02:00
|
|
|
|
2020-07-27 18:29:22 +02:00
|
|
|
func TestAny(t *testing.T) {
|
2020-04-25 01:40:49 +02:00
|
|
|
builder := &strings.Builder{}
|
|
|
|
builder.WriteString("foo")
|
2020-07-17 22:10:19 +02:00
|
|
|
jsonifyStruct := struct {
|
|
|
|
Public string
|
|
|
|
private string
|
|
|
|
Tagged string `json:"tagName"`
|
|
|
|
Empty string
|
|
|
|
OmitEmpty string `json:",omitempty"`
|
|
|
|
Omit string `json:"-"`
|
|
|
|
}{"foo", "bar", "baz", "", "", "omitted"}
|
|
|
|
invalidStruct := struct {
|
|
|
|
N complex64
|
|
|
|
}{complex(0, 0)}
|
2020-04-25 01:40:49 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
key string
|
|
|
|
value interface{}
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType attribute.Type
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "bool type inferred",
|
|
|
|
value: true,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.BOOL,
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "int64 type inferred",
|
|
|
|
value: int64(42),
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.INT64,
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue: int64(42),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "float64 type inferred",
|
|
|
|
value: float64(42.1),
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.FLOAT64,
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue: 42.1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "string type inferred",
|
|
|
|
value: "foo",
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.STRING,
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "stringer type inferred",
|
|
|
|
value: builder,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.STRING,
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "unknown value serialized as %v",
|
|
|
|
value: nil,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.STRING,
|
2020-04-25 01:40:49 +02:00
|
|
|
wantValue: "<nil>",
|
|
|
|
},
|
2020-07-17 22:10:19 +02:00
|
|
|
{
|
|
|
|
key: "JSON struct serialized correctly",
|
|
|
|
value: &jsonifyStruct,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.STRING,
|
2020-07-17 22:10:19 +02:00
|
|
|
wantValue: `{"Public":"foo","tagName":"baz","Empty":""}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "Invalid JSON struct falls back to string",
|
|
|
|
value: &invalidStruct,
|
2021-02-18 19:59:37 +02:00
|
|
|
wantType: attribute.STRING,
|
2020-07-17 22:10:19 +02:00
|
|
|
wantValue: "&{(0+0i)}",
|
|
|
|
},
|
2020-04-25 01:40:49 +02:00
|
|
|
} {
|
|
|
|
t.Logf("Running test case %s", testcase.key)
|
2021-02-18 19:59:37 +02:00
|
|
|
keyValue := attribute.Any(testcase.key, testcase.value)
|
2020-05-14 01:06:03 +02:00
|
|
|
if keyValue.Value.Type() != testcase.wantType {
|
|
|
|
t.Errorf("wrong value type, got %#v, expected %#v", keyValue.Value.Type(), testcase.wantType)
|
2020-04-25 01:40:49 +02:00
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
got := keyValue.Value.AsInterface()
|
2020-04-25 01:40:49 +02:00
|
|
|
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
|
|
|
|
t.Errorf("+got, -want: %s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 02:48:43 +02:00
|
|
|
|
|
|
|
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: "INVALID value type should be invalid",
|
|
|
|
valid: false,
|
|
|
|
kv: attribute.KeyValue{
|
|
|
|
Key: attribute.Key("valid key"),
|
|
|
|
// Default type is INVALID.
|
|
|
|
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 ARRAY type Value should be valid",
|
|
|
|
valid: true,
|
|
|
|
kv: attribute.Array("array", []int{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if got, want := test.kv.Valid(), test.valid; got != want {
|
|
|
|
t.Error(test.desc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|