github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/tracer_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2017 3 4 package instana_test 5 6 import ( 7 "testing" 8 9 instana "github.com/instana/go-sensor" 10 ot "github.com/opentracing/opentracing-go" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestTracerAPI(t *testing.T) { 15 tracer := instana.NewTracer() 16 assert.NotNil(t, tracer) 17 18 recorder := instana.NewTestRecorder() 19 20 tracer = instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder) 21 defer instana.ShutdownSensor() 22 assert.NotNil(t, tracer) 23 24 tracer = instana.NewTracerWithOptions(&instana.Options{AgentClient: alwaysReadyClient{}}) 25 assert.NotNil(t, tracer) 26 } 27 28 func TestTracerBasics(t *testing.T) { 29 opts := instana.Options{LogLevel: instana.Debug, AgentClient: alwaysReadyClient{}} 30 recorder := instana.NewTestRecorder() 31 tracer := instana.NewTracerWithEverything(&opts, recorder) 32 defer instana.ShutdownSensor() 33 34 sp := tracer.StartSpan("test") 35 sp.SetBaggageItem("foo", "bar") 36 sp.Finish() 37 38 spans := recorder.GetQueuedSpans() 39 assert.Equal(t, len(spans), 1) 40 } 41 42 func TestTracer_StartSpan_SuppressTracing(t *testing.T) { 43 recorder := instana.NewTestRecorder() 44 tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder) 45 defer instana.ShutdownSensor() 46 47 sp := tracer.StartSpan("test", instana.SuppressTracing()) 48 49 sc := sp.Context().(instana.SpanContext) 50 assert.True(t, sc.Suppressed) 51 } 52 53 func TestTracer_StartSpan_WithCorrelationData(t *testing.T) { 54 recorder := instana.NewTestRecorder() 55 tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder) 56 defer instana.ShutdownSensor() 57 58 sp := tracer.StartSpan("test", ot.ChildOf(instana.SpanContext{ 59 Correlation: instana.EUMCorrelationData{ 60 Type: "type1", 61 ID: "id1", 62 }, 63 })) 64 65 sc := sp.Context().(instana.SpanContext) 66 assert.Equal(t, instana.EUMCorrelationData{}, sc.Correlation) 67 } 68 69 type strangeContext struct{} 70 71 func (c *strangeContext) ForeachBaggageItem(handler func(k, v string) bool) {} 72 73 func TestTracer_NonInstanaSpan(t *testing.T) { 74 tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, nil) 75 defer instana.ShutdownSensor() 76 77 ref := ot.SpanReference{ 78 Type: ot.ChildOfRef, 79 ReferencedContext: &strangeContext{}, 80 } 81 82 opts := ot.StartSpanOptions{ 83 References: []ot.SpanReference{ 84 ref, 85 }, 86 } 87 88 assert.NotPanics(t, func() { 89 tracer.StartSpanWithOptions("my_operation", opts) 90 }) 91 }