...

Source file src/go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform/attribute_test.go

Documentation: go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform

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

View as plain text