forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing_test.go
420 lines (386 loc) · 10.7 KB
/
tracing_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package sentry
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"reflect"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TraceIDFromHex(s string) TraceID {
var id TraceID
_, err := hex.Decode(id[:], []byte(s))
if err != nil {
panic(err)
}
return id
}
func SpanIDFromHex(s string) SpanID {
var id SpanID
_, err := hex.Decode(id[:], []byte(s))
if err != nil {
panic(err)
}
return id
}
func TestSpanMarshalJSON(t *testing.T) {
s := &Span{}
testMarshalJSONOmitEmptyParentSpanID(t, s)
}
func TestSpanStatusMarshalJSON(t *testing.T) {
tests := map[SpanStatus]string{
SpanStatus(42): `null`,
SpanStatusUndefined: `null`,
SpanStatusOK: `"ok"`,
SpanStatusDeadlineExceeded: `"deadline_exceeded"`,
SpanStatusCanceled: `"cancelled"`,
}
for s, want := range tests {
s, want := s, want
t.Run(fmt.Sprintf("SpanStatus(%d)", s), func(t *testing.T) {
b, err := json.Marshal(s)
if err != nil {
t.Fatal(err)
}
got := string(b)
if got != want {
t.Fatalf("got %s, want %s", got, want)
}
})
}
}
func TestTraceContextMarshalJSON(t *testing.T) {
tc := &TraceContext{}
testMarshalJSONOmitEmptyParentSpanID(t, tc)
}
func testMarshalJSONOmitEmptyParentSpanID(t *testing.T, v interface{}) {
t.Helper()
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)
}
id := reflect.ValueOf(SpanIDFromHex("c7b73e77a3734fee"))
reflect.ValueOf(v).Elem().FieldByName("ParentSpanID").Set(id)
b, err = json.Marshal(v)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(b, []byte("parent_span_id")) {
t.Fatalf("missing parent_span_id: %s", b)
}
}
func TestStartSpan(t *testing.T) {
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
Transport: transport,
})
op := "test.op"
transaction := "Test Transaction"
description := "A Description"
status := SpanStatusOK
parentSpanID := SpanIDFromHex("f00db33f")
sampled := SampledTrue
startTime := time.Now()
endTime := startTime.Add(3 * time.Second)
data := map[string]interface{}{
"k": "v",
}
span := StartSpan(ctx, op,
TransactionName(transaction),
func(s *Span) {
s.Description = description
s.Status = status
s.ParentSpanID = parentSpanID
s.Sampled = sampled
s.StartTime = startTime
s.EndTime = endTime
s.Data = data
},
)
span.Finish()
SpanCheck{
Sampled: sampled,
RecorderLen: 1,
}.Check(t, span)
events := transport.Events()
if got := len(events); got != 1 {
t.Fatalf("sent %d events, want 1", got)
}
want := &Event{
Type: transactionType,
Transaction: transaction,
Contexts: map[string]Context{
"trace": TraceContext{
TraceID: span.TraceID,
SpanID: span.SpanID,
ParentSpanID: parentSpanID,
Op: op,
Description: description,
Status: status,
}.Map(),
},
Tags: nil,
// TODO(tracing): the root span / transaction data field is
// mapped into Event.Extra for now, pending spec clarification.
// https://github.com/getsentry/develop/issues/244#issuecomment-778694182
Extra: span.Data,
Timestamp: endTime,
StartTime: startTime,
}
opts := cmp.Options{
cmpopts.IgnoreFields(Event{},
"Contexts", "EventID", "Level", "Platform",
"Release", "Sdk", "ServerName", "Modules",
),
cmpopts.EquateEmpty(),
}
if diff := cmp.Diff(want, events[0], opts); diff != "" {
t.Fatalf("Event mismatch (-want +got):\n%s", diff)
}
// Check trace context explicitly, as we ignored all contexts above to
// disregard other contexts.
if diff := cmp.Diff(want.Contexts["trace"], events[0].Contexts["trace"]); diff != "" {
t.Fatalf("TraceContext mismatch (-want +got):\n%s", diff)
}
}
func TestStartChild(t *testing.T) {
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
TracesSampleRate: 1.0,
Transport: transport,
})
span := StartSpan(ctx, "top", TransactionName("Test Transaction"))
child := span.StartChild("child")
child.Finish()
span.Finish()
c := SpanCheck{
Sampled: SampledTrue,
RecorderLen: 2,
}
c.Check(t, span)
c.Check(t, child)
events := transport.Events()
if got := len(events); got != 1 {
t.Fatalf("sent %d events, want 1", got)
}
want := &Event{
Type: transactionType,
Transaction: "Test Transaction",
Contexts: map[string]Context{
"trace": TraceContext{
TraceID: span.TraceID,
SpanID: span.SpanID,
Op: span.Op,
}.Map(),
},
Spans: []*Span{
{
TraceID: child.TraceID,
SpanID: child.SpanID,
ParentSpanID: child.ParentSpanID,
Op: child.Op,
Sampled: SampledTrue,
},
},
}
opts := cmp.Options{
cmpopts.IgnoreFields(Event{},
"EventID", "Level", "Platform", "Modules",
"Release", "Sdk", "ServerName", "Timestamp", "StartTime",
),
cmpopts.IgnoreMapEntries(func(k string, v interface{}) bool {
return k != "trace"
}),
cmpopts.IgnoreFields(Span{},
"StartTime", "EndTime",
),
cmpopts.IgnoreUnexported(Span{}),
cmpopts.EquateEmpty(),
}
if diff := cmp.Diff(want, events[0], opts); diff != "" {
t.Fatalf("Event mismatch (-want +got):\n%s", diff)
}
}
// testContextKey is used to store a value in a context so that we can check
// that SDK operations on that context preserve the original context values.
type testContextKey struct{}
type testContextValue struct{}
func NewTestContext(options ClientOptions) context.Context {
client, err := NewClient(options)
if err != nil {
panic(err)
}
hub := NewHub(client, NewScope())
ctx := context.WithValue(context.Background(), testContextKey{}, testContextValue{})
return SetHubOnContext(ctx, hub)
}
// A SpanCheck is a test helper describing span properties that can be checked
// with the Check method.
type SpanCheck struct {
Sampled Sampled
ZeroTraceID bool
ZeroSpanID bool
RecorderLen int
}
func (c SpanCheck) Check(t *testing.T, span *Span) {
t.Helper()
// Invariant: original context values are preserved
gotCtx := span.Context()
if _, ok := gotCtx.Value(testContextKey{}).(testContextValue); !ok {
t.Errorf("original context value lost")
}
// Invariant: SpanFromContext(span.Context) == span
if spanFromContext(gotCtx) != span {
t.Errorf("span not in its context")
}
if got := span.TraceID == zeroTraceID; got != c.ZeroTraceID {
want := "zero"
if !c.ZeroTraceID {
want = "non-" + want
}
t.Errorf("got TraceID = %s, want %s", span.TraceID, want)
}
if got := span.SpanID == zeroSpanID; got != c.ZeroSpanID {
want := "zero"
if !c.ZeroSpanID {
want = "non-" + want
}
t.Errorf("got SpanID = %s, want %s", span.SpanID, want)
}
if got, want := span.Sampled, c.Sampled; got != want {
t.Errorf("got Sampled = %v, want %v", got, want)
}
if got, want := len(span.spanRecorder().spans), c.RecorderLen; got != want {
t.Errorf("got %d spans in recorder, want %d", got, want)
}
if span.StartTime.IsZero() {
t.Error("start time not set")
}
if span.EndTime.IsZero() {
t.Error("end time not set")
}
if span.EndTime.Before(span.StartTime) {
t.Error("end time before start time")
}
}
func TestToSentryTrace(t *testing.T) {
tests := []struct {
span *Span
want string
}{
{&Span{}, "00000000000000000000000000000000-0000000000000000"},
{&Span{Sampled: SampledTrue}, "00000000000000000000000000000000-0000000000000000-1"},
{&Span{Sampled: SampledFalse}, "00000000000000000000000000000000-0000000000000000-0"},
{&Span{TraceID: TraceID{1}}, "01000000000000000000000000000000-0000000000000000"},
{&Span{SpanID: SpanID{1}}, "00000000000000000000000000000000-0100000000000000"},
}
for _, tt := range tests {
if got := tt.span.ToSentryTrace(); got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
}
}
func TestContinueSpanFromRequest(t *testing.T) {
traceID := TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4")
spanID := SpanIDFromHex("b72fa28504b07285")
for _, sampled := range []Sampled{SampledTrue, SampledFalse, SampledUndefined} {
sampled := sampled
t.Run(sampled.String(), func(t *testing.T) {
var s Span
hkey := http.CanonicalHeaderKey("sentry-trace")
hval := (&Span{
TraceID: traceID,
SpanID: spanID,
Sampled: sampled,
}).ToSentryTrace()
header := http.Header{hkey: []string{hval}}
ContinueFromRequest(&http.Request{Header: header})(&s)
if s.TraceID != traceID {
t.Errorf("got %q, want %q", s.TraceID, traceID)
}
if s.ParentSpanID != spanID {
t.Errorf("got %q, want %q", s.ParentSpanID, spanID)
}
if s.Sampled != sampled {
t.Errorf("got %q, want %q", s.Sampled, sampled)
}
})
}
}
func TestContinueSpanFromTrace(t *testing.T) {
traceID := TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4")
spanID := SpanIDFromHex("b72fa28504b07285")
for _, sampled := range []Sampled{SampledTrue, SampledFalse, SampledUndefined} {
sampled := sampled
t.Run(sampled.String(), func(t *testing.T) {
var s Span
trace := (&Span{
TraceID: traceID,
SpanID: spanID,
Sampled: sampled,
}).ToSentryTrace()
ContinueFromTrace(trace)(&s)
if s.TraceID != traceID {
t.Errorf("got %q, want %q", s.TraceID, traceID)
}
if s.ParentSpanID != spanID {
t.Errorf("got %q, want %q", s.ParentSpanID, spanID)
}
if s.Sampled != sampled {
t.Errorf("got %q, want %q", s.Sampled, sampled)
}
})
}
}
func TestSpanFromContext(t *testing.T) {
// SpanFromContext always returns a non-nil value, such that you can use
// it without nil checks.
// When no span was in the context, the returned value is a no-op.
// Calling StartChild on the no-op creates a valid transaction.
// SpanFromContext(ctx).StartChild(...) === StartSpan(ctx, ...)
ctx := NewTestContext(ClientOptions{})
span := spanFromContext(ctx)
_ = span
// SpanCheck{
// ZeroTraceID: true,
// ZeroSpanID: true,
// }.Check(t, span)
// // Should create a transaction
// child := span.StartChild("top")
// SpanCheck{
// RecorderLen: 1,
// }.Check(t, child)
}
func TestDoubleSampling(t *testing.T) {
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
SampleRate: math.SmallestNonzeroFloat64,
TracesSampleRate: 1.0,
Transport: transport,
})
span := StartSpan(ctx, "op", TransactionName("name"))
// CaptureException should not send any event because of SampleRate.
GetHubFromContext(ctx).CaptureException(errors.New("ignored"))
if got := len(transport.Events()); got != 0 {
t.Fatalf("got %d events, want 0", got)
}
// Finish should send one transaction event, always sampled via
// TracesSampleRate.
span.Finish()
if got := len(transport.Events()); got != 1 {
t.Fatalf("got %d events, want 1", got)
}
if got := transport.Events()[0].Type; got != transactionType {
t.Fatalf("got %v event, want %v", got, transactionType)
}
}