go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama@v0.43.0/option_test.go (about) 1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package otelsarama 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 23 "go.opentelemetry.io/otel" 24 "go.opentelemetry.io/otel/propagation" 25 "go.opentelemetry.io/otel/trace" 26 ) 27 28 // We need a fake tracer provider to ensure the one passed in options is the one used afterwards. 29 // In order to avoid adding the SDK as a dependency, we use this mock. 30 type fakeTracerProvider struct{} 31 32 func (fakeTracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer { 33 return fakeTracer{ 34 name: name, 35 } 36 } 37 38 type fakeTracer struct { 39 name string 40 } 41 42 func (fakeTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { 43 return ctx, nil 44 } 45 46 func TestNewConfig(t *testing.T) { 47 tp := fakeTracerProvider{} 48 prop := propagation.NewCompositeTextMapPropagator() 49 50 testCases := []struct { 51 name string 52 opts []Option 53 expected config 54 }{ 55 { 56 name: "with provider", 57 opts: []Option{ 58 WithTracerProvider(tp), 59 }, 60 expected: config{ 61 TracerProvider: tp, 62 Tracer: tp.Tracer(defaultTracerName, trace.WithInstrumentationVersion(Version())), 63 Propagators: otel.GetTextMapPropagator(), 64 }, 65 }, 66 { 67 name: "with empty provider", 68 opts: []Option{ 69 WithTracerProvider(nil), 70 }, 71 expected: config{ 72 TracerProvider: otel.GetTracerProvider(), 73 Tracer: otel.GetTracerProvider().Tracer(defaultTracerName, trace.WithInstrumentationVersion(Version())), 74 Propagators: otel.GetTextMapPropagator(), 75 }, 76 }, 77 { 78 name: "with propagators", 79 opts: []Option{ 80 WithPropagators(prop), 81 }, 82 expected: config{ 83 TracerProvider: otel.GetTracerProvider(), 84 Tracer: otel.GetTracerProvider().Tracer(defaultTracerName, trace.WithInstrumentationVersion(Version())), 85 Propagators: prop, 86 }, 87 }, 88 { 89 name: "with empty propagators", 90 opts: []Option{ 91 WithPropagators(nil), 92 }, 93 expected: config{ 94 TracerProvider: otel.GetTracerProvider(), 95 Tracer: otel.GetTracerProvider().Tracer(defaultTracerName, trace.WithInstrumentationVersion(Version())), 96 Propagators: otel.GetTextMapPropagator(), 97 }, 98 }, 99 } 100 101 for _, tc := range testCases { 102 t.Run(tc.name, func(t *testing.T) { 103 result := newConfig(tc.opts...) 104 assert.Equal(t, tc.expected, result) 105 }) 106 } 107 }