go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama@v0.43.0/option.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 // import "go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama"
    16  
    17  import (
    18  	"go.opentelemetry.io/otel"
    19  	"go.opentelemetry.io/otel/propagation"
    20  	"go.opentelemetry.io/otel/trace"
    21  )
    22  
    23  const defaultTracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama"
    24  
    25  type config struct {
    26  	TracerProvider trace.TracerProvider
    27  	Propagators    propagation.TextMapPropagator
    28  
    29  	Tracer trace.Tracer
    30  }
    31  
    32  // newConfig returns a config with all Options set.
    33  func newConfig(opts ...Option) config {
    34  	cfg := config{
    35  		Propagators:    otel.GetTextMapPropagator(),
    36  		TracerProvider: otel.GetTracerProvider(),
    37  	}
    38  	for _, opt := range opts {
    39  		opt.apply(&cfg)
    40  	}
    41  
    42  	cfg.Tracer = cfg.TracerProvider.Tracer(
    43  		defaultTracerName,
    44  		trace.WithInstrumentationVersion(Version()),
    45  	)
    46  
    47  	return cfg
    48  }
    49  
    50  // Option interface used for setting optional config properties.
    51  type Option interface {
    52  	apply(*config)
    53  }
    54  
    55  type optionFunc func(*config)
    56  
    57  func (fn optionFunc) apply(c *config) {
    58  	fn(c)
    59  }
    60  
    61  // WithTracerProvider specifies a tracer provider to use for creating a tracer.
    62  // If none is specified, the global provider is used.
    63  func WithTracerProvider(provider trace.TracerProvider) Option {
    64  	return optionFunc(func(cfg *config) {
    65  		if provider != nil {
    66  			cfg.TracerProvider = provider
    67  		}
    68  	})
    69  }
    70  
    71  // WithPropagators specifies propagators to use for extracting
    72  // information from the HTTP requests. If none are specified, global
    73  // ones will be used.
    74  func WithPropagators(propagators propagation.TextMapPropagator) Option {
    75  	return optionFunc(func(cfg *config) {
    76  		if propagators != nil {
    77  			cfg.Propagators = propagators
    78  		}
    79  	})
    80  }