github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/adapters_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package instana_test 5 6 import ( 7 "net/http/httptest" 8 "testing" 9 10 "github.com/instana/testify/assert" 11 "github.com/instana/testify/require" 12 instana "github.com/mier85/go-sensor" 13 ot "github.com/opentracing/opentracing-go" 14 "github.com/opentracing/opentracing-go/ext" 15 ) 16 17 func TestWithTracingSpan(t *testing.T) { 18 recorder := instana.NewTestRecorder() 19 s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{}, recorder)) 20 21 rec := httptest.NewRecorder() 22 req := httptest.NewRequest("GET", "/test", nil) 23 24 s.WithTracingSpan("test-span", rec, req, func(sp ot.Span) { 25 sp.SetTag("custom-tag", "value") 26 }) 27 28 spans := recorder.GetQueuedSpans() 29 require.Len(t, spans, 1) 30 31 span := spans[0] 32 assert.Empty(t, span.ParentID) 33 assert.Equal(t, 0, span.Ec) 34 35 require.IsType(t, instana.SDKSpanData{}, span.Data) 36 data := span.Data.(instana.SDKSpanData) 37 38 assert.Equal(t, "test-span", data.Tags.Name) 39 assert.Equal(t, "entry", data.Tags.Type) 40 41 assert.Equal(t, map[string]interface{}{ 42 "tags": ot.Tags{ 43 "http.method": "GET", 44 "http.url": "/test", 45 "peer.hostname": "example.com", 46 "span.kind": ext.SpanKindRPCServerEnum, 47 "custom-tag": "value", 48 }, 49 }, data.Tags.Custom) 50 } 51 52 func TestWithTracingSpan_PanicHandling(t *testing.T) { 53 recorder := instana.NewTestRecorder() 54 s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{}, recorder)) 55 56 rec := httptest.NewRecorder() 57 req := httptest.NewRequest("GET", "/test", nil) 58 59 require.Panics(t, func() { 60 s.WithTracingSpan("test-span", rec, req, func(sp ot.Span) { 61 panic("something went wrong") 62 }) 63 }) 64 65 spans := recorder.GetQueuedSpans() 66 require.Len(t, spans, 2) 67 68 span, logSpan := spans[0], spans[1] 69 assert.Empty(t, span.ParentID) 70 assert.Equal(t, 1, span.Ec) 71 72 require.IsType(t, instana.SDKSpanData{}, span.Data) 73 data := span.Data.(instana.SDKSpanData) 74 75 assert.Equal(t, "test-span", data.Tags.Name) 76 assert.Equal(t, "entry", data.Tags.Type) 77 78 assert.Len(t, data.Tags.Custom, 1) 79 assert.Equal(t, ot.Tags{ 80 "http.method": "GET", 81 "http.url": "/test", 82 "peer.hostname": "example.com", 83 "span.kind": ext.SpanKindRPCServerEnum, 84 }, data.Tags.Custom["tags"]) 85 86 assert.Equal(t, span.TraceID, logSpan.TraceID) 87 assert.Equal(t, span.SpanID, logSpan.ParentID) 88 assert.Equal(t, "log.go", logSpan.Name) 89 90 // assert that log message has been recorded within the span interval 91 assert.GreaterOrEqual(t, logSpan.Timestamp, span.Timestamp) 92 assert.LessOrEqual(t, logSpan.Duration, span.Duration) 93 94 require.IsType(t, instana.LogSpanData{}, logSpan.Data) 95 logData := logSpan.Data.(instana.LogSpanData) 96 97 assert.Equal(t, instana.LogSpanTags{ 98 Level: "ERROR", 99 Message: `error: "something went wrong"`, 100 }, logData.Tags) 101 } 102 103 func TestWithTracingSpan_WithActiveParentSpan(t *testing.T) { 104 recorder := instana.NewTestRecorder() 105 tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder) 106 s := instana.NewSensorWithTracer(tracer) 107 108 rec := httptest.NewRecorder() 109 req := httptest.NewRequest("GET", "/test", nil) 110 111 parentSpan := tracer.StartSpan("parent-span") 112 ctx := instana.ContextWithSpan(req.Context(), parentSpan) 113 114 s.WithTracingSpan("test-span", rec, req.WithContext(ctx), func(sp ot.Span) {}) 115 parentSpan.Finish() 116 117 spans := recorder.GetQueuedSpans() 118 require.Len(t, spans, 2) 119 120 assert.Equal(t, spans[1].TraceID, spans[0].TraceID) 121 assert.Equal(t, spans[1].SpanID, spans[0].ParentID) 122 } 123 124 func TestWithTracingSpan_WithWireContext(t *testing.T) { 125 recorder := instana.NewTestRecorder() 126 s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{}, recorder)) 127 128 rec := httptest.NewRecorder() 129 req := httptest.NewRequest("GET", "/test", nil) 130 traceID := instana.FormatID(1234567890) 131 parentSpanID := instana.FormatID(1) 132 133 req.Header.Set(instana.FieldT, traceID) 134 req.Header.Set(instana.FieldS, parentSpanID) 135 136 s.WithTracingSpan("test-span", rec, req, func(sp ot.Span) {}) 137 138 spans := recorder.GetQueuedSpans() 139 require.Len(t, spans, 1) 140 141 assert.Equal(t, int64(1234567890), spans[0].TraceID) 142 assert.Equal(t, int64(1), spans[0].ParentID) 143 } 144 145 func TestWithTracingContext(t *testing.T) {}