-
Notifications
You must be signed in to change notification settings - Fork 39
/
collector_test.go
144 lines (104 loc) · 3.33 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
// (c) Copyright IBM Corp. 2023
package instana_test
import (
"fmt"
"net/http"
"net/http/httptest"
"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) {
assert.NotNil(t, instana.C, "instana.C should never be nil and be initialized as noop")
sc, err := instana.C.Extract(nil, nil)
assert.Nil(t, sc)
assert.Error(t, err)
assert.Nil(t, instana.C.StartSpan(""))
assert.Nil(t, instana.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.ShutdownSensor()
assert.NotNil(t, instana.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 instana.C.LegacySensor()")
}
func Test_Collector_Singleton(t *testing.T) {
instana.C = nil
var ok bool
var instance instana.TracerLogger
_, ok = instana.C.(*instana.Collector)
assert.False(t, ok, "instana.C is noop before InitCollector is called")
instana.InitCollector(instana.DefaultOptions())
instance, ok = instana.C.(*instana.Collector)
assert.True(t, ok, "instana.C is of type instana.Collector after InitCollector is called")
instana.InitCollector(instana.DefaultOptions())
assert.Equal(t, instana.C, instance, "instana.C is singleton and should not be reassigned if InitCollector is called again")
}
func Test_Collector_EmbeddedTracer(t *testing.T) {
instana.C = nil
c := instana.InitCollector(nil)
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) {
instana.C = nil
instana.InitCollector(nil)
l := &mylogger{}
instana.C.SetLogger(l)
instana.C.Debug()
instana.C.Info()
instana.C.Warn()
instana.C.Error()
instana.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"]++
}