github.com/blend/go-sdk@v1.20220411.3/tracing/tracing_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package tracing
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"testing"
    14  
    15  	opentracing "github.com/opentracing/opentracing-go"
    16  	"github.com/opentracing/opentracing-go/mocktracer"
    17  
    18  	"github.com/blend/go-sdk/assert"
    19  	"github.com/blend/go-sdk/ex"
    20  )
    21  
    22  func TestStartSpanFromContext(t *testing.T) {
    23  	assert := assert.New(t)
    24  	mockTracer := mocktracer.New()
    25  
    26  	span, ctx := StartSpanFromContext(
    27  		context.Background(),
    28  		mockTracer,
    29  		"test.operation.one",
    30  		opentracing.Tags(map[string]interface{}{"k1": "v1"}),
    31  	)
    32  
    33  	mockSpan := span.(*mocktracer.MockSpan)
    34  	assert.Equal("test.operation.one", mockSpan.OperationName)
    35  	assert.Len(mockSpan.Tags(), 1)
    36  	assert.Equal("v1", mockSpan.Tags()["k1"])
    37  
    38  	spanFromCtx := opentracing.SpanFromContext(ctx)
    39  	mockSpanFromCtx := spanFromCtx.(*mocktracer.MockSpan)
    40  	assert.Equal(mockSpan.String(), mockSpanFromCtx.String())
    41  }
    42  
    43  func TestStartSpanFromContextWithParent(t *testing.T) {
    44  	assert := assert.New(t)
    45  	mockTracer := mocktracer.New()
    46  
    47  	// Start Parent Span from Background Context
    48  	parentSpan, ctx := StartSpanFromContext(
    49  		context.Background(),
    50  		mockTracer,
    51  		"test.operation.one",
    52  	)
    53  
    54  	mockParentSpan := parentSpan.(*mocktracer.MockSpan)
    55  
    56  	// Start Child Span from Context with Parent Span
    57  	span, ctx := StartSpanFromContext(
    58  		ctx,
    59  		mockTracer,
    60  		"test.operation.two",
    61  	)
    62  
    63  	mockSpan := span.(*mocktracer.MockSpan)
    64  	assert.Equal("test.operation.two", mockSpan.OperationName)
    65  
    66  	spanFromCtx := opentracing.SpanFromContext(ctx)
    67  	mockSpanFromCtx := spanFromCtx.(*mocktracer.MockSpan)
    68  	assert.Equal(mockSpan.String(), mockSpanFromCtx.String())
    69  
    70  	assert.Equal(mockSpan.ParentID, mockParentSpan.SpanContext.SpanID)
    71  }
    72  
    73  func TestGetTracingSpanFromContext(t *testing.T) {
    74  	assert := assert.New(t)
    75  	mockTracer := mocktracer.New()
    76  
    77  	spanKey := struct{}{}
    78  	span := mockTracer.StartSpan("test.operation")
    79  	ctx := context.Background()
    80  	ctx = context.WithValue(ctx, spanKey, span)
    81  
    82  	spanFromCtx := GetTracingSpanFromContext(ctx, spanKey)
    83  
    84  	mockSpan := span.(*mocktracer.MockSpan)
    85  	mockSpanFromCtx := spanFromCtx.(*mocktracer.MockSpan)
    86  	assert.Equal(mockSpan.String(), mockSpanFromCtx.String())
    87  }
    88  
    89  func TestGetTracingSpanFromContextMiss(t *testing.T) {
    90  	assert := assert.New(t)
    91  	mockTracer := mocktracer.New()
    92  
    93  	spanKey := struct{}{}
    94  	span := mockTracer.StartSpan("test.operation")
    95  	ctx := context.Background()
    96  	ctx = context.WithValue(ctx, spanKey, span)
    97  
    98  	spanFromCtx := GetTracingSpanFromContext(ctx, "wrongKey")
    99  	assert.Nil(spanFromCtx)
   100  }
   101  
   102  func TestSpanError(t *testing.T) {
   103  	assert := assert.New(t)
   104  	mockTracer := mocktracer.New()
   105  
   106  	// Non go-sdk/ex error
   107  	nonExErr := fmt.Errorf("Test Error")
   108  	span := mockTracer.StartSpan("test.operation")
   109  	SpanError(span, nonExErr)
   110  
   111  	mockSpan := span.(*mocktracer.MockSpan)
   112  	assert.Equal("Test Error", mockSpan.Tags()[TagKeyError])
   113  	assert.Nil(mockSpan.Tags()[TagKeyErrorMessage])
   114  	assert.Nil(mockSpan.Tags()[TagKeyErrorStack])
   115  
   116  	// go-sdk/ex error
   117  	nonExErr = ex.New("Test Ex Error").
   118  		WithMessage("Test Message")
   119  	span = mockTracer.StartSpan("test.operation")
   120  	SpanError(span, nonExErr)
   121  
   122  	mockSpan = span.(*mocktracer.MockSpan)
   123  	assert.Equal("Test Ex Error", mockSpan.Tags()[TagKeyError])
   124  	assert.Equal("Test Message", mockSpan.Tags()[TagKeyErrorMessage])
   125  	assert.NotNil(mockSpan.Tags()[TagKeyErrorStack])
   126  	assert.Contains(mockSpan.Tags()[TagKeyErrorStack].(string), "tracing_test.go")
   127  }