github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/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  	"os"
     8  	"testing"
     9  
    10  	instana "github.com/instana/go-sensor"
    11  	"github.com/opentracing/opentracing-go"
    12  	"github.com/opentracing/opentracing-go/ext"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestSpanKind_String(t *testing.T) {
    18  	examples := map[string]struct {
    19  		Kind     instana.SpanKind
    20  		Expected string
    21  	}{
    22  		"entry": {
    23  			Kind:     instana.EntrySpanKind,
    24  			Expected: "entry",
    25  		},
    26  		"exit": {
    27  			Kind:     instana.ExitSpanKind,
    28  			Expected: "exit",
    29  		},
    30  		"intermediate": {
    31  			Kind:     instana.IntermediateSpanKind,
    32  			Expected: "intermediate",
    33  		},
    34  		"unknown": {
    35  			Kind:     instana.SpanKind(0),
    36  			Expected: "intermediate",
    37  		},
    38  	}
    39  
    40  	for name, example := range examples {
    41  		t.Run(name, func(t *testing.T) {
    42  			assert.Equal(t, example.Expected, example.Kind.String())
    43  		})
    44  	}
    45  }
    46  
    47  func TestServiceNameViaConfig(t *testing.T) {
    48  	recorder := instana.NewTestRecorder()
    49  	tracer := instana.NewTracerWithEverything(
    50  		&instana.Options{
    51  			AgentClient: alwaysReadyClient{},
    52  			Service:     "Service Name",
    53  		},
    54  		recorder,
    55  	)
    56  	defer instana.ShutdownSensor()
    57  	sp := tracer.StartSpan("g.http")
    58  	sp.Finish()
    59  	spans := recorder.GetQueuedSpans()
    60  
    61  	require.Len(t, spans, 1)
    62  	span := spans[0]
    63  	assert.Equal(t, "Service Name", span.Data.(instana.HTTPSpanData).SpanData.Service)
    64  	assert.Contains(t, spanToJson(t, span), "\"service\":\"Service Name\"")
    65  }
    66  
    67  func TestServiceNameViaEnvVar(t *testing.T) {
    68  	envVarOriginalValue, wasSet := os.LookupEnv("INSTANA_SERVICE_NAME")
    69  	os.Setenv("INSTANA_SERVICE_NAME", "Service Name")
    70  	defer func() {
    71  		if wasSet {
    72  			os.Setenv("INSTANA_SERVICE_NAME", envVarOriginalValue)
    73  		} else {
    74  			os.Unsetenv("INSTANA_SERVICE_NAME")
    75  		}
    76  	}()
    77  
    78  	recorder := instana.NewTestRecorder()
    79  	tracer := instana.NewTracerWithEverything(
    80  		&instana.Options{
    81  			AgentClient: alwaysReadyClient{},
    82  		},
    83  		recorder,
    84  	)
    85  	defer instana.ShutdownSensor()
    86  	sp := tracer.StartSpan("g.http")
    87  	sp.Finish()
    88  	spans := recorder.GetQueuedSpans()
    89  
    90  	require.Len(t, spans, 1)
    91  	span := spans[0]
    92  	assert.Equal(t, "Service Name", span.Data.(instana.HTTPSpanData).SpanData.Service)
    93  	assert.Contains(t, spanToJson(t, span), "\"service\":\"Service Name\"")
    94  }
    95  
    96  func TestServiceNameEmpty(t *testing.T) {
    97  	recorder := instana.NewTestRecorder()
    98  	tracer := instana.NewTracerWithEverything(
    99  		&instana.Options{
   100  			AgentClient: alwaysReadyClient{},
   101  		},
   102  		recorder,
   103  	)
   104  	defer instana.ShutdownSensor()
   105  	sp := tracer.StartSpan("g.http")
   106  	sp.Finish()
   107  	spans := recorder.GetQueuedSpans()
   108  
   109  	require.Len(t, spans, 1)
   110  	span := spans[0]
   111  	assert.Equal(t, "", span.Data.(instana.HTTPSpanData).SpanData.Service)
   112  	assert.NotContains(t, spanToJson(t, span), "\"service\":")
   113  }
   114  
   115  func spanToJson(t *testing.T, span instana.Span) string {
   116  	jsonBytes, err := span.MarshalJSON()
   117  	assert.NoError(t, err)
   118  	return string(jsonBytes)
   119  }
   120  
   121  func TestNewSDKSpanData(t *testing.T) {
   122  	recorder := instana.NewTestRecorder()
   123  	tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder)
   124  	defer instana.ShutdownSensor()
   125  
   126  	sp := tracer.StartSpan("sdk",
   127  		ext.SpanKindRPCServer,
   128  		opentracing.Tags{
   129  			"host":       "localhost",
   130  			"custom.tag": "42",
   131  		})
   132  	sp.Finish()
   133  
   134  	spans := recorder.GetQueuedSpans()
   135  	require.Len(t, spans, 1)
   136  
   137  	span := spans[0]
   138  	require.IsType(t, instana.SDKSpanData{}, span.Data)
   139  
   140  	data := span.Data.(instana.SDKSpanData)
   141  	assert.Equal(t, instana.SDKSpanTags{
   142  		Name: "sdk",
   143  		Type: "entry",
   144  		Custom: map[string]interface{}{
   145  			"tags": opentracing.Tags{
   146  				"span.kind":  ext.SpanKindRPCServerEnum,
   147  				"host":       "localhost",
   148  				"custom.tag": "42",
   149  			},
   150  		},
   151  	}, data.Tags)
   152  }
   153  
   154  func TestSpanData_CustomTags(t *testing.T) {
   155  	recorder := instana.NewTestRecorder()
   156  	tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder)
   157  	defer instana.ShutdownSensor()
   158  
   159  	sp := tracer.StartSpan("g.http", opentracing.Tags{
   160  		"http.host":   "localhost",
   161  		"http.path":   "/",
   162  		"custom.tag":  "42",
   163  		"another.tag": true,
   164  	})
   165  	sp.Finish()
   166  
   167  	spans := recorder.GetQueuedSpans()
   168  	require.Len(t, spans, 1)
   169  
   170  	span := spans[0]
   171  	require.IsType(t, instana.HTTPSpanData{}, span.Data)
   172  
   173  	data := span.Data.(instana.HTTPSpanData)
   174  
   175  	assert.Equal(t, instana.HTTPSpanTags{
   176  		Host: "localhost",
   177  		Path: "/",
   178  	}, data.Tags)
   179  	assert.Equal(t, &instana.CustomSpanData{
   180  		Tags: map[string]interface{}{
   181  			"custom.tag":  "42",
   182  			"another.tag": true,
   183  		},
   184  	}, data.Custom)
   185  }