forked from jaegertracing/jaeger-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span_test.go
191 lines (151 loc) · 5.71 KB
/
span_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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed 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 jaeger
import (
"sync"
"sync/atomic"
"testing"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/jaeger-client-go/internal/throttler"
)
func TestBaggageIterator(t *testing.T) {
service := "DOOP"
tracer, closer := NewTracer(service, NewConstSampler(true), NewNullReporter())
defer closer.Close()
sp1 := tracer.StartSpan("s1").(*Span)
sp1.SetBaggageItem("Some_Key", "12345")
sp1.SetBaggageItem("Some-other-key", "42")
expectedBaggage := map[string]string{"Some_Key": "12345", "Some-other-key": "42"}
assertBaggage(t, sp1, expectedBaggage)
assertBaggageRecords(t, sp1, expectedBaggage)
b := extractBaggage(sp1, false) // break out early
assert.Equal(t, 1, len(b), "only one baggage item should be extracted")
sp2 := tracer.StartSpan("s2", opentracing.ChildOf(sp1.Context())).(*Span)
assertBaggage(t, sp2, expectedBaggage) // child inherits the same baggage
require.Len(t, sp2.logs, 0) // child doesn't inherit the baggage logs
}
func assertBaggageRecords(t *testing.T, sp *Span, expected map[string]string) {
require.Len(t, sp.logs, len(expected))
for _, logRecord := range sp.logs {
require.Len(t, logRecord.Fields, 3)
require.Equal(t, "event:baggage", logRecord.Fields[0].String())
key := logRecord.Fields[1].Value().(string)
value := logRecord.Fields[2].Value().(string)
require.Contains(t, expected, key)
assert.Equal(t, expected[key], value)
}
}
func assertBaggage(t *testing.T, sp opentracing.Span, expected map[string]string) {
b := extractBaggage(sp, true)
assert.Equal(t, expected, b)
}
func extractBaggage(sp opentracing.Span, allItems bool) map[string]string {
b := make(map[string]string)
sp.Context().ForeachBaggageItem(func(k, v string) bool {
b[k] = v
return allItems
})
return b
}
func TestSpanProperties(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()
sp1 := tracer.StartSpan("s1").(*Span)
sp1.SetTag("foo", "bar")
var expectedTags = make(opentracing.Tags)
expectedTags["foo"] = "bar"
expectedTags["sampler.type"] = "const"
expectedTags["sampler.param"] = true
assert.Equal(t, tracer, sp1.Tracer())
assert.NotNil(t, sp1.Context())
assert.Equal(t, sp1.context, sp1.SpanContext())
assert.Equal(t, sp1.startTime, sp1.StartTime())
assert.Equal(t, sp1.duration, sp1.Duration())
assert.Equal(t, sp1.Tags(), expectedTags)
}
func TestSpanOperationName(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()
sp1 := tracer.StartSpan("s1").(*Span)
sp1.SetOperationName("s2")
sp1.Finish()
assert.Equal(t, "s2", sp1.OperationName())
}
func TestSetTag_SamplingPriority(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter(),
TracerOptions.DebugThrottler(throttler.DefaultThrottler{}))
sp1 := tracer.StartSpan("s1").(*Span)
ext.SamplingPriority.Set(sp1, 0)
assert.False(t, sp1.context.IsDebug())
ext.SamplingPriority.Set(sp1, 1)
assert.True(t, sp1.context.IsDebug())
assert.NotNil(t, findDomainTag(sp1, "sampling.priority"), "sampling.priority tag should be added")
closer.Close()
tracer, closer = NewTracer("DOOP", NewConstSampler(true), NewNullReporter(),
TracerOptions.DebugThrottler(testThrottler{allowAll: false}))
defer closer.Close()
sp1 = tracer.StartSpan("s1").(*Span)
ext.SamplingPriority.Set(sp1, 1)
assert.False(t, sp1.context.IsDebug(), "debug should not be allowed by the throttler")
}
func TestSetFirehoseMode(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()
sp1 := tracer.StartSpan("s1").(*Span)
assert.False(t, sp1.context.IsFirehose())
EnableFirehose(sp1)
assert.True(t, sp1.context.IsFirehose())
}
type testThrottler struct {
allowAll bool
}
func (t testThrottler) IsAllowed(operation string) bool {
return t.allowAll
}
func TestBaggageContextRace(t *testing.T) {
tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter())
defer closer.Close()
sp1 := tracer.StartSpan("s1").(*Span)
var startWg, endWg sync.WaitGroup
startWg.Add(1)
endWg.Add(2)
f := func() {
startWg.Wait()
sp1.SetBaggageItem("x", "y")
sp1.Context().ForeachBaggageItem(func(k, v string) bool { return false })
endWg.Done()
}
go f()
go f()
startWg.Done()
endWg.Wait()
}
func TestSpanLifecycle(t *testing.T) {
service := "DOOP"
tracer, closer := NewTracer(service, NewConstSampler(true), NewNullReporter(), TracerOptions.PoolSpans(true))
// After closing all contexts will be released
defer closer.Close()
sp1 := tracer.StartSpan("s1").(*Span)
sp1.LogEvent("foo")
assert.True(t, sp1.tracer != nil, "invalid span initalisation")
sp1.Retain() // After this we are responsible for the span releasing
assert.Equal(t, int32(1), atomic.LoadInt32(&sp1.referenceCounter))
sp1.Finish() // The span is still alive
assert.True(t, sp1.tracer != nil, "invalid span finishing")
sp1.Release() // Now we will kill the object and return it in the pool
assert.True(t, sp1.tracer == nil, "span must be released")
}