github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/opentracing/tracing_test.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package opentracing 22 23 import ( 24 "io" 25 "testing" 26 27 "github.com/lightstep/lightstep-tracer-go" 28 "github.com/opentracing/opentracing-go" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 "github.com/uber-go/tally" 32 "github.com/uber/jaeger-client-go" 33 jaegercfg "github.com/uber/jaeger-client-go/config" 34 "go.uber.org/zap" 35 ) 36 37 func TestTracingConfiguration_NewTracer(t *testing.T) { 38 serviceName := "foo" 39 doCall := func(cfg *TracingConfiguration) (opentracing.Tracer, io.Closer, error) { 40 return cfg.NewTracer(serviceName, tally.NoopScope, zap.L()) 41 } 42 43 t.Run("defaults to noop", func(t *testing.T) { 44 cfg := TracingConfiguration{} 45 tr, closer, err := doCall(&cfg) 46 defer closer.Close() 47 require.NoError(t, err) 48 assert.Equal(t, opentracing.NoopTracer{}, tr) 49 assert.Equal(t, noopCloser{}, closer) 50 }) 51 52 t.Run("errors on non-jaeger", func(t *testing.T) { 53 cfg := TracingConfiguration{ 54 Backend: "someone_else", 55 } 56 _, _, err := doCall(&cfg) 57 require.Contains(t, err.Error(), "unknown tracing backend: someone_else") 58 }) 59 60 t.Run("initializes LightStep", func(t *testing.T) { 61 cfg := TracingConfiguration{ 62 Backend: "lightstep", 63 Lightstep: lightstep.Options{ 64 AccessToken: "foo", 65 }, 66 } 67 68 tr, closer, err := doCall(&cfg) 69 defer closer.Close() 70 71 assert.NoError(t, err) 72 assert.NotNil(t, tr) 73 }) 74 75 t.Run("initializes jaeger tracer", func(t *testing.T) { 76 cfg := TracingConfiguration{ 77 Backend: TracingBackendJaeger, 78 } 79 tr, closer, err := doCall(&cfg) 80 defer closer.Close() 81 82 require.NoError(t, err) 83 assert.IsType(t, (*jaeger.Tracer)(nil), tr) 84 }) 85 86 t.Run("sets service name on empty", func(t *testing.T) { 87 cfg := TracingConfiguration{ 88 Backend: TracingBackendJaeger, 89 } 90 _, closer, err := doCall(&cfg) 91 defer closer.Close() 92 93 require.NoError(t, err) 94 assert.Equal(t, serviceName, cfg.Jaeger.ServiceName) 95 }) 96 97 t.Run("leaves service name on non-empty", func(t *testing.T) { 98 cfg := TracingConfiguration{ 99 Backend: TracingBackendJaeger, 100 Jaeger: jaegercfg.Configuration{ 101 ServiceName: "other", 102 }, 103 } 104 _, closer, err := doCall(&cfg) 105 defer closer.Close() 106 107 require.NoError(t, err) 108 assert.Equal(t, "other", cfg.Jaeger.ServiceName) 109 }) 110 }