github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/recorder_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/opentracing/opentracing-go/ext" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestRecorderBasics(t *testing.T) { 17 18 recorder := instana.NewTestRecorder() 19 tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder) 20 defer instana.ShutdownSensor() 21 22 pSpan := tracer.StartSpan("parent-span") 23 span := tracer.StartSpan("http-client", ot.ChildOf(pSpan.Context())) 24 span.SetTag(string(ext.SpanKind), "exit") 25 span.SetTag("http.status", 200) 26 span.SetTag("http.url", "https://www.instana.com/product/") 27 span.SetTag(string(ext.HTTPMethod), "GET") 28 span.Finish() 29 30 // Validate GetQueuedSpans returns queued spans and clears the queue 31 spans := recorder.GetQueuedSpans() 32 assert.Len(t, spans, 1) 33 assert.Equal(t, 0, recorder.QueuedSpansCount()) 34 } 35 36 func TestRecorder_BatchSpan(t *testing.T) { 37 recorder := instana.NewTestRecorder() 38 tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder) 39 defer instana.ShutdownSensor() 40 41 tracer.StartSpan("test-span", instana.BatchSize(2)).Finish() 42 43 spans := recorder.GetQueuedSpans() 44 require.Len(t, spans, 1) 45 46 require.NotNil(t, spans[0].Batch) 47 assert.Equal(t, 2, spans[0].Batch.Size) 48 } 49 50 func TestRecorder_BatchSpan_Single(t *testing.T) { 51 recorder := instana.NewTestRecorder() 52 tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder) 53 defer instana.ShutdownSensor() 54 55 tracer.StartSpan("test-span", instana.BatchSize(1)).Finish() 56 57 spans := recorder.GetQueuedSpans() 58 require.Len(t, spans, 1) 59 60 assert.Nil(t, spans[0].Batch) 61 }