-
Notifications
You must be signed in to change notification settings - Fork 214
/
dynamic_sampling_context_test.go
247 lines (233 loc) · 6.02 KB
/
dynamic_sampling_context_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
package sentry
import (
"context"
"testing"
"github.com/getsentry/sentry-go/internal/testutils"
)
func TestDynamicSamplingContextFromHeader(t *testing.T) {
tests := []struct {
input []byte
want DynamicSamplingContext
errMsg string
}{
// Empty baggage header
{
input: []byte(""),
want: DynamicSamplingContext{
Frozen: false,
Entries: map[string]string{},
},
},
// Third-party baggage
{
input: []byte("other-vendor-key1=value1;value2, other-vendor-key2=value3"),
want: DynamicSamplingContext{
Frozen: false,
Entries: map[string]string{},
},
},
// Sentry-only baggage
{
input: []byte("sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1"),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"sample_rate": "1",
},
},
},
// Mixed baggage
{
input: []byte("sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1,foo=bar;foo;bar;bar=baz"),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"sample_rate": "1",
},
},
},
// Invalid baggage value
{
input: []byte(","),
want: DynamicSamplingContext{
Frozen: false,
},
errMsg: "invalid baggage list-member: \"\"",
},
}
for _, tc := range tests {
got, err := DynamicSamplingContextFromHeader(tc.input)
assertEqual(t, got, tc.want, "Context mismatch")
if err != nil {
assertEqual(t, err.Error(), tc.errMsg, "Error mismatch")
}
}
}
func TestDynamicSamplingContextFromTransaction(t *testing.T) {
tests := []struct {
input *Span
want DynamicSamplingContext
}{
// Normal flow
{
input: func() *Span {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Dsn: "http://[email protected]/sentry/1",
Release: "1.0.0",
Environment: "test",
})
hubFromContext(ctx).ConfigureScope(func(scope *Scope) {
scope.SetUser(User{Segment: "user_segment"})
})
txn := StartTransaction(ctx, "name", WithTransactionSource(SourceCustom))
txn.TraceID = TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03")
return txn
}(),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"sample_rate": "1",
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"release": "1.0.0",
"environment": "test",
"transaction": "name",
"sampled": "true",
},
},
},
// Transaction with source url, do not include in Dynamic Sampling context
{
input: func() *Span {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 0.0,
Dsn: "http://[email protected]/sentry/1",
Release: "1.0.0",
})
txn := StartTransaction(ctx, "name", WithTransactionSource(SourceURL))
txn.TraceID = TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03")
return txn
}(),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"release": "1.0.0",
"sampled": "false",
},
},
},
// Empty context without a valid Client
{
input: func() *Span {
ctx := context.Background()
tx := StartTransaction(ctx, "op")
return tx
}(),
want: DynamicSamplingContext{
Frozen: false,
Entries: map[string]string{},
},
},
}
for _, tc := range tests {
got := DynamicSamplingContextFromTransaction(tc.input)
assertEqual(t, got, tc.want)
}
}
func TestHasEntries(t *testing.T) {
var dsc DynamicSamplingContext
dsc = DynamicSamplingContext{}
assertEqual(t, dsc.HasEntries(), false)
dsc = DynamicSamplingContext{
Entries: map[string]string{
"foo": "bar",
},
}
assertEqual(t, dsc.HasEntries(), true)
}
func TestString(t *testing.T) {
var dsc DynamicSamplingContext
dsc = DynamicSamplingContext{}
assertEqual(t, dsc.String(), "")
dsc = DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"sample_rate": "1",
},
}
testutils.AssertBaggageStringsEqual(t, dsc.String(), "sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1")
}
func TestDynamicSamplingContextFromScope(t *testing.T) {
tests := map[string]struct {
scope *Scope
client *Client
expected DynamicSamplingContext
}{
"Valid input": {
scope: &Scope{
propagationContext: PropagationContext{
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"),
SpanID: SpanIDFromHex("a9f442f9330b4e09"),
},
},
client: func() *Client {
dsn, _ := NewDsn("http://[email protected]/sentry/1")
return &Client{
options: ClientOptions{
Dsn: dsn.String(),
Release: "1.0.0",
Environment: "production",
},
dsn: dsn,
}
}(),
expected: DynamicSamplingContext{
Entries: map[string]string{
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"release": "1.0.0",
"environment": "production",
},
Frozen: true,
},
},
"Nil client": {
scope: &Scope{
propagationContext: PropagationContext{
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"),
SpanID: SpanIDFromHex("a9f442f9330b4e09"),
},
},
client: nil,
expected: DynamicSamplingContext{
Entries: map[string]string{},
Frozen: false,
},
},
"Nil scope": {
scope: nil,
client: &Client{},
expected: DynamicSamplingContext{
Entries: map[string]string{},
Frozen: false,
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := DynamicSamplingContextFromScope(tt.scope, tt.client)
assertEqual(t, tt.expected, result)
})
}
}