github.com/blend/go-sdk@v1.20220411.3/tracing/httptrace/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 httptrace_test 9 10 import ( 11 "context" 12 "fmt" 13 "net/http" 14 "testing" 15 "time" 16 17 opentracing "github.com/opentracing/opentracing-go" 18 "github.com/opentracing/opentracing-go/mocktracer" 19 20 "github.com/blend/go-sdk/assert" 21 "github.com/blend/go-sdk/tracing" 22 "github.com/blend/go-sdk/tracing/httptrace" 23 "github.com/blend/go-sdk/webutil" 24 ) 25 26 func TestStartHTTPSpan(t *testing.T) { 27 assert := assert.New(t) 28 mockTracer := mocktracer.New() 29 30 path := "/test-resource" 31 req := webutil.NewMockRequest("GET", path) 32 resource := "/:id" 33 startTime := time.Now().Add(-10 * time.Second) 34 span, _ := httptrace.StartHTTPSpan( 35 context.TODO(), 36 mockTracer, 37 req, 38 resource, 39 startTime, 40 opentracing.Tag{Key: "http.route", Value: resource}, 41 ) 42 43 mockSpan := span.(*mocktracer.MockSpan) 44 assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName) 45 46 expectedTags := map[string]interface{}{ 47 tracing.TagKeyMeasured: 1, 48 tracing.TagKeyResourceName: fmt.Sprintf("GET %s", resource), 49 tracing.TagKeySpanType: tracing.SpanTypeWeb, 50 tracing.TagKeyHTTPMethod: "GET", 51 tracing.TagKeyHTTPURL: path, 52 "http.remote_addr": "127.0.0.1", 53 "http.host": "localhost", 54 "http.user_agent": "go-sdk test", 55 "http.route": resource, 56 } 57 assert.Equal(expectedTags, mockSpan.Tags()) 58 assert.Equal(startTime, mockSpan.StartTime) 59 assert.True(mockSpan.FinishTime.IsZero()) 60 } 61 62 func TestStart(t *testing.T) { 63 assert := assert.New(t) 64 mockTracer := mocktracer.New() 65 httpTracer := httptrace.Tracer(mockTracer) 66 67 path := "/test-resource" 68 req := webutil.NewMockRequest("GET", path) 69 _, req = httpTracer.Start(req) 70 71 span := opentracing.SpanFromContext(req.Context()) 72 mockSpan := span.(*mocktracer.MockSpan) 73 assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName) 74 75 expectedTags := map[string]interface{}{ 76 tracing.TagKeyMeasured: 1, 77 tracing.TagKeyResourceName: fmt.Sprintf("GET %s", path), 78 tracing.TagKeySpanType: tracing.SpanTypeWeb, 79 tracing.TagKeyHTTPMethod: "GET", 80 tracing.TagKeyHTTPURL: path, 81 "http.remote_addr": "127.0.0.1", 82 "http.host": "localhost", 83 "http.user_agent": "go-sdk test", 84 } 85 assert.Equal(expectedTags, mockSpan.Tags()) 86 assert.True(mockSpan.FinishTime.IsZero()) 87 } 88 89 func applyIncomingSpan(req *http.Request, t opentracing.Tracer, s opentracing.Span) { 90 _ = t.Inject( 91 s.Context(), 92 opentracing.HTTPHeaders, 93 opentracing.HTTPHeadersCarrier(req.Header), 94 ) 95 } 96 97 func TestStartWithParentSpan(t *testing.T) { 98 assert := assert.New(t) 99 mockTracer := mocktracer.New() 100 httpTracer := httptrace.Tracer(mockTracer) 101 102 parentSpan := mockTracer.StartSpan("test_op") 103 path := "/test-resource" 104 req := webutil.NewMockRequest("GET", path) 105 applyIncomingSpan(req, mockTracer, parentSpan) 106 _, req = httpTracer.Start(req) 107 108 span := opentracing.SpanFromContext(req.Context()) 109 mockSpan := span.(*mocktracer.MockSpan) 110 assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName) 111 112 mockParentSpan := parentSpan.(*mocktracer.MockSpan) 113 assert.Equal(mockSpan.ParentID, mockParentSpan.SpanContext.SpanID) 114 } 115 116 func TestFinish(t *testing.T) { 117 assert := assert.New(t) 118 mockTracer := mocktracer.New() 119 httpTracer := httptrace.Tracer(mockTracer) 120 121 path := "/test-resource" 122 req := webutil.NewMockRequest("GET", path) 123 tf, req := httpTracer.Start(req) 124 125 tf.Finish(http.StatusOK, nil) 126 127 span := opentracing.SpanFromContext(req.Context()) 128 mockSpan := span.(*mocktracer.MockSpan) 129 assert.False(mockSpan.FinishTime.IsZero()) 130 } 131 132 func TestFinishError(t *testing.T) { 133 assert := assert.New(t) 134 mockTracer := mocktracer.New() 135 httpTracer := httptrace.Tracer(mockTracer) 136 137 path := "/test-resource" 138 req := webutil.NewMockRequest("GET", path) 139 tf, req := httpTracer.Start(req) 140 141 tf.Finish(http.StatusOK, fmt.Errorf("error")) 142 143 span := opentracing.SpanFromContext(req.Context()) 144 mockSpan := span.(*mocktracer.MockSpan) 145 assert.Equal("error", mockSpan.Tags()[tracing.TagKeyError]) 146 assert.Equal("200", mockSpan.Tags()[tracing.TagKeyHTTPCode]) 147 assert.False(mockSpan.FinishTime.IsZero()) 148 }