| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package tracetransform |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
commonpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
) |
| 14 |
|
| 15 |
type attributeTest struct { |
| 16 |
attrs []attribute.KeyValue |
| 17 |
expected []*commonpb.KeyValue |
| 18 |
} |
| 19 |
|
| 20 |
func TestAttributes(t *testing.T) { |
| 21 |
for _, test := range []attributeTest{ |
| 22 |
{nil, nil}, |
| 23 |
{ |
| 24 |
[]attribute.KeyValue{ |
| 25 |
attribute.Int("int to int", 123), |
| 26 |
attribute.Int64("int64 to int64", 1234567), |
| 27 |
attribute.Float64("float64 to double", 1.61), |
| 28 |
attribute.String("string to string", "string"), |
| 29 |
attribute.Bool("bool to bool", true), |
| 30 |
{Key: "empty to empty"}, |
| 31 |
}, |
| 32 |
[]*commonpb.KeyValue{ |
| 33 |
{ |
| 34 |
Key: "int to int", |
| 35 |
Value: &commonpb.AnyValue{ |
| 36 |
Value: &commonpb.AnyValue_IntValue{ |
| 37 |
IntValue: 123, |
| 38 |
}, |
| 39 |
}, |
| 40 |
}, |
| 41 |
{ |
| 42 |
Key: "int64 to int64", |
| 43 |
Value: &commonpb.AnyValue{ |
| 44 |
Value: &commonpb.AnyValue_IntValue{ |
| 45 |
IntValue: 1234567, |
| 46 |
}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
Key: "float64 to double", |
| 51 |
Value: &commonpb.AnyValue{ |
| 52 |
Value: &commonpb.AnyValue_DoubleValue{ |
| 53 |
DoubleValue: 1.61, |
| 54 |
}, |
| 55 |
}, |
| 56 |
}, |
| 57 |
{ |
| 58 |
Key: "string to string", |
| 59 |
Value: &commonpb.AnyValue{ |
| 60 |
Value: &commonpb.AnyValue_StringValue{ |
| 61 |
StringValue: "string", |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
Key: "bool to bool", |
| 67 |
Value: &commonpb.AnyValue{ |
| 68 |
Value: &commonpb.AnyValue_BoolValue{ |
| 69 |
BoolValue: true, |
| 70 |
}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
Key: "empty to empty", |
| 75 |
Value: &commonpb.AnyValue{}, |
| 76 |
}, |
| 77 |
}, |
| 78 |
}, |
| 79 |
} { |
| 80 |
got := KeyValues(test.attrs) |
| 81 |
if !assert.Len(t, got, len(test.expected)) { |
| 82 |
continue |
| 83 |
} |
| 84 |
for i, actual := range got { |
| 85 |
if a, ok := actual.Value.Value.(*commonpb.AnyValue_DoubleValue); ok { |
| 86 |
e, ok := test.expected[i].Value.Value.(*commonpb.AnyValue_DoubleValue) |
| 87 |
if !ok { |
| 88 |
t.Errorf("expected AnyValue_DoubleValue, got %T", test.expected[i].Value.Value) |
| 89 |
continue |
| 90 |
} |
| 91 |
if !assert.InDelta(t, e.DoubleValue, a.DoubleValue, 0.01) { |
| 92 |
continue |
| 93 |
} |
| 94 |
e.DoubleValue = a.DoubleValue |
| 95 |
} |
| 96 |
assert.Equal(t, test.expected[i], actual) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestArrayAttributes(t *testing.T) { |
| 102 |
// Array KeyValue supports only arrays of primitive types: |
| 103 |
// "bool", "int", "int64", |
| 104 |
// "float64", "string", |
| 105 |
for _, test := range []attributeTest{ |
| 106 |
{nil, nil}, |
| 107 |
{ |
| 108 |
[]attribute.KeyValue{ |
| 109 |
{ |
| 110 |
Key: attribute.Key("empty"), |
| 111 |
Value: attribute.Value{}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
[]*commonpb.KeyValue{ |
| 115 |
{ |
| 116 |
Key: "empty", |
| 117 |
Value: &commonpb.AnyValue{}, |
| 118 |
}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
{ |
| 122 |
[]attribute.KeyValue{ |
| 123 |
attribute.BoolSlice("bool slice to bool array", []bool{true, false}), |
| 124 |
attribute.IntSlice("int slice to int64 array", []int{1, 2, 3}), |
| 125 |
attribute.Int64Slice("int64 slice to int64 array", []int64{1, 2, 3}), |
| 126 |
attribute.Float64Slice("float64 slice to double array", []float64{1.11, 2.22, 3.33}), |
| 127 |
attribute.StringSlice("string slice to string array", []string{"foo", "bar", "baz"}), |
| 128 |
}, |
| 129 |
[]*commonpb.KeyValue{ |
| 130 |
newOTelBoolArray("bool slice to bool array", []bool{true, false}), |
| 131 |
newOTelIntArray("int slice to int64 array", []int64{1, 2, 3}), |
| 132 |
newOTelIntArray("int64 slice to int64 array", []int64{1, 2, 3}), |
| 133 |
newOTelDoubleArray("float64 slice to double array", []float64{1.11, 2.22, 3.33}), |
| 134 |
newOTelStringArray("string slice to string array", []string{"foo", "bar", "baz"}), |
| 135 |
}, |
| 136 |
}, |
| 137 |
} { |
| 138 |
actualArrayAttributes := KeyValues(test.attrs) |
| 139 |
expectedArrayAttributes := test.expected |
| 140 |
if !assert.Len(t, actualArrayAttributes, len(expectedArrayAttributes)) { |
| 141 |
continue |
| 142 |
} |
| 143 |
|
| 144 |
for i, actualArrayAttr := range actualArrayAttributes { |
| 145 |
expectedArrayAttr := expectedArrayAttributes[i] |
| 146 |
expectedKey, actualKey := expectedArrayAttr.Key, actualArrayAttr.Key |
| 147 |
if !assert.Equal(t, expectedKey, actualKey) { |
| 148 |
continue |
| 149 |
} |
| 150 |
|
| 151 |
expected := expectedArrayAttr.Value.GetArrayValue() |
| 152 |
actual := actualArrayAttr.Value.GetArrayValue() |
| 153 |
if expected == nil { |
| 154 |
assert.Nil(t, actual) |
| 155 |
continue |
| 156 |
} |
| 157 |
if assert.NotNil(t, actual, "expected not nil for %s", actualKey) { |
| 158 |
assertExpectedArrayValues(t, expected.Values, actual.Values) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func assertExpectedArrayValues(t *testing.T, expectedValues, actualValues []*commonpb.AnyValue) { |
| 165 |
for i, actual := range actualValues { |
| 166 |
expected := expectedValues[i] |
| 167 |
if a, ok := actual.Value.(*commonpb.AnyValue_DoubleValue); ok { |
| 168 |
e, ok := expected.Value.(*commonpb.AnyValue_DoubleValue) |
| 169 |
if !ok { |
| 170 |
t.Errorf("expected AnyValue_DoubleValue, got %T", expected.Value) |
| 171 |
continue |
| 172 |
} |
| 173 |
if !assert.InDelta(t, e.DoubleValue, a.DoubleValue, 0.01) { |
| 174 |
continue |
| 175 |
} |
| 176 |
e.DoubleValue = a.DoubleValue |
| 177 |
} |
| 178 |
assert.Equal(t, expected, actual) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func newOTelBoolArray(key string, values []bool) *commonpb.KeyValue { |
| 183 |
arrayValues := []*commonpb.AnyValue{} |
| 184 |
for _, b := range values { |
| 185 |
arrayValues = append(arrayValues, &commonpb.AnyValue{ |
| 186 |
Value: &commonpb.AnyValue_BoolValue{ |
| 187 |
BoolValue: b, |
| 188 |
}, |
| 189 |
}) |
| 190 |
} |
| 191 |
|
| 192 |
return newOTelArray(key, arrayValues) |
| 193 |
} |
| 194 |
|
| 195 |
func newOTelIntArray(key string, values []int64) *commonpb.KeyValue { |
| 196 |
arrayValues := []*commonpb.AnyValue{} |
| 197 |
|
| 198 |
for _, i := range values { |
| 199 |
arrayValues = append(arrayValues, &commonpb.AnyValue{ |
| 200 |
Value: &commonpb.AnyValue_IntValue{ |
| 201 |
IntValue: i, |
| 202 |
}, |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
return newOTelArray(key, arrayValues) |
| 207 |
} |
| 208 |
|
| 209 |
func newOTelDoubleArray(key string, values []float64) *commonpb.KeyValue { |
| 210 |
arrayValues := []*commonpb.AnyValue{} |
| 211 |
|
| 212 |
for _, d := range values { |
| 213 |
arrayValues = append(arrayValues, &commonpb.AnyValue{ |
| 214 |
Value: &commonpb.AnyValue_DoubleValue{ |
| 215 |
DoubleValue: d, |
| 216 |
}, |
| 217 |
}) |
| 218 |
} |
| 219 |
|
| 220 |
return newOTelArray(key, arrayValues) |
| 221 |
} |
| 222 |
|
| 223 |
func newOTelStringArray(key string, values []string) *commonpb.KeyValue { |
| 224 |
arrayValues := []*commonpb.AnyValue{} |
| 225 |
|
| 226 |
for _, s := range values { |
| 227 |
arrayValues = append(arrayValues, &commonpb.AnyValue{ |
| 228 |
Value: &commonpb.AnyValue_StringValue{ |
| 229 |
StringValue: s, |
| 230 |
}, |
| 231 |
}) |
| 232 |
} |
| 233 |
|
| 234 |
return newOTelArray(key, arrayValues) |
| 235 |
} |
| 236 |
|
| 237 |
func newOTelArray(key string, arrayValues []*commonpb.AnyValue) *commonpb.KeyValue { |
| 238 |
return &commonpb.KeyValue{ |
| 239 |
Key: key, |
| 240 |
Value: &commonpb.AnyValue{ |
| 241 |
Value: &commonpb.AnyValue_ArrayValue{ |
| 242 |
ArrayValue: &commonpb.ArrayValue{ |
| 243 |
Values: arrayValues, |
| 244 |
}, |
| 245 |
}, |
| 246 |
}, |
| 247 |
} |
| 248 |
} |