forked from SkyAPM/go2sky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_propagation_test.go
182 lines (162 loc) · 4.05 KB
/
trace_propagation_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
// Licensed to SkyAPM org under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. SkyAPM org licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package go2sky
import (
"context"
"sync"
"testing"
"github.com/SkyAPM/go2sky/propagation"
)
const (
sample = 1
traceID = "1f2d4bf47bf711eab794acde48001122"
parentSegmentID = "1e7c204a7bf711eab858acde48001122"
parentSpanID = 0
parentService = "service"
parentServiceInstance = "instance"
parentEndpoint = "/foo/bar"
addressUsedAtClient = "foo.svc:8787"
)
var header string
func init() {
scx := propagation.SpanContext{
Sample: sample,
TraceID: traceID,
ParentSegmentID: parentSegmentID,
ParentSpanID: parentSpanID,
ParentService: parentService,
ParentServiceInstance: parentServiceInstance,
ParentEndpoint: parentEndpoint,
AddressUsedAtClient: addressUsedAtClient,
}
header = scx.EncodeSW8()
}
func TestTracer_EntryAndExit(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(2)
tracer, err := NewTracer("service", WithReporter(&NoopReporter{wg: wg}))
if err != nil {
t.Error(err)
}
entrySpan, ctx, err := tracer.CreateEntrySpan(context.Background(), "/rest/api", func() (string, error) {
return "", nil
})
if err != nil {
t.Error(err)
}
exitSpan, err := tracer.CreateExitSpan(ctx, "/foo/bar", "foo.svc:8787", func(head string) error {
scx := propagation.SpanContext{}
err = scx.DecodeSW8(head)
if err != nil {
t.Fail()
}
return nil
})
if err != nil {
t.Error(err)
}
exitSpan.End()
entrySpan.End()
wg.Wait()
}
func TestTracer_Entry(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
reporter := &NoopReporter{wg: wg}
tracer, err := NewTracer("service", WithReporter(reporter))
if err != nil {
t.Error(err)
}
entrySpan, _, err := tracer.CreateEntrySpan(context.Background(), "/rest/api", func() (string, error) {
return header, nil
})
if err != nil {
t.Error(err)
}
entrySpan.End()
wg.Wait()
span := reporter.Spans[0]
if span.Context().TraceID != traceID {
t.Fail()
}
if len(span.Refs()) != 1 {
t.Fail()
}
}
func TestTracer_EntryAndExitInTrace(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(2)
tracer, err := NewTracer("service", WithInstance("instance"), WithReporter(&NoopReporter{wg: wg}))
if err != nil {
t.Error(err)
}
entrySpan, ctx, err := tracer.CreateEntrySpan(context.Background(), "/rest/api", func() (string, error) {
return header, nil
})
if err != nil {
t.Error(err)
}
exitSpan, err := tracer.CreateExitSpan(ctx, "/foo/bar", "foo.svc:8786", func(head string) error {
sc := propagation.SpanContext{}
err = sc.DecodeSW8(head)
if err != nil {
t.Fail()
}
if sc.Sample != sample {
t.Fail()
}
if sc.TraceID != traceID {
t.Fail()
}
if sc.ParentSpanID != 1 {
t.Fail()
}
if sc.ParentService != "service" {
t.Fail()
}
if sc.ParentServiceInstance != "instance" {
t.Fail()
}
if sc.ParentEndpoint != "/rest/api" {
t.Fail()
}
if sc.AddressUsedAtClient != "foo.svc:8786" {
t.Fail()
}
return nil
})
if err != nil {
t.Error(err)
}
exitSpan.End()
entrySpan.End()
wg.Wait()
}
type NoopReporter struct {
wg *sync.WaitGroup
Spans []ReportedSpan
}
func (*NoopReporter) Boot(service string, serviceInstance string) {
}
func (r *NoopReporter) Send(spans []ReportedSpan) {
r.Spans = spans
for range spans {
r.wg.Done()
}
}
func (NoopReporter) Close() {
}