forked from uber/tchannel-go
/
propagation.go
313 lines (275 loc) · 9.82 KB
/
propagation.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
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package testtracing
import (
"fmt"
"testing"
"time"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/testutils"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/jaeger-client-go"
"golang.org/x/net/context"
)
const (
// BaggageKey is used for testing baggage propagation
BaggageKey = "luggage"
// BaggageValue is used for testing baggage propagation
BaggageValue = "suitcase"
)
// TracingRequest tests tracing capabilities in a given server.
type TracingRequest struct {
// ForwardCount tells the server how many times to forward this request to itself recursively
ForwardCount int
}
// TracingResponse captures the trace info observed in the server and its downstream calls
type TracingResponse struct {
TraceID uint64
SpanID uint64
ParentID uint64
TracingEnabled bool
Child *TracingResponse
Luggage string
}
// ObserveSpan extracts an OpenTracing span from the context and populates the response.
func (r *TracingResponse) ObserveSpan(ctx context.Context) *TracingResponse {
if span := opentracing.SpanFromContext(ctx); span != nil {
if mockSpan, ok := span.(*mocktracer.MockSpan); ok {
sc := mockSpan.Context().(mocktracer.MockSpanContext)
r.TraceID = uint64(sc.TraceID)
r.SpanID = uint64(sc.SpanID)
r.ParentID = uint64(mockSpan.ParentID)
r.TracingEnabled = sc.Sampled
} else if span := tchannel.CurrentSpan(ctx); span != nil {
r.TraceID = span.TraceID()
r.SpanID = span.SpanID()
r.ParentID = span.ParentID()
r.TracingEnabled = span.Flags()&1 == 1
}
r.Luggage = span.BaggageItem(BaggageKey)
}
return r
}
// TraceHandler is a base class for testing tracing propagation
type TraceHandler struct {
Ch *tchannel.Channel
}
// HandleCall is used by handlers from different encodings as the main business logic.
// It respects the ForwardCount input parameter to make downstream calls, and returns
// a result containing the observed tracing span and the downstream results.
func (h *TraceHandler) HandleCall(
ctx context.Context,
req *TracingRequest,
downstream TracingCall,
) (*TracingResponse, error) {
var childResp *TracingResponse
if req.ForwardCount > 0 {
downstreamReq := &TracingRequest{ForwardCount: req.ForwardCount - 1}
if resp, err := downstream(ctx, downstreamReq); err == nil {
childResp = resp
} else {
return nil, err
}
}
resp := &TracingResponse{Child: childResp}
resp.ObserveSpan(ctx)
return resp, nil
}
// TracerType is a convenient enum to indicate which type of tracer is being used in the test.
// It is a string because it's printed as part of the test description in the logs.
type TracerType string
const (
// Noop is for the default no-op tracer from OpenTracing
Noop TracerType = "NOOP"
// Mock tracer, baggage-capable, non-Zipkin trace IDs
Mock TracerType = "MOCK"
// Jaeger is Uber's tracer, baggage-capable, Zipkin-style trace IDs
Jaeger TracerType = "JAEGER"
)
// TracingCall is used in a few other structs here
type TracingCall func(ctx context.Context, req *TracingRequest) (*TracingResponse, error)
// EncodingInfo describes the encoding used with tracing propagation test
type EncodingInfo struct {
Format tchannel.Format
HeadersSupported bool
}
// PropagationTestSuite is a collection of test cases for a certain encoding
type PropagationTestSuite struct {
Encoding EncodingInfo
Register func(t *testing.T, ch *tchannel.Channel) TracingCall
TestCases map[TracerType][]PropagationTestCase
}
// PropagationTestCase describes a single propagation test case and expected results
type PropagationTestCase struct {
ForwardCount int
TracingDisabled bool
ExpectedBaggage string
ExpectedSpanCount int
}
type tracerChoice struct {
tracerType TracerType
tracer opentracing.Tracer
spansRecorded func() int
resetSpans func()
isFake bool
zipkinCompatible bool
}
// Run executes the test cases in the test suite against 3 different tracer implementations
func (s *PropagationTestSuite) Run(t *testing.T) {
tests := []struct {
name string
run func(t *testing.T)
}{
{"Noop_Tracer", s.runWithNoopTracer},
{"Mock_Tracer", s.runWithMockTracer},
{"Jaeger_Tracer", s.runWithJaegerTracer},
}
for _, test := range tests {
t.Logf("Running with %s", test.name)
test.run(t)
}
}
func (s *PropagationTestSuite) runWithNoopTracer(t *testing.T) {
s.runWithTracer(t, tracerChoice{
tracer: nil, // will cause opentracing.GlobalTracer() to be used
tracerType: Noop,
spansRecorded: func() int { return 0 },
resetSpans: func() {},
isFake: true,
})
}
func (s *PropagationTestSuite) runWithMockTracer(t *testing.T) {
mockTracer := mocktracer.New()
s.runWithTracer(t, tracerChoice{
tracerType: Mock,
tracer: mockTracer,
spansRecorded: func() int {
return len(MockTracerSampledSpans(mockTracer))
},
resetSpans: func() {
mockTracer.Reset()
},
})
}
func (s *PropagationTestSuite) runWithJaegerTracer(t *testing.T) {
jaegerReporter := jaeger.NewInMemoryReporter()
jaegerTracer, jaegerCloser := jaeger.NewTracer(testutils.DefaultServerName,
jaeger.NewConstSampler(true),
jaegerReporter)
// To enable logging, use composite reporter:
// jaeger.NewCompositeReporter(jaegerReporter, jaeger.NewLoggingReporter(jaeger.StdLogger)))
defer jaegerCloser.Close()
s.runWithTracer(t, tracerChoice{
tracerType: Jaeger,
tracer: jaegerTracer,
spansRecorded: func() int {
return len(jaegerReporter.GetSpans())
},
resetSpans: func() {
jaegerReporter.Reset()
},
zipkinCompatible: true,
})
}
func (s *PropagationTestSuite) runWithTracer(t *testing.T, tracer tracerChoice) {
testCases, ok := s.TestCases[tracer.tracerType]
if !ok {
t.Logf("No test cases for encoding=%s and tracer=%s", s.Encoding.Format, tracer.tracerType)
return
}
opts := &testutils.ChannelOpts{
ChannelOptions: tchannel.ChannelOptions{Tracer: tracer.tracer},
DisableRelay: true,
}
ch := testutils.NewServer(t, opts)
defer ch.Close()
ch.Peers().Add(ch.PeerInfo().HostPort)
call := s.Register(t, ch)
for _, tt := range testCases {
s.runTestCase(t, tracer, ch, tt, call)
}
}
func (s *PropagationTestSuite) runTestCase(
t *testing.T,
tracer tracerChoice,
ch *tchannel.Channel,
test PropagationTestCase,
call TracingCall,
) {
descr := fmt.Sprintf("test %+v with tracer %+v", test, tracer)
ch.Logger().Debugf("Starting tracing test %s", descr)
tracer.resetSpans()
span := ch.Tracer().StartSpan("client")
span.SetBaggageItem(BaggageKey, BaggageValue)
ctx := opentracing.ContextWithSpan(context.Background(), span)
ctxBuilder := tchannel.NewContextBuilder(5 * time.Second).SetParentContext(ctx)
if test.TracingDisabled {
ctxBuilder.DisableTracing()
}
ctx, cancel := ctxBuilder.Build()
defer cancel()
req := &TracingRequest{ForwardCount: test.ForwardCount}
ch.Logger().Infof("Sending tracing request %+v", req)
response, err := call(ctx, req)
require.NoError(t, err)
ch.Logger().Infof("Received tracing response %+v", response)
// Spans are finished in inbound.doneSending() or outbound.doneReading(),
// which are called on different go-routines and may execute *after* the
// response has been received by the client. Give them a chance to run.
for i := 0; i < 1000; i++ {
if spanCount := tracer.spansRecorded(); spanCount == test.ExpectedSpanCount {
break
}
time.Sleep(testutils.Timeout(time.Millisecond))
}
spanCount := tracer.spansRecorded()
ch.Logger().Debugf("end span count: %d", spanCount)
// finish span after taking count of recorded spans, as we're only interested
// in the count of spans created by RPC calls.
span.Finish()
root := new(TracingResponse).ObserveSpan(ctx)
if !tracer.isFake {
assert.Equal(t, uint64(0), root.ParentID)
assert.NotEqual(t, uint64(0), root.TraceID)
}
assert.Equal(t, test.ExpectedSpanCount, spanCount, "Wrong span count; %s", descr)
for r, cnt := response, 0; r != nil || cnt <= test.ForwardCount; r, cnt = r.Child, cnt+1 {
require.NotNil(t, r, "Expecting response for forward=%d; %s", cnt, descr)
if !tracer.isFake {
if tracer.zipkinCompatible || s.Encoding.HeadersSupported {
assert.Equal(t, root.TraceID, r.TraceID, "traceID should be the same; %s", descr)
}
assert.Equal(t, test.ExpectedBaggage, r.Luggage, "baggage should propagate; %s", descr)
}
}
ch.Logger().Debugf("Finished tracing test %s", descr)
}
// MockTracerSampledSpans is a helper function that returns only sampled spans from MockTracer
func MockTracerSampledSpans(tracer *mocktracer.MockTracer) []*mocktracer.MockSpan {
var spans []*mocktracer.MockSpan
for _, span := range tracer.FinishedSpans() {
if span.Context().(mocktracer.MockSpanContext).Sampled {
spans = append(spans, span)
}
}
return spans
}