forked from openzipkin-contrib/zipkin-go-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagation_test.go
144 lines (126 loc) · 4.19 KB
/
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
package zipkintracer_test
import (
"bytes"
"net/http"
"reflect"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
opentracing "github.com/opentracing/opentracing-go"
zipkintracer "github.com/openzipkin/zipkin-go-opentracing"
"github.com/openzipkin/zipkin-go-opentracing/flag"
"github.com/openzipkin/zipkin-go-opentracing/types"
)
type verbatimCarrier struct {
zipkintracer.SpanContext
b map[string]string
}
var _ zipkintracer.DelegatingCarrier = &verbatimCarrier{}
func (vc *verbatimCarrier) SetBaggageItem(k, v string) {
vc.b[k] = v
}
func (vc *verbatimCarrier) GetBaggage(f func(string, string)) {
for k, v := range vc.b {
f(k, v)
}
}
func (vc *verbatimCarrier) SetState(tID types.TraceID, sID uint64, pID *uint64, sampled bool, flags flag.Flags) {
vc.SpanContext = zipkintracer.SpanContext{
TraceID: tID,
SpanID: sID,
ParentSpanID: pID,
Sampled: sampled,
Flags: flags,
}
}
func (vc *verbatimCarrier) State() (traceID types.TraceID, spanID uint64, parentSpanID *uint64, sampled bool, flags flag.Flags) {
return vc.SpanContext.TraceID, vc.SpanContext.SpanID, vc.SpanContext.ParentSpanID, vc.SpanContext.Sampled, vc.SpanContext.Flags
}
func TestSpanPropagator(t *testing.T) {
const op = "test"
recorder := zipkintracer.NewInMemoryRecorder()
tracer, err := zipkintracer.NewTracer(
recorder,
zipkintracer.ClientServerSameSpan(true),
zipkintracer.DebugMode(true),
zipkintracer.TraceID128Bit(true),
)
if err != nil {
t.Fatalf("Unable to create Tracer: %+v", err)
}
// create root span so propagation test will include parentSpanID
ps := tracer.StartSpan("root")
defer ps.Finish()
// client side span with parent span 'ps'
sp := tracer.StartSpan(op, opentracing.ChildOf(ps.Context()))
sp.SetBaggageItem("foo", "bar")
tmc := opentracing.HTTPHeadersCarrier(http.Header{})
tests := []struct {
typ, carrier interface{}
}{
{zipkintracer.Delegator, zipkintracer.DelegatingCarrier(&verbatimCarrier{b: map[string]string{}})},
{opentracing.Binary, &bytes.Buffer{}},
{opentracing.HTTPHeaders, tmc},
{opentracing.TextMap, tmc},
}
for i, test := range tests {
if err := tracer.Inject(sp.Context(), test.typ, test.carrier); err != nil {
t.Fatalf("%d: %v", i, err)
}
injectedContext, err := tracer.Extract(test.typ, test.carrier)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
child := tracer.StartSpan(
op,
opentracing.ChildOf(injectedContext))
child.Finish()
}
sp.Finish()
spans := recorder.GetSpans()
if a, e := len(spans), len(tests)+1; a != e {
t.Fatalf("expected %d spans, got %d", e, a)
}
// The last span is the original one.
exp, spans := spans[len(spans)-1], spans[:len(spans)-1]
exp.Duration = time.Duration(123)
exp.Start = time.Time{}.Add(1)
for i, sp := range spans {
if a, e := *sp.Context.ParentSpanID, exp.Context.SpanID; a != e {
t.Fatalf("%d: ParentSpanID %d does not match expectation %d", i, a, e)
} else {
// Prepare for comparison.
sp.Context.Flags &= flag.Debug // other flags then Debug should be discarded in comparison
exp.Context.Flags &= flag.Debug // other flags then Debug should be discarded in comparison
sp.Context.SpanID, sp.Context.ParentSpanID = exp.Context.SpanID, exp.Context.ParentSpanID
sp.Duration, sp.Start = exp.Duration, exp.Start
}
if a, e := sp.Context.TraceID, exp.Context.TraceID; a != e {
t.Fatalf("%d: TraceID changed from %d to %d", i, e, a)
}
if exp.Context.ParentSpanID == nil {
t.Fatalf("%d: Expected a ParentSpanID, got nil", i)
}
if p, c := sp.Context.ParentSpanID, exp.Context.ParentSpanID; p != c {
t.Fatalf("%d: ParentSpanID changed from %d to %d", i, p, c)
}
if !reflect.DeepEqual(exp, sp) {
t.Fatalf("%d: wanted %+v, got %+v", i, spew.Sdump(exp), spew.Sdump(sp))
}
}
}
func TestInvalidCarrier(t *testing.T) {
recorder := zipkintracer.NewInMemoryRecorder()
tracer, err := zipkintracer.NewTracer(
recorder,
zipkintracer.ClientServerSameSpan(true),
zipkintracer.DebugMode(true),
zipkintracer.TraceID128Bit(true),
)
if err != nil {
t.Fatalf("Unable to create Tracer: %+v", err)
}
if _, err = tracer.Extract(zipkintracer.Delegator, "invalid carrier"); err == nil {
t.Fatalf("Expected: %s, got nil", opentracing.ErrInvalidCarrier)
}
}