-
Notifications
You must be signed in to change notification settings - Fork 53
/
proto_logencoder_test.go
169 lines (135 loc) · 4.28 KB
/
proto_logencoder_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package lightstep
import (
"fmt"
"math"
"strings"
"github.com/lightstep/lightstep-tracer-common/golang/gogo/collectorpb"
"github.com/opentracing/opentracing-go/log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("ProtoLogEncoderTest", func() {
It("encodes empty keys correctly", func(done Done) {
check(log.String("", ""))
check(log.Int("", 0))
close(done)
})
It("encodes string-value keys correctly", func(done Done) {
check(log.String("string:Hello", "Hello"))
check(log.String("string:", ""))
close(done)
})
It("encodes bool-value keys correctly", func(done Done) {
check(log.Bool("bool:false", false))
check(log.Bool("bool:true", true))
close(done)
})
It("encodes int-value keys correctly", func(done Done) {
check(log.Int("int:1", 1))
check(log.Int("int:-1", -1))
close(done)
})
It("encodes int32-value keys correctly", func(done Done) {
check(log.Int32("int:2147483647", math.MaxInt32))
check(log.Int32("int:0", 0))
check(log.Int32("int:10", 10))
check(log.Int32("int:-10", -10))
check(log.Int32("int:-2147483648", math.MinInt32))
close(done)
})
It("encodes int64-value keys correctly", func(done Done) {
check(log.Int64("int:2147483647", math.MaxInt32))
check(log.Int64("int:-2147483648", math.MinInt32))
check(log.Int64("int:9223372036854775807", math.MaxInt64))
check(log.Int64("int:-9223372036854775808", math.MinInt64))
close(done)
})
It("encodes uint32-value keys correctly", func(done Done) {
// Note: unsigned integers are currently encoded as strings. LS-117)
check(log.Uint32("string:0", 0))
check(log.Uint32("string:10", 10))
check(log.Uint32("string:2147483647", math.MaxInt32))
check(log.Uint32("string:4294967295", math.MaxUint32))
close(done)
})
It("encodes uint64-value keys correctly", func(done Done) {
check(log.Uint64("string:0", 0))
check(log.Uint64("string:10", 10))
check(log.Uint64("string:2147483647", math.MaxInt32))
check(log.Uint64("string:2147483648", math.MaxInt32+1))
check(log.Uint64("string:4294967295", math.MaxUint32))
check(log.Uint64("string:18446744073709551615", math.MaxUint64))
close(done)
})
It("encodes object-value keys correctly", func(done Done) {
check(log.Object("json:{}", struct{}{}))
close(done)
})
It("encodes lazy keys correctly", func(done Done) {
check(log.Lazy(func(encoder log.Encoder) {
}), withExpectedNumFields(0), withCheckEmptyKey(false))
check(log.Lazy(func(encoder log.Encoder) {
encoder.EmitInt("int:1", 1)
}), withExpectedNumFields(1), withCheckEmptyKey(false))
check(log.Lazy(func(encoder log.Encoder) {
encoder.EmitInt("int:1", 1)
encoder.EmitInt("int:2", 2)
}), withExpectedNumFields(2), withCheckEmptyKey(false))
close(done)
})
})
type checkOptions struct {
expectedNumFields int
checkEmptyKey bool
}
type checkOption func(*checkOptions)
func withExpectedNumFields(expectedNumFields int) checkOption {
return func(opts *checkOptions) {
opts.expectedNumFields = expectedNumFields
}
}
func withCheckEmptyKey(checkEmptyKey bool) checkOption {
return func(opts *checkOptions) {
opts.checkEmptyKey = checkEmptyKey
}
}
func check(f log.Field, checkOpts ...checkOption) {
opts := checkOptions{
expectedNumFields: 1,
checkEmptyKey: true,
}
for _, opt := range checkOpts {
opt(&opts)
}
options := Options{}
converter := newProtoConverter(options)
buffer := newSpansBuffer(1)
out := &collectorpb.Log{}
marshalFields(converter, out, []log.Field{f}, &buffer)
Expect(buffer.logEncoderErrorCount).To(Equal(int64(0)))
Expect(len(out.Fields)).To(Equal(opts.expectedNumFields))
for _, comp := range out.Fields {
// Make sure empty keys don't crash.
if len(f.Key()) == 0 && opts.checkEmptyKey {
Expect(comp.Key).To(Equal(""))
return
}
insplit := strings.SplitN(comp.Key, ":", 2)
vtype := insplit[0]
expect := insplit[1]
var value interface{}
switch vtype {
case "string":
value = comp.Value.(*collectorpb.KeyValue_StringValue).StringValue
case "int":
value = comp.Value.(*collectorpb.KeyValue_IntValue).IntValue
case "bool":
value = comp.Value.(*collectorpb.KeyValue_BoolValue).BoolValue
case "json":
value = comp.Value.(*collectorpb.KeyValue_JsonValue).JsonValue
default:
panic("Invalid type")
}
Expect(fmt.Sprint(value)).To(Equal(expect))
}
}