github.com/prysmaticlabs/prysm@v1.4.4/shared/tracing/tracer.go (about)

     1  // Package tracing sets up jaeger as an opentracing tool
     2  // for services in Prysm.
     3  package tracing
     4  
     5  import (
     6  	"errors"
     7  
     8  	"contrib.go.opencensus.io/exporter/jaeger"
     9  	"github.com/prysmaticlabs/prysm/shared/version"
    10  	"github.com/sirupsen/logrus"
    11  	"go.opencensus.io/trace"
    12  )
    13  
    14  var log = logrus.WithField("prefix", "tracing")
    15  
    16  // Setup creates and initializes a new tracing configuration..
    17  func Setup(serviceName, processName, endpoint string, sampleFraction float64, enable bool) error {
    18  	if !enable {
    19  		trace.ApplyConfig(trace.Config{DefaultSampler: trace.NeverSample()})
    20  		return nil
    21  	}
    22  
    23  	if serviceName == "" {
    24  		return errors.New("tracing service name cannot be empty")
    25  	}
    26  
    27  	trace.ApplyConfig(trace.Config{
    28  		DefaultSampler:          trace.ProbabilitySampler(sampleFraction),
    29  		MaxMessageEventsPerSpan: 500,
    30  	})
    31  
    32  	log.Infof("Starting Jaeger exporter endpoint at address = %s", endpoint)
    33  	exporter, err := jaeger.NewExporter(jaeger.Options{
    34  		CollectorEndpoint: endpoint,
    35  		Process: jaeger.Process{
    36  			ServiceName: serviceName,
    37  			Tags: []jaeger.Tag{
    38  				jaeger.StringTag("process_name", processName),
    39  				jaeger.StringTag("version", version.Version()),
    40  			},
    41  		},
    42  		BufferMaxCount: 10000,
    43  		OnError: func(err error) {
    44  			log.WithError(err).Error("Failed to process span")
    45  		},
    46  	})
    47  	if err != nil {
    48  		return err
    49  	}
    50  	trace.RegisterExporter(exporter)
    51  
    52  	return nil
    53  }