github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/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  	"github.com/instana/testify/assert"
    10  	instana "github.com/mier85/go-sensor"
    11  	ot "github.com/opentracing/opentracing-go"
    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{}, recorder)
    21  	assert.NotNil(t, tracer)
    22  
    23  	tracer = instana.NewTracerWithOptions(&instana.Options{})
    24  	assert.NotNil(t, tracer)
    25  }
    26  
    27  func TestTracerBasics(t *testing.T) {
    28  	opts := instana.Options{LogLevel: instana.Debug}
    29  	recorder := instana.NewTestRecorder()
    30  	tracer := instana.NewTracerWithEverything(&opts, recorder)
    31  
    32  	sp := tracer.StartSpan("test")
    33  	sp.SetBaggageItem("foo", "bar")
    34  	sp.Finish()
    35  
    36  	spans := recorder.GetQueuedSpans()
    37  	assert.Equal(t, len(spans), 1)
    38  }
    39  
    40  func TestTracer_StartSpan_SuppressTracing(t *testing.T) {
    41  	recorder := instana.NewTestRecorder()
    42  	tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder)
    43  
    44  	sp := tracer.StartSpan("test", instana.SuppressTracing())
    45  
    46  	sc := sp.Context().(instana.SpanContext)
    47  	assert.True(t, sc.Suppressed)
    48  }
    49  
    50  func TestTracer_StartSpan_WithCorrelationData(t *testing.T) {
    51  	recorder := instana.NewTestRecorder()
    52  	tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder)
    53  
    54  	sp := tracer.StartSpan("test", ot.ChildOf(instana.SpanContext{
    55  		Correlation: instana.EUMCorrelationData{
    56  			Type: "type1",
    57  			ID:   "id1",
    58  		},
    59  	}))
    60  
    61  	sc := sp.Context().(instana.SpanContext)
    62  	assert.Equal(t, instana.EUMCorrelationData{}, sc.Correlation)
    63  }
    64  
    65  type strangeContext struct{}
    66  
    67  func (c *strangeContext) ForeachBaggageItem(handler func(k, v string) bool) {}
    68  
    69  func TestTracer_NonInstanaSpan(t *testing.T) {
    70  	tracer := instana.NewTracerWithEverything(&instana.Options{}, nil)
    71  
    72  	ref := ot.SpanReference{
    73  		Type:              ot.ChildOfRef,
    74  		ReferencedContext: &strangeContext{},
    75  	}
    76  
    77  	opts := ot.StartSpanOptions{
    78  		References: []ot.SpanReference{
    79  			ref,
    80  		},
    81  	}
    82  
    83  	assert.NotPanics(t, func() {
    84  		tracer.StartSpanWithOptions("my_operation", opts)
    85  	})
    86  }