|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package loadbalancingexporter |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 11 | +) |
| 12 | + |
| 13 | +func TestBuildAttributeRoutingKeyValue_Primitives(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + value pcommon.Value |
| 17 | + want string |
| 18 | + }{ |
| 19 | + {name: "string", value: pcommon.NewValueStr("abc"), want: "k=abc|"}, |
| 20 | + {name: "int", value: pcommon.NewValueInt(42), want: "k=42|"}, |
| 21 | + {name: "double", value: pcommon.NewValueDouble(3.5), want: "k=3.5|"}, |
| 22 | + {name: "bool", value: pcommon.NewValueBool(true), want: "k=true|"}, |
| 23 | + {name: "empty", value: pcommon.NewValueEmpty(), want: "k=|"}, |
| 24 | + { |
| 25 | + name: "bytes", |
| 26 | + value: func() pcommon.Value { |
| 27 | + v := pcommon.NewValueBytes() |
| 28 | + v.Bytes().FromRaw([]byte{0x01, 0xFF}) |
| 29 | + return v |
| 30 | + }(), |
| 31 | + want: "k=Af8=|", |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + for _, tc := range tests { |
| 36 | + t.Run(tc.name, func(t *testing.T) { |
| 37 | + assert.Equal(t, tc.want, buildAttributeRoutingKeyValue("k", tc.value)) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestBuildAttributeRoutingKeyValue_MapAndSliceDeterministic(t *testing.T) { |
| 43 | + m1 := pcommon.NewValueMap() |
| 44 | + m1.Map().PutInt("b", 2) |
| 45 | + m1.Map().PutStr("a", "x") |
| 46 | + |
| 47 | + m2 := pcommon.NewValueMap() |
| 48 | + m2.Map().PutStr("a", "x") |
| 49 | + m2.Map().PutInt("b", 2) |
| 50 | + |
| 51 | + assert.Equal(t, "k={\"a\":\"x\",\"b\":2}|", buildAttributeRoutingKeyValue("k", m1)) |
| 52 | + assert.Equal(t, buildAttributeRoutingKeyValue("k", m1), buildAttributeRoutingKeyValue("k", m2)) |
| 53 | + |
| 54 | + s := pcommon.NewValueSlice() |
| 55 | + s.Slice().AppendEmpty().SetInt(1) |
| 56 | + s.Slice().AppendEmpty().SetStr("x") |
| 57 | + s.Slice().AppendEmpty().SetBool(false) |
| 58 | + |
| 59 | + assert.Equal(t, "k=[1,\"x\",false]|", buildAttributeRoutingKeyValue("k", s)) |
| 60 | +} |
0 commit comments