gitlab.com/gitlab-org/labkit@v1.21.0/tracing/impl/lightstep_tracer_test.go (about) 1 // +build tracer_static,tracer_static_lightstep 2 3 package impl 4 5 import ( 6 "fmt" 7 "github.com/lightstep/lightstep-tracer-go" 8 "github.com/opentracing/opentracing-go" 9 "github.com/stretchr/testify/require" 10 "testing" 11 12 "gitlab.com/gitlab-org/labkit/tracing/connstr" 13 ) 14 15 func Test_lightstepTracerFactory(t *testing.T) { 16 tests := []struct { 17 connectionString string 18 wantErr bool 19 strict bool 20 }{ 21 { 22 connectionString: "opentracing://lightstep", 23 wantErr: true, 24 strict: true, 25 }, 26 { 27 connectionString: "opentracing://lightstep?access_token=12345", 28 wantErr: false, 29 strict: true, 30 }, 31 { 32 connectionString: "opentracing://lightstep?access_token=12345&relaxed", 33 wantErr: false, 34 strict: false, 35 }, 36 { 37 connectionString: "opentracing://lightstep?access_token=12345&strict", 38 wantErr: true, 39 strict: true, 40 }, 41 } 42 for _, tt := range tests { 43 t.Run(tt.connectionString, func(t *testing.T) { 44 _, options, err := connstr.Parse(tt.connectionString) 45 if err != nil { 46 t.Errorf("TracerFactory() error = unable to parse connection string: %v", err) 47 } 48 if tt.strict { 49 options[keyStrictConnectionParsing] = "1" 50 } 51 52 options["service_name"] = "test" 53 54 gotTracer, gotCloser, err := lightstepTracerFactory(options) 55 56 if (err != nil) != tt.wantErr { 57 t.Errorf("TracerFactory() error = %v, wantErr %v", err, tt.wantErr) 58 return 59 } 60 61 if !tt.wantErr { 62 if gotTracer == nil { 63 t.Errorf("TracerFactory() expected a tracer, got nil") 64 } 65 if gotCloser == nil { 66 t.Errorf("TracerFactory() expected a closed, got nil") 67 } 68 } 69 }) 70 } 71 } 72 73 func TestIsSampled_lightstep(t *testing.T) { 74 t.Parallel() 75 76 for _, tc := range []struct { 77 desc string 78 connection string 79 sampled bool 80 }{ 81 { 82 desc: "lightstep sampled", 83 connection: "opentracing://lightstep?access_token=12345&relaxed", 84 sampled: true, 85 }, 86 { 87 desc: "lightstep not sampled", 88 connection: "opentracing://lightstep?access_token=12345&relaxed", 89 sampled: false, 90 }, 91 } { 92 t.Run(tc.desc, func(t *testing.T) { 93 _, options, err := connstr.Parse(tc.connection) 94 require.NoError(t, err) 95 options["service_name"] = "test" 96 97 lightstepTracer, closer, err := lightstepTracerFactory(options) 98 require.NoError(t, err) 99 100 var opt opentracing.StartSpanOption 101 if tc.sampled { 102 opt = lightstep.SetSampled("true") 103 } else { 104 opt = lightstep.SetSampled("false") 105 } 106 107 span := lightstepTracer.StartSpan("rootSpan", lightstep.SetTraceID(1), opt) 108 for i := 0; i < 10; i++ { 109 require.Equal(t, tc.sampled, IsSampled(span)) 110 span = lightstepTracer.StartSpan(fmt.Sprintf("span%d", i), opentracing.ChildOf(span.Context())) 111 } 112 require.NoError(t, closer.Close()) 113 }) 114 } 115 }