/
dynamic_sampling_context_test.go
130 lines (122 loc) · 3.18 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
package sentry
import (
"testing"
)
func TestDynamicSamplingContextFromHeader(t *testing.T) {
tests := []struct {
input []byte
want DynamicSamplingContext
}{
{
input: []byte(""),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{},
},
},
{
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",
},
},
},
{
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",
},
},
},
}
for _, tc := range tests {
got, err := DynamicSamplingContextFromHeader(tc.input)
if err != nil {
t.Fatal(err)
}
assertEqual(t, got, tc.want)
}
}
func TestDynamicSamplingContextFromTransaction(t *testing.T) {
tests := []struct {
input *Span
want DynamicSamplingContext
}{
// Normal flow
{
input: func() *Span {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 0.5,
Dsn: "http://public@example.com/sentry/1",
Release: "1.0.0",
Environment: "test",
})
hubFromContext(ctx).ConfigureScope(func(scope *Scope) {
scope.SetUser(User{Segment: "user_segment"})
})
txn := StartTransaction(ctx, "name", TransctionSource(SourceCustom))
txn.TraceID = TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03")
return txn
}(),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"sample_rate": "0.5",
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"release": "1.0.0",
"environment": "test",
"transaction": "name",
"user_segment": "user_segment",
},
},
},
// Transaction with source url, do not include in Dynamic Sampling context
{
input: func() *Span {
ctx := NewTestContext(ClientOptions{
EnableTracing: true,
TracesSampleRate: 0.5,
Dsn: "http://public@example.com/sentry/1",
Release: "1.0.0",
})
txn := StartTransaction(ctx, "name")
txn.TraceID = TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03")
return txn
}(),
want: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"sample_rate": "0.5",
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
"public_key": "public",
"release": "1.0.0",
},
},
},
}
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)
}