-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaggage_test.go
137 lines (112 loc) · 3.59 KB
/
baggage_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
package oteltag_test
import (
"context"
"testing"
"go.opentelemetry.io/otel/baggage"
oteltag "github.com/remychantenay/otel-tag"
)
func TestBaggageMembers(t *testing.T) {
t.Run("when non-zero values - should add all members to baggage", func(t *testing.T) {
const wantMemberCount = 10
want := map[string]string{
"val_str": "a_string",
"val_int": "42",
"val_int64": "42000000000",
"val_float64": "99.718281828",
"val_bool": "true",
"val_str_slice": "a_string_1,a_string_2,a_string_3",
"val_int_slice": "1,2,3",
"val_int64_slice": "100000,200000,300000",
"val_float64_slice": "1.1,2.2,3.3",
"val_bool_slice": "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},
}
members := oteltag.BaggageMembers(m)
bag, _ := baggage.New(members...)
ctx := baggage.ContextWithBaggage(context.Background(), bag)
bag = baggage.FromContext(ctx)
memberCount := len(bag.Members())
if memberCount != wantMemberCount {
t.Errorf("\ngot %d members\nwant %d", memberCount, wantMemberCount)
}
for k, v := range want {
member := bag.Member(k)
if member.Value() != v {
t.Errorf("\ngot %q for member %q\nwant %q", member.Value(), k, v)
}
}
})
t.Run("when zero values and omitted - should not add members to baggage", func(t *testing.T) {
const wantMemberCount = 1
want := map[string]string{
"val_bool": "true",
}
m := testModel{ValBool: true}
members := oteltag.BaggageMembers(m)
bag, _ := baggage.New(members...)
ctx := baggage.ContextWithBaggage(context.Background(), bag)
bag = baggage.FromContext(ctx)
memberCount := len(bag.Members())
if memberCount != wantMemberCount {
t.Errorf("\ngot %d members\nwant %d", memberCount, wantMemberCount)
}
for k, v := range want {
member := bag.Member(k)
if member.Value() != v {
t.Errorf("\ngot %q for member %q\nwant %q", member.Value(), k, v)
}
}
})
t.Run("when zero values and not omitted - should add members to baggage", func(t *testing.T) {
const wantMemberCount = 1
want := map[string]string{
"val_str_not_omitted": "",
}
m := struct {
ValStrNotOmitted string `otel:"val_str_not_omitted"`
}{
ValStrNotOmitted: "",
}
members := oteltag.BaggageMembers(m)
bag, _ := baggage.New(members...)
ctx := baggage.ContextWithBaggage(context.Background(), bag)
bag = baggage.FromContext(ctx)
memberCount := len(bag.Members())
if memberCount != wantMemberCount {
t.Errorf("\ngot %d members\nwant %d", memberCount, wantMemberCount)
}
for k, v := range want {
member := bag.Member(k)
if member.Value() != v {
t.Errorf("\ngot %q for member %q\nwant %q", member.Value(), k, v)
}
}
})
t.Run("when fields are not tagged - should not add members to baggage", func(t *testing.T) {
const wantMemberCount = 0
m := struct {
ValStrIgnored string
}{
ValStrIgnored: "a_string",
}
members := oteltag.BaggageMembers(m)
bag, _ := baggage.New(members...)
ctx := baggage.ContextWithBaggage(context.Background(), bag)
bag = baggage.FromContext(ctx)
memberCount := len(bag.Members())
if memberCount != wantMemberCount {
t.Errorf("\ngot %d members\nwant %d", memberCount, wantMemberCount)
}
})
}