| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package telemetry // import "go.opentelemetry.io/collector/internal/telemetry" |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
"go.uber.org/zap" |
| 12 |
) |
| 13 |
|
| 14 |
func TestToZapFields(t *testing.T) { |
| 15 |
tests := []struct { |
| 16 |
attrs attribute.Set |
| 17 |
expected []zap.Field |
| 18 |
}{ |
| 19 |
{ |
| 20 |
attrs: attribute.NewSet( |
| 21 |
attribute.String("string_key", "string_value"), |
| 22 |
), |
| 23 |
expected: []zap.Field{ |
| 24 |
zap.String("string_key", "string_value"), |
| 25 |
}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
attrs: attribute.NewSet( |
| 29 |
attribute.Bool("bool_key", true), |
| 30 |
), |
| 31 |
expected: []zap.Field{ |
| 32 |
zap.Bool("bool_key", true), |
| 33 |
}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
attrs: attribute.NewSet( |
| 37 |
attribute.Int64("int64_key", 42), |
| 38 |
), |
| 39 |
expected: []zap.Field{ |
| 40 |
zap.Int64("int64_key", 42), |
| 41 |
}, |
| 42 |
}, |
| 43 |
{ |
| 44 |
attrs: attribute.NewSet( |
| 45 |
attribute.Float64("float64_key", 3.14), |
| 46 |
), |
| 47 |
expected: []zap.Field{ |
| 48 |
zap.Float64("float64_key", 3.14), |
| 49 |
}, |
| 50 |
}, |
| 51 |
{ |
| 52 |
attrs: attribute.NewSet( |
| 53 |
attribute.BoolSlice("bool_slice_key", []bool{true, false, true}), |
| 54 |
), |
| 55 |
expected: []zap.Field{ |
| 56 |
zap.Bools("bool_slice_key", []bool{true, false, true}), |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
attrs: attribute.NewSet( |
| 61 |
attribute.Int64Slice("int64_slice_key", []int64{1, 2, 3}), |
| 62 |
), |
| 63 |
expected: []zap.Field{ |
| 64 |
zap.Int64s("int64_slice_key", []int64{1, 2, 3}), |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
attrs: attribute.NewSet( |
| 69 |
attribute.Float64Slice("float64_slice_key", []float64{1.1, 2.2, 3.3}), |
| 70 |
), |
| 71 |
expected: []zap.Field{ |
| 72 |
zap.Float64s("float64_slice_key", []float64{1.1, 2.2, 3.3}), |
| 73 |
}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
attrs: attribute.NewSet( |
| 77 |
attribute.StringSlice("string_slice_key", []string{"a", "b", "c"}), |
| 78 |
), |
| 79 |
expected: []zap.Field{ |
| 80 |
zap.Strings("string_slice_key", []string{"a", "b", "c"}), |
| 81 |
}, |
| 82 |
}, |
| 83 |
} |
| 84 |
|
| 85 |
for _, tt := range tests { |
| 86 |
name := "<empty>" |
| 87 |
if tt.attrs.Len() > 0 { |
| 88 |
attr, ok := tt.attrs.Get(0) |
| 89 |
if ok { |
| 90 |
name = string(attr.Key) |
| 91 |
} |
| 92 |
} |
| 93 |
t.Run(name, func(t *testing.T) { |
| 94 |
result := ToZapFields(tt.attrs.ToSlice()) |
| 95 |
require.Equal(t, tt.expected, result) |
| 96 |
}) |
| 97 |
} |
| 98 |
} |