gitlab.com/gitlab-org/labkit@v1.21.0/tracing/initialization.go (about)

     1  package tracing
     2  
     3  import (
     4  	"io"
     5  
     6  	opentracing "github.com/opentracing/opentracing-go"
     7  	log "github.com/sirupsen/logrus"
     8  	"gitlab.com/gitlab-org/labkit/tracing/connstr"
     9  	"gitlab.com/gitlab-org/labkit/tracing/impl"
    10  )
    11  
    12  type nopCloser struct {
    13  }
    14  
    15  func (nopCloser) Close() error { return nil }
    16  
    17  // Initialize will initialize distributed tracing.
    18  func Initialize(opts ...InitializationOption) io.Closer {
    19  	config := applyInitializationOptions(opts)
    20  
    21  	if config.connectionString == "" {
    22  		// No opentracing connection has been set
    23  		return &nopCloser{}
    24  	}
    25  
    26  	driverName, options, err := connstr.Parse(config.connectionString)
    27  	if err != nil {
    28  		log.WithError(err).Infoln("unable to parse connection")
    29  		return &nopCloser{}
    30  	}
    31  
    32  	// URL-provided service_name overrides the InitializationOption
    33  	if _, ok := options["service_name"]; !ok {
    34  		options["service_name"] = config.serviceName
    35  	}
    36  
    37  	tracer, closer, err := impl.New(driverName, options)
    38  	if err != nil {
    39  		log.WithError(err).Warn("skipping tracing configuration step")
    40  		return &nopCloser{}
    41  	}
    42  
    43  	if tracer == nil {
    44  		log.Warn("no tracer provided, tracing will be disabled")
    45  	} else {
    46  		log.Info("Tracing enabled")
    47  		opentracing.SetGlobalTracer(tracer)
    48  	}
    49  
    50  	if closer == nil {
    51  		return &nopCloser{}
    52  	}
    53  	return closer
    54  }