-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconv_test.go
46 lines (41 loc) · 1.88 KB
/
conv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package otelzap
import (
"bytes"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
testFieldKey = "test-123"
testNow = time.Now()
)
func TestOTeLAttributeMapping(t *testing.T) {
tests := []struct {
Input zapcore.Field
Expected []attribute.KeyValue
}{
{Input: zap.Bool(testFieldKey, true), Expected: []attribute.KeyValue{attribute.Bool(testFieldKey, true)}},
{Input: zap.Float64(testFieldKey, 123.123), Expected: []attribute.KeyValue{attribute.Float64(testFieldKey, 123.123)}},
{Input: zap.Int(testFieldKey, 123), Expected: []attribute.KeyValue{attribute.Int64(testFieldKey, 123)}},
{Input: zap.String(testFieldKey, "hello"), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "hello")}},
{Input: zap.ByteString(testFieldKey, []byte("hello")), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "hello")}},
{Input: zap.Binary(testFieldKey, []byte{1, 0, 0, 1}), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "AQAAAQ==")}},
{Input: zap.Duration(testFieldKey, time.Minute), Expected: []attribute.KeyValue{attribute.Float64(testFieldKey, time.Minute.Seconds())}},
{Input: zap.Time(testFieldKey, testNow), Expected: []attribute.KeyValue{attribute.Int64(testFieldKey, testNow.Unix())}},
{Input: zap.Stringer(testFieldKey, bytes.NewBuffer([]byte("hello"))), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "hello")}},
{Input: zap.Error(errors.New("world")), Expected: []attribute.KeyValue{semconv.ExceptionMessage("world")}},
{Input: zap.Skip(), Expected: []attribute.KeyValue{}},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%+v", test.Input), func(t *testing.T) {
output := otelAttribute(test.Input)
assert.ElementsMatch(t, test.Expected, output)
})
}
}