github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/tracing/tracing.go (about) 1 package tracing 2 3 import ( 4 "io" 5 "os" 6 "strings" 7 "time" 8 9 "github.com/ShyftNetwork/go-empyrean/log" 10 jaeger "github.com/uber/jaeger-client-go" 11 jaegercfg "github.com/uber/jaeger-client-go/config" 12 cli "gopkg.in/urfave/cli.v1" 13 ) 14 15 var Enabled bool = false 16 17 // TracingEnabledFlag is the CLI flag name to use to enable trace collections. 18 const TracingEnabledFlag = "tracing" 19 20 var ( 21 Closer io.Closer 22 ) 23 24 var ( 25 TracingFlag = cli.BoolFlag{ 26 Name: TracingEnabledFlag, 27 Usage: "Enable tracing", 28 } 29 TracingEndpointFlag = cli.StringFlag{ 30 Name: "tracing.endpoint", 31 Usage: "Tracing endpoint", 32 Value: "0.0.0.0:6831", 33 } 34 TracingSvcFlag = cli.StringFlag{ 35 Name: "tracing.svc", 36 Usage: "Tracing service name", 37 Value: "swarm", 38 } 39 ) 40 41 // Flags holds all command-line flags required for tracing collection. 42 var Flags = []cli.Flag{ 43 TracingFlag, 44 TracingEndpointFlag, 45 TracingSvcFlag, 46 } 47 48 // Init enables or disables the open tracing system. 49 func init() { 50 for _, arg := range os.Args { 51 if flag := strings.TrimLeft(arg, "-"); flag == TracingEnabledFlag { 52 Enabled = true 53 } 54 } 55 } 56 57 func Setup(ctx *cli.Context) { 58 if Enabled { 59 log.Info("Enabling opentracing") 60 var ( 61 endpoint = ctx.GlobalString(TracingEndpointFlag.Name) 62 svc = ctx.GlobalString(TracingSvcFlag.Name) 63 ) 64 65 Closer = initTracer(endpoint, svc) 66 } 67 } 68 69 func initTracer(endpoint, svc string) (closer io.Closer) { 70 // Sample configuration for testing. Use constant sampling to sample every trace 71 // and enable LogSpan to log every span via configured Logger. 72 cfg := jaegercfg.Configuration{ 73 Sampler: &jaegercfg.SamplerConfig{ 74 Type: jaeger.SamplerTypeConst, 75 Param: 1, 76 }, 77 Reporter: &jaegercfg.ReporterConfig{ 78 LogSpans: true, 79 BufferFlushInterval: 1 * time.Second, 80 LocalAgentHostPort: endpoint, 81 }, 82 } 83 84 // Example logger and metrics factory. Use github.com/uber/jaeger-client-go/log 85 // and github.com/uber/jaeger-lib/metrics respectively to bind to real logging and metrics 86 // frameworks. 87 //jLogger := jaegerlog.StdLogger 88 //jMetricsFactory := metrics.NullFactory 89 90 // Initialize tracer with a logger and a metrics factory 91 closer, err := cfg.InitGlobalTracer( 92 svc, 93 //jaegercfg.Logger(jLogger), 94 //jaegercfg.Metrics(jMetricsFactory), 95 //jaegercfg.Observer(rpcmetrics.NewObserver(jMetricsFactory, rpcmetrics.DefaultNameNormalizer)), 96 ) 97 if err != nil { 98 log.Error("Could not initialize Jaeger tracer", "err", err) 99 } 100 101 return closer 102 }