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