github.com/codingfuture/orig-energi3@v0.8.4/swarm/tracing/tracing.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package tracing
    19  
    20  import (
    21  	"io"
    22  	"os"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/log"
    27  	jaeger "github.com/uber/jaeger-client-go"
    28  	jaegercfg "github.com/uber/jaeger-client-go/config"
    29  	cli "gopkg.in/urfave/cli.v1"
    30  )
    31  
    32  var Enabled bool = false
    33  
    34  // TracingEnabledFlag is the CLI flag name to use to enable trace collections.
    35  const TracingEnabledFlag = "tracing"
    36  
    37  var (
    38  	Closer io.Closer
    39  )
    40  
    41  var (
    42  	TracingFlag = cli.BoolFlag{
    43  		Name:  TracingEnabledFlag,
    44  		Usage: "Enable tracing",
    45  	}
    46  	TracingEndpointFlag = cli.StringFlag{
    47  		Name:  "tracing.endpoint",
    48  		Usage: "Tracing endpoint",
    49  		Value: "0.0.0.0:6831",
    50  	}
    51  	TracingSvcFlag = cli.StringFlag{
    52  		Name:  "tracing.svc",
    53  		Usage: "Tracing service name",
    54  		Value: "swarm",
    55  	}
    56  )
    57  
    58  // Flags holds all command-line flags required for tracing collection.
    59  var Flags = []cli.Flag{
    60  	TracingFlag,
    61  	TracingEndpointFlag,
    62  	TracingSvcFlag,
    63  }
    64  
    65  // Init enables or disables the open tracing system.
    66  func init() {
    67  	for _, arg := range os.Args {
    68  		if flag := strings.TrimLeft(arg, "-"); flag == TracingEnabledFlag {
    69  			Enabled = true
    70  		}
    71  	}
    72  }
    73  
    74  func Setup(ctx *cli.Context) {
    75  	if Enabled {
    76  		log.Info("Enabling opentracing")
    77  		var (
    78  			endpoint = ctx.GlobalString(TracingEndpointFlag.Name)
    79  			svc      = ctx.GlobalString(TracingSvcFlag.Name)
    80  		)
    81  
    82  		Closer = initTracer(endpoint, svc)
    83  	}
    84  }
    85  
    86  func initTracer(endpoint, svc string) (closer io.Closer) {
    87  	// Sample configuration for testing. Use constant sampling to sample every trace
    88  	// and enable LogSpan to log every span via configured Logger.
    89  	cfg := jaegercfg.Configuration{
    90  		Sampler: &jaegercfg.SamplerConfig{
    91  			Type:  jaeger.SamplerTypeConst,
    92  			Param: 1,
    93  		},
    94  		Reporter: &jaegercfg.ReporterConfig{
    95  			LogSpans:            true,
    96  			BufferFlushInterval: 1 * time.Second,
    97  			LocalAgentHostPort:  endpoint,
    98  		},
    99  	}
   100  
   101  	// Example logger and metrics factory. Use github.com/uber/jaeger-client-go/log
   102  	// and github.com/uber/jaeger-lib/metrics respectively to bind to real logging and metrics
   103  	// frameworks.
   104  	//jLogger := jaegerlog.StdLogger
   105  	//jMetricsFactory := metrics.NullFactory
   106  
   107  	// Initialize tracer with a logger and a metrics factory
   108  	closer, err := cfg.InitGlobalTracer(
   109  		svc,
   110  		//jaegercfg.Logger(jLogger),
   111  		//jaegercfg.Metrics(jMetricsFactory),
   112  		//jaegercfg.Observer(rpcmetrics.NewObserver(jMetricsFactory, rpcmetrics.DefaultNameNormalizer)),
   113  	)
   114  	if err != nil {
   115  		log.Error("Could not initialize Jaeger tracer", "err", err)
   116  	}
   117  
   118  	return closer
   119  }