github.com/blend/go-sdk@v1.20220411.3/tracing/webtrace/tracer_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 webtrace
     9  
    10  import (
    11  	"fmt"
    12  	"testing"
    13  
    14  	opentracing "github.com/opentracing/opentracing-go"
    15  	"github.com/opentracing/opentracing-go/mocktracer"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  	"github.com/blend/go-sdk/tracing"
    19  	"github.com/blend/go-sdk/web"
    20  )
    21  
    22  func TestStart(t *testing.T) {
    23  	assert := assert.New(t)
    24  	mockTracer := mocktracer.New()
    25  	webTracer := Tracer(mockTracer)
    26  
    27  	ctx := web.MockCtx("GET", "/test-resource")
    28  	_ = webTracer.Start(ctx)
    29  
    30  	span := opentracing.SpanFromContext(ctx.Context())
    31  	mockSpan := span.(*mocktracer.MockSpan)
    32  	assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName)
    33  
    34  	assert.Len(mockSpan.Tags(), 8)
    35  	assert.Equal("GET /test-resource", mockSpan.Tags()[tracing.TagKeyResourceName])
    36  	assert.Equal(tracing.SpanTypeWeb, mockSpan.Tags()[tracing.TagKeySpanType])
    37  	assert.Equal("GET", mockSpan.Tags()[tracing.TagKeyHTTPMethod])
    38  	assert.Equal("/test-resource", mockSpan.Tags()[tracing.TagKeyHTTPURL])
    39  	assert.Equal("127.0.0.1", mockSpan.Tags()["http.remote_addr"])
    40  	assert.Equal("localhost", mockSpan.Tags()["http.host"])
    41  	assert.Equal("go-sdk test", mockSpan.Tags()["http.user_agent"])
    42  	assert.True(mockSpan.FinishTime.IsZero())
    43  }
    44  
    45  func TestStartWithRoute(t *testing.T) {
    46  	assert := assert.New(t)
    47  	mockTracer := mocktracer.New()
    48  	webTracer := Tracer(mockTracer)
    49  
    50  	ctx := web.MockCtx(
    51  		"GET", "/test-resource/3",
    52  		web.OptCtxRoute(&web.Route{Path: "/test-resource/:id"}),
    53  	)
    54  	_ = webTracer.Start(ctx)
    55  
    56  	span := opentracing.SpanFromContext(ctx.Context())
    57  	mockSpan := span.(*mocktracer.MockSpan)
    58  	assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName)
    59  
    60  	assert.Len(mockSpan.Tags(), 9)
    61  	assert.Equal("GET /test-resource/:id", mockSpan.Tags()[tracing.TagKeyResourceName])
    62  	assert.Equal(tracing.SpanTypeWeb, mockSpan.Tags()[tracing.TagKeySpanType])
    63  	assert.Equal("GET", mockSpan.Tags()[tracing.TagKeyHTTPMethod])
    64  	assert.Equal("/test-resource/3", mockSpan.Tags()[tracing.TagKeyHTTPURL])
    65  	assert.Equal("127.0.0.1", mockSpan.Tags()["http.remote_addr"])
    66  	assert.Equal("localhost", mockSpan.Tags()["http.host"])
    67  	assert.Equal("go-sdk test", mockSpan.Tags()["http.user_agent"])
    68  	assert.Equal("/test-resource/:id", mockSpan.Tags()["http.route"])
    69  	assert.True(mockSpan.FinishTime.IsZero())
    70  }
    71  
    72  func optCtxIncomingSpan(t opentracing.Tracer, s opentracing.Span) web.CtxOption {
    73  	return func(c *web.Ctx) {
    74  		_ = t.Inject(
    75  			s.Context(),
    76  			opentracing.HTTPHeaders,
    77  			opentracing.HTTPHeadersCarrier(c.Request.Header),
    78  		)
    79  	}
    80  }
    81  
    82  func TestStartWithParentSpan(t *testing.T) {
    83  	assert := assert.New(t)
    84  	mockTracer := mocktracer.New()
    85  	webTracer := Tracer(mockTracer)
    86  
    87  	parentSpan := mockTracer.StartSpan("test_op")
    88  	ctx := web.MockCtx(
    89  		"GET", "/test-resource",
    90  		optCtxIncomingSpan(mockTracer, parentSpan),
    91  	)
    92  	_ = webTracer.Start(ctx)
    93  
    94  	span := opentracing.SpanFromContext(ctx.Context())
    95  	mockSpan := span.(*mocktracer.MockSpan)
    96  	assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName)
    97  
    98  	mockParentSpan := parentSpan.(*mocktracer.MockSpan)
    99  	assert.Equal(mockSpan.ParentID, mockParentSpan.SpanContext.SpanID)
   100  }
   101  
   102  func TestFinish(t *testing.T) {
   103  	assert := assert.New(t)
   104  	mockTracer := mocktracer.New()
   105  	webTracer := Tracer(mockTracer)
   106  
   107  	ctx := web.MockCtx("GET", "/test-resource")
   108  	tf := webTracer.Start(ctx)
   109  
   110  	result := web.Raw([]byte("success"))
   111  	assert.Nil(result.Render(ctx))
   112  
   113  	tf.Finish(ctx, nil)
   114  
   115  	span := opentracing.SpanFromContext(ctx.Context())
   116  	mockSpan := span.(*mocktracer.MockSpan)
   117  	assert.Equal("200", mockSpan.Tags()[tracing.TagKeyHTTPCode])
   118  	assert.False(mockSpan.FinishTime.IsZero())
   119  }
   120  
   121  func TestFinishError(t *testing.T) {
   122  	assert := assert.New(t)
   123  	mockTracer := mocktracer.New()
   124  	webTracer := Tracer(mockTracer)
   125  
   126  	ctx := web.MockCtx("GET", "/test-resource")
   127  	tf := webTracer.Start(ctx)
   128  
   129  	result := web.Raw([]byte("success"))
   130  	result.StatusCode = 500
   131  	assert.Nil(result.Render(ctx))
   132  
   133  	tf.Finish(ctx, fmt.Errorf("error"))
   134  
   135  	span := opentracing.SpanFromContext(ctx.Context())
   136  	mockSpan := span.(*mocktracer.MockSpan)
   137  	assert.Equal("500", mockSpan.Tags()[tracing.TagKeyHTTPCode])
   138  	assert.Equal("error", mockSpan.Tags()[tracing.TagKeyError])
   139  	assert.False(mockSpan.FinishTime.IsZero())
   140  }
   141  
   142  func TestFinishNilSpan(t *testing.T) {
   143  	assert := assert.New(t)
   144  
   145  	ctx := web.MockCtx("GET", "/test-resource")
   146  	webTraceFinisher{}.Finish(ctx, nil)
   147  	assert.Nil(opentracing.SpanFromContext(ctx.Context()))
   148  }
   149  
   150  func TestStartView(t *testing.T) {
   151  	assert := assert.New(t)
   152  	mockTracer := mocktracer.New()
   153  	webViewTracer := Tracer(mockTracer).(web.ViewTracer)
   154  
   155  	ctx := web.MockCtx("GET", "/test-resource")
   156  	viewResult := &web.ViewResult{
   157  		ViewName:   "test_view",
   158  		StatusCode: 200,
   159  	}
   160  	wvtf := webViewTracer.StartView(ctx, viewResult)
   161  	span := wvtf.(*webViewTraceFinisher).span
   162  	mockSpan := span.(*mocktracer.MockSpan)
   163  	assert.Equal(tracing.OperationHTTPRender, mockSpan.OperationName)
   164  
   165  	assert.Len(mockSpan.Tags(), 3)
   166  	assert.Equal("test_view", mockSpan.Tags()[tracing.TagKeyResourceName])
   167  	assert.Equal(tracing.SpanTypeWeb, mockSpan.Tags()[tracing.TagKeySpanType])
   168  	assert.True(mockSpan.FinishTime.IsZero())
   169  }
   170  
   171  func TestStartViewWithParentSpan(t *testing.T) {
   172  	assert := assert.New(t)
   173  	mockTracer := mocktracer.New()
   174  	webViewTracer := Tracer(mockTracer).(web.ViewTracer)
   175  
   176  	parentSpan := mockTracer.StartSpan("test_op")
   177  	ctx := web.MockCtx("GET", "/test-resource")
   178  	ctx.WithContext(opentracing.ContextWithSpan(ctx.Context(), parentSpan))
   179  	viewResult := &web.ViewResult{
   180  		ViewName:   "test_view",
   181  		StatusCode: 200,
   182  	}
   183  	wvtf := webViewTracer.StartView(ctx, viewResult)
   184  	span := wvtf.(*webViewTraceFinisher).span
   185  	mockSpan := span.(*mocktracer.MockSpan)
   186  	assert.Equal(tracing.OperationHTTPRender, mockSpan.OperationName)
   187  
   188  	mockParentSpan := parentSpan.(*mocktracer.MockSpan)
   189  	assert.Equal(mockSpan.ParentID, mockParentSpan.SpanContext.SpanID)
   190  }
   191  
   192  func TestFinishView(t *testing.T) {
   193  	assert := assert.New(t)
   194  	mockTracer := mocktracer.New()
   195  	webViewTracer := Tracer(mockTracer).(web.ViewTracer)
   196  
   197  	ctx := web.MockCtx("GET", "/test-resource")
   198  	viewResult := &web.ViewResult{
   199  		ViewName:   "test_view",
   200  		StatusCode: 200,
   201  	}
   202  	wvtf := webViewTracer.StartView(ctx, viewResult)
   203  	wvtf.FinishView(ctx, viewResult, nil)
   204  
   205  	span := wvtf.(*webViewTraceFinisher).span
   206  	mockSpan := span.(*mocktracer.MockSpan)
   207  
   208  	assert.Nil(mockSpan.Tags()[tracing.TagKeyError])
   209  	assert.False(mockSpan.FinishTime.IsZero())
   210  }
   211  
   212  func TestFinishViewNilSpan(t *testing.T) {
   213  	assert := assert.New(t)
   214  
   215  	ctx := web.MockCtx("GET", "/test-resource")
   216  	webViewTraceFinisher{}.FinishView(ctx, nil, nil)
   217  	assert.Nil(opentracing.SpanFromContext(ctx.Context()))
   218  }