-
Notifications
You must be signed in to change notification settings - Fork 214
/
propagation_context_test.go
145 lines (130 loc) · 3.55 KB
/
propagation_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
package sentry
import (
"bytes"
"encoding/json"
"testing"
)
func TestPropagationContextMarshalJSON(t *testing.T) {
v := NewPropagationContext()
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
if bytes.Contains(b, []byte("parent_span_id")) {
t.Fatalf("unwanted parent_span_id: %s", b)
}
v.ParentSpanID = SpanIDFromHex("b72fa28504b07285")
b2, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(b2, []byte("parent_span_id")) {
t.Fatalf("missing parent_span_id: %s", b)
}
}
func TestPropagationContextMap(t *testing.T) {
p := NewPropagationContext()
assertEqual(t,
p.Map(),
map[string]interface{}{
"trace_id": p.TraceID,
"span_id": p.SpanID,
},
"without parent span id")
p.ParentSpanID = SpanIDFromHex("b72fa28504b07285")
assertEqual(t,
p.Map(),
map[string]interface{}{
"trace_id": p.TraceID,
"span_id": p.SpanID,
"parent_span_id": p.ParentSpanID,
},
"without praent span id")
}
func TestPropagationContextFromHeaders(t *testing.T) {
tests := []struct {
traceStr string
baggageStr string
want PropagationContext
}{
{
// No sentry-trace or baggage => nothing to do, unfrozen DSC
traceStr: "",
baggageStr: "",
want: PropagationContext{
DynamicSamplingContext: DynamicSamplingContext{
Frozen: false,
Entries: nil,
},
},
},
{
// Third-party baggage => nothing to do, unfrozen DSC
traceStr: "",
baggageStr: "other-vendor-key1=value1;value2, other-vendor-key2=value3",
want: PropagationContext{
DynamicSamplingContext: DynamicSamplingContext{
Frozen: false,
Entries: map[string]string{},
},
},
},
{
// sentry-trace and no baggage => we should create a new DSC and freeze it
// immediately.
traceStr: "bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1",
baggageStr: "",
want: PropagationContext{
TraceID: TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4"),
ParentSpanID: SpanIDFromHex("b72fa28504b07285"),
DynamicSamplingContext: DynamicSamplingContext{
Frozen: true,
},
},
},
{
traceStr: "bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1",
baggageStr: "sentry-trace_id=d49d9bf66f13450b81f65bc51cf49c03,sentry-public_key=public,sentry-sample_rate=1",
want: PropagationContext{
TraceID: TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4"),
ParentSpanID: SpanIDFromHex("b72fa28504b07285"),
DynamicSamplingContext: DynamicSamplingContext{
Frozen: true,
Entries: map[string]string{
"public_key": "public",
"sample_rate": "1",
"trace_id": "d49d9bf66f13450b81f65bc51cf49c03",
},
},
},
},
}
for _, tt := range tests {
p, err := PropagationContextFromHeaders(tt.traceStr, tt.baggageStr)
if err != nil {
t.Fatal(err)
}
if tt.want.TraceID != zeroTraceID && p.TraceID != tt.want.TraceID {
t.Errorf("got TraceID = %s, want %s", p.TraceID, tt.want.TraceID)
}
if p.TraceID == zeroTraceID {
t.Errorf("got TraceID = %s, want non-zero", p.TraceID)
}
if p.ParentSpanID != tt.want.ParentSpanID {
t.Errorf("got ParentSpanID = %s, want %s", p.ParentSpanID, tt.want.ParentSpanID)
}
if p.SpanID == zeroSpanID {
t.Errorf("got SpanID = %s, want non-zero", p.SpanID)
}
assertEqual(t, p.DynamicSamplingContext, tt.want.DynamicSamplingContext)
}
}
func TestNewPropagationContext(t *testing.T) {
context := NewPropagationContext()
if context.TraceID == zeroTraceID {
t.Errorf("TraceID should not be zero")
}
if context.SpanID == zeroSpanID {
t.Errorf("SpanID should not be zero")
}
}