-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspan_test.go
268 lines (221 loc) · 6.95 KB
/
span_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package oteltag_test
import (
"context"
"slices"
"testing"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
oteltag "github.com/remychantenay/otel-tag"
)
func TestSpanAttributes(t *testing.T) {
const testOperationName = "span"
setupTracer := func() (*tracetest.SpanRecorder, trace.Tracer) {
spanRecorder := tracetest.NewSpanRecorder()
traceProvider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
tracer := traceProvider.Tracer("test-tracer")
return spanRecorder, tracer
}
t.Run("when non-zero values - should add all attributes to span", func(t *testing.T) {
const (
wantSpanCount = 1
expectedAttributeCount = 10
)
spanRecorder, tracer := setupTracer()
want := map[attribute.Key]attribute.Value{
"val_str": attribute.StringValue("a_string"),
"val_int": attribute.IntValue(42),
"val_int64": attribute.Int64Value(42000000000),
"val_float64": attribute.Float64Value(99.718281828),
"val_bool": attribute.BoolValue(true),
"val_str_slice": attribute.StringSliceValue([]string{"a_string_1", "a_string_2", "a_string_3"}),
"val_int_slice": attribute.IntSliceValue([]int{1, 2, 3}),
"val_int64_slice": attribute.Int64SliceValue([]int64{100000, 200000, 300000}),
"val_float64_slice": attribute.Float64SliceValue([]float64{1.1, 2.2, 3.3}),
"val_bool_slice": attribute.BoolSliceValue([]bool{true, false, true, false}),
}
m := testModel{
ValStr: "a_string",
ValInt: 42,
ValInt64: 42000000000,
ValFloat64: 99.718281828,
ValBool: true,
ValStrSlice: []string{"a_string_1", "a_string_2", "a_string_3"},
ValIntSlice: []int{1, 2, 3},
ValInt64Slice: []int64{100000, 200000, 300000},
ValFloat64Slice: []float64{1.1, 2.2, 3.3},
ValBoolSlice: []bool{true, false, true, false},
}
func() {
_, span := tracer.Start(
context.Background(),
testOperationName,
trace.WithAttributes(oteltag.SpanAttributes(m)...),
)
defer span.End()
}()
spans := spanRecorder.Ended()
if len(spans) != wantSpanCount {
t.Errorf("\ngot %d spans\nwant %d", len(spans), wantSpanCount)
}
attrCount := len(spans[0].Attributes())
if attrCount != expectedAttributeCount {
t.Errorf("\ngot %d attributes\nwant %d", attrCount, expectedAttributeCount)
}
for k, v := range want {
if !slices.Contains(spans[0].Attributes(), attribute.KeyValue{Key: k, Value: v}) {
t.Errorf("\nmissing '%v' attribute with value %q", k, v.AsString())
}
}
})
t.Run("when zero values and omitted - should not add attributes to span", func(t *testing.T) {
const (
wantSpanCount = 1
expectedAttributeCount = 1
)
spanRecorder, tracer := setupTracer()
want := map[attribute.Key]attribute.Value{
"val_bool": attribute.BoolValue(true),
}
m := testModel{ValBool: true}
func() {
_, span := tracer.Start(
context.Background(),
testOperationName,
trace.WithAttributes(oteltag.SpanAttributes(m)...),
)
defer span.End()
}()
spans := spanRecorder.Ended()
if len(spans) != wantSpanCount {
t.Errorf("\ngot %d spans\nwant %d", len(spans), wantSpanCount)
}
attrCount := len(spans[0].Attributes())
if attrCount != expectedAttributeCount {
t.Errorf("\ngot %d attributes\nwant %d", attrCount, expectedAttributeCount)
}
for k, v := range want {
if !slices.Contains(spans[0].Attributes(), attribute.KeyValue{Key: k, Value: v}) {
t.Errorf("\nmissing '%v' attribute with value %q", k, v.AsString())
}
}
})
t.Run("when zero values and not omitted - should add attributes to span", func(t *testing.T) {
const (
wantSpanCount = 1
expectedAttributeCount = 1
)
spanRecorder, tracer := setupTracer()
want := map[attribute.Key]attribute.Value{
"val_str_not_omitted": attribute.StringValue(""),
}
m := struct {
ValStrNotOmitted string `otel:"val_str_not_omitted"`
}{
ValStrNotOmitted: "",
}
func() {
_, span := tracer.Start(
context.Background(),
testOperationName,
trace.WithAttributes(oteltag.SpanAttributes(m)...),
)
defer span.End()
}()
spans := spanRecorder.Ended()
if len(spans) != wantSpanCount {
t.Errorf("\ngot %d spans\nwant %d", len(spans), wantSpanCount)
}
attrCount := len(spans[0].Attributes())
if attrCount != expectedAttributeCount {
t.Errorf("\ngot %d attributes\nwant %d", attrCount, expectedAttributeCount)
}
for k, v := range want {
if !slices.Contains(spans[0].Attributes(), attribute.KeyValue{Key: k, Value: v}) {
t.Errorf("\nmissing '%v' attribute with value %q", k, v.AsString())
}
}
})
t.Run("when fields are not tagged - should not add attributes to span", func(t *testing.T) {
const (
wantSpanCount = 1
expectedAttributeCount = 0
)
spanRecorder, tracer := setupTracer()
m := struct {
ValStrIgnored string
}{
ValStrIgnored: "a_string",
}
func() {
_, span := tracer.Start(
context.Background(),
testOperationName,
trace.WithAttributes(oteltag.SpanAttributes(m)...),
)
defer span.End()
}()
spans := spanRecorder.Ended()
if len(spans) != wantSpanCount {
t.Errorf("\ngot %d spans\nwant %d", len(spans), wantSpanCount)
}
attrCount := len(spans[0].Attributes())
if attrCount != expectedAttributeCount {
t.Errorf("\ngot %d attributes\nwant %d", attrCount, expectedAttributeCount)
}
})
t.Run("when structs in structs - should add all attributes to span", func(t *testing.T) {
const (
wantSpanCount = 1
expectedAttributeCount = 3
)
want := map[attribute.Key]attribute.Value{
"struct1.val_str_1": attribute.StringValue("a_string"),
"struct2.val_str_2": attribute.StringValue("b_string"),
"struct3.val_str_3": attribute.StringValue("c_string"),
}
spanRecorder, tracer := setupTracer()
type struct1 struct {
ValStr1 string `otel:"struct1.val_str_1"`
}
type struct2 struct {
ValStr2 string `otel:"struct2.val_str_2"`
}
type struct3 struct {
S1 struct1
S2 *struct2
ValStr3 string `otel:"struct3.val_str_3"`
}
m := struct3{
S1: struct1{
ValStr1: "a_string",
},
S2: &struct2{
ValStr2: "b_string",
},
ValStr3: "c_string",
}
func() {
_, span := tracer.Start(
context.Background(),
testOperationName,
trace.WithAttributes(oteltag.SpanAttributes(m)...),
)
defer span.End()
}()
spans := spanRecorder.Ended()
if len(spans) != wantSpanCount {
t.Errorf("\ngot %d spans\nwant %d", len(spans), wantSpanCount)
}
attrCount := len(spans[0].Attributes())
if attrCount != expectedAttributeCount {
t.Errorf("\ngot %d attributes\nwant %d", attrCount, expectedAttributeCount)
}
for k, v := range want {
if !slices.Contains(spans[0].Attributes(), attribute.KeyValue{Key: k, Value: v}) {
t.Errorf("\nmissing '%v' attribute with value %q", k, v.AsString())
}
}
})
}