github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/json_span_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package instana_test 5 6 import ( 7 "testing" 8 9 "github.com/instana/testify/assert" 10 "github.com/instana/testify/require" 11 instana "github.com/mier85/go-sensor" 12 "github.com/opentracing/opentracing-go" 13 "github.com/opentracing/opentracing-go/ext" 14 ) 15 16 func TestSpanKind_String(t *testing.T) { 17 examples := map[string]struct { 18 Kind instana.SpanKind 19 Expected string 20 }{ 21 "entry": { 22 Kind: instana.EntrySpanKind, 23 Expected: "entry", 24 }, 25 "exit": { 26 Kind: instana.ExitSpanKind, 27 Expected: "exit", 28 }, 29 "intermediate": { 30 Kind: instana.IntermediateSpanKind, 31 Expected: "intermediate", 32 }, 33 "unknown": { 34 Kind: instana.SpanKind(0), 35 Expected: "intermediate", 36 }, 37 } 38 39 for name, example := range examples { 40 t.Run(name, func(t *testing.T) { 41 assert.Equal(t, example.Expected, example.Kind.String()) 42 }) 43 } 44 } 45 46 func TestNewSDKSpanData(t *testing.T) { 47 recorder := instana.NewTestRecorder() 48 tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder) 49 50 sp := tracer.StartSpan("sdk", 51 ext.SpanKindRPCServer, 52 opentracing.Tags{ 53 "host": "localhost", 54 "custom.tag": "42", 55 }) 56 sp.Finish() 57 58 spans := recorder.GetQueuedSpans() 59 require.Len(t, spans, 1) 60 61 span := spans[0] 62 require.IsType(t, instana.SDKSpanData{}, span.Data) 63 64 data := span.Data.(instana.SDKSpanData) 65 assert.Equal(t, instana.SDKSpanTags{ 66 Name: "sdk", 67 Type: "entry", 68 Custom: map[string]interface{}{ 69 "tags": opentracing.Tags{ 70 "span.kind": ext.SpanKindRPCServerEnum, 71 "host": "localhost", 72 "custom.tag": "42", 73 }, 74 }, 75 }, data.Tags) 76 } 77 78 func TestSpanData_CustomTags(t *testing.T) { 79 recorder := instana.NewTestRecorder() 80 tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder) 81 82 sp := tracer.StartSpan("g.http", opentracing.Tags{ 83 "http.host": "localhost", 84 "http.path": "/", 85 "custom.tag": "42", 86 "another.tag": true, 87 }) 88 sp.Finish() 89 90 spans := recorder.GetQueuedSpans() 91 require.Len(t, spans, 1) 92 93 span := spans[0] 94 require.IsType(t, instana.HTTPSpanData{}, span.Data) 95 96 data := span.Data.(instana.HTTPSpanData) 97 98 assert.Equal(t, instana.HTTPSpanTags{ 99 Host: "localhost", 100 Path: "/", 101 }, data.Tags) 102 assert.Equal(t, &instana.CustomSpanData{ 103 Tags: map[string]interface{}{ 104 "custom.tag": "42", 105 "another.tag": true, 106 }, 107 }, data.Custom) 108 }