forked from jaegertracing/jaeger-client-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
span_test.go
115 lines (93 loc) · 3.29 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
// 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"
"testing"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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)
assert.Equal(t, tracer, sp1.Tracer())
assert.NotNil(t, sp1.Context())
}
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 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()
}