github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/collector_test.go (about) 1 // (c) Copyright IBM Corp. 2023 2 3 package instana_test 4 5 import ( 6 "fmt" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 instana "github.com/instana/go-sensor" 12 ot "github.com/opentracing/opentracing-go" 13 "github.com/opentracing/opentracing-go/ext" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func Test_Collector_Noop(t *testing.T) { 19 assert.NotNil(t, instana.C, "instana.C should never be nil and be initialized as noop") 20 21 sc, err := instana.C.Extract(nil, nil) 22 assert.Nil(t, sc) 23 assert.Error(t, err) 24 assert.Nil(t, instana.C.StartSpan("")) 25 assert.Nil(t, instana.C.LegacySensor()) 26 } 27 28 func Test_Collector_LegacySensor(t *testing.T) { 29 recorder := instana.NewTestRecorder() 30 c := instana.InitCollector(&instana.Options{AgentClient: alwaysReadyClient{}, Recorder: recorder}) 31 s := c.LegacySensor() 32 defer instana.ShutdownSensor() 33 34 assert.NotNil(t, instana.C.LegacySensor()) 35 36 h := instana.TracingHandlerFunc(s, "/{action}", func(w http.ResponseWriter, req *http.Request) { 37 fmt.Fprintln(w, "Ok") 38 }) 39 40 req := httptest.NewRequest(http.MethodGet, "/foo", nil) 41 42 h.ServeHTTP(httptest.NewRecorder(), req) 43 44 assert.Len(t, recorder.GetQueuedSpans(), 1, "Instrumentations should still work fine with instana.C.LegacySensor()") 45 } 46 47 func Test_Collector_Singleton(t *testing.T) { 48 instana.C = nil 49 var ok bool 50 var instance instana.TracerLogger 51 52 _, ok = instana.C.(*instana.Collector) 53 assert.False(t, ok, "instana.C is noop before InitCollector is called") 54 55 instana.InitCollector(instana.DefaultOptions()) 56 57 instance, ok = instana.C.(*instana.Collector) 58 assert.True(t, ok, "instana.C is of type instana.Collector after InitCollector is called") 59 60 instana.InitCollector(instana.DefaultOptions()) 61 62 assert.Equal(t, instana.C, instance, "instana.C is singleton and should not be reassigned if InitCollector is called again") 63 } 64 65 func Test_Collector_EmbeddedTracer(t *testing.T) { 66 instana.C = nil 67 c := instana.InitCollector(nil) 68 69 sp := c.StartSpan("my-span") 70 71 carrier := ot.TextMapCarrier(make(map[string]string)) 72 73 err := c.Inject(sp.Context(), ot.TextMap, carrier) 74 assert.Nil(t, err) 75 76 sctx, err := c.Extract(ot.TextMap, carrier) 77 assert.Nil(t, err) 78 79 opt := ext.RPCServerOption(sctx) 80 opts := []ot.StartSpanOption{opt} 81 82 cs := c.StartSpan("child-span", opts...) 83 84 parentCtx, ok := sp.Context().(instana.SpanContext) 85 assert.True(t, ok) 86 87 childCtx, ok := cs.Context().(instana.SpanContext) 88 assert.True(t, ok) 89 90 assert.Equal(t, parentCtx.TraceID, childCtx.TraceID) 91 assert.Equal(t, parentCtx.SpanID, childCtx.ParentID) 92 } 93 94 func Test_Collector_Logger(t *testing.T) { 95 instana.C = nil 96 instana.InitCollector(nil) 97 98 l := &mylogger{} 99 100 instana.C.SetLogger(l) 101 102 instana.C.Debug() 103 instana.C.Info() 104 instana.C.Warn() 105 instana.C.Error() 106 instana.C.Error() 107 108 assert.Equal(t, 1, l.counter["debug"]) 109 assert.Equal(t, 1, l.counter["info"]) 110 assert.Equal(t, 1, l.counter["warn"]) 111 assert.Equal(t, 2, l.counter["error"]) 112 } 113 114 var _ instana.LeveledLogger = (*mylogger)(nil) 115 116 type mylogger struct { 117 counter map[string]int 118 } 119 120 func (l *mylogger) init() { 121 if l.counter == nil { 122 l.counter = make(map[string]int) 123 } 124 } 125 126 func (l *mylogger) Debug(v ...interface{}) { 127 l.init() 128 l.counter["debug"]++ 129 } 130 131 func (l *mylogger) Info(v ...interface{}) { 132 l.init() 133 l.counter["info"]++ 134 } 135 136 func (l *mylogger) Warn(v ...interface{}) { 137 l.init() 138 l.counter["warn"]++ 139 } 140 141 func (l *mylogger) Error(v ...interface{}) { 142 l.init() 143 l.counter["error"]++ 144 }