-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcollector_test.go
173 lines (124 loc) · 3.99 KB
/
collector_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
// (c) Copyright IBM Corp. 2023
package instana_test
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
instana "github.com/instana/go-sensor"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
)
func Test_Collector_Noop(t *testing.T) {
c, err := instana.GetCollector()
assert.Error(t, err, "should return error as collector has not been initialized yet.")
assert.NotNil(t, c, "instana collector should never be nil and be initialized as noop")
sc, err := c.Extract(nil, nil)
assert.Nil(t, sc)
assert.Error(t, err)
assert.Nil(t, c.StartSpan(""))
assert.Nil(t, c.LegacySensor())
}
func Test_Collector_LegacySensor(t *testing.T) {
recorder := instana.NewTestRecorder()
c := instana.InitCollector(&instana.Options{AgentClient: alwaysReadyClient{}, Recorder: recorder})
s := c.LegacySensor()
defer instana.ShutdownCollector()
assert.NotNil(t, c.LegacySensor())
h := instana.TracingHandlerFunc(s, "/{action}", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "Ok")
})
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
h.ServeHTTP(httptest.NewRecorder(), req)
assert.Len(t, recorder.GetQueuedSpans(), 1, "Instrumentations should still work fine with LegacySensor() method")
}
func Test_Collector_Singleton(t *testing.T) {
var ok bool
var instance instana.TracerLogger
c, err := instana.GetCollector()
assert.Error(t, err, "should return error as collector has not been initialized yet.")
defer instana.ShutdownCollector()
_, ok = c.(*instana.Collector)
assert.False(t, ok, "instana collector is noop before InitCollector is called")
c = instana.InitCollector(instana.DefaultOptions())
instance, ok = c.(*instana.Collector)
assert.True(t, ok, "instana collector is of type instana.Collector after InitCollector is called")
c = instana.InitCollector(instana.DefaultOptions())
assert.Equal(t, c, instance, "instana collector is singleton and should not be reassigned if InitCollector is called again")
}
func Test_InitCollector_With_Goroutines(t *testing.T) {
defer instana.ShutdownCollector()
var wg sync.WaitGroup
wg.Add(3)
for i := 0; i < 3; i++ {
go func(id int) {
defer wg.Done()
c := instana.InitCollector(instana.DefaultOptions())
_, ok := c.(*instana.Collector)
assert.True(t, ok, "instana collector is of type instana.Collector after InitCollector is called")
assert.NotNil(t, c)
}(i)
}
wg.Wait()
}
func Test_Collector_EmbeddedTracer(t *testing.T) {
c := instana.InitCollector(nil)
defer instana.ShutdownCollector()
sp := c.StartSpan("my-span")
carrier := ot.TextMapCarrier(make(map[string]string))
err := c.Inject(sp.Context(), ot.TextMap, carrier)
assert.Nil(t, err)
sctx, err := c.Extract(ot.TextMap, carrier)
assert.Nil(t, err)
opt := ext.RPCServerOption(sctx)
opts := []ot.StartSpanOption{opt}
cs := c.StartSpan("child-span", opts...)
parentCtx, ok := sp.Context().(instana.SpanContext)
assert.True(t, ok)
childCtx, ok := cs.Context().(instana.SpanContext)
assert.True(t, ok)
assert.Equal(t, parentCtx.TraceID, childCtx.TraceID)
assert.Equal(t, parentCtx.SpanID, childCtx.ParentID)
}
func Test_Collector_Logger(t *testing.T) {
c := instana.InitCollector(nil)
defer instana.ShutdownCollector()
l := &mylogger{}
c.SetLogger(l)
c.Debug()
c.Info()
c.Warn()
c.Error()
c.Error()
assert.Equal(t, 1, l.counter["debug"])
assert.Equal(t, 1, l.counter["info"])
assert.Equal(t, 1, l.counter["warn"])
assert.Equal(t, 2, l.counter["error"])
}
var _ instana.LeveledLogger = (*mylogger)(nil)
type mylogger struct {
counter map[string]int
}
func (l *mylogger) init() {
if l.counter == nil {
l.counter = make(map[string]int)
}
}
func (l *mylogger) Debug(v ...interface{}) {
l.init()
l.counter["debug"]++
}
func (l *mylogger) Info(v ...interface{}) {
l.init()
l.counter["info"]++
}
func (l *mylogger) Warn(v ...interface{}) {
l.init()
l.counter["warn"]++
}
func (l *mylogger) Error(v ...interface{}) {
l.init()
l.counter["error"]++
}