gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/pkg/katautils/tracing.go (about) 1 // Copyright (c) 2018 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package katautils 7 8 import ( 9 "context" 10 "io" 11 12 opentracing "github.com/opentracing/opentracing-go" 13 "github.com/uber/jaeger-client-go/config" 14 ) 15 16 // Implements jaeger-client-go.Logger interface 17 type traceLogger struct { 18 } 19 20 // tracerCloser contains a copy of the closer returned by createTracer() which 21 // is used by stopTracing(). 22 var tracerCloser io.Closer 23 24 func (t traceLogger) Error(msg string) { 25 kataUtilsLogger.Error(msg) 26 } 27 28 func (t traceLogger) Infof(msg string, args ...interface{}) { 29 kataUtilsLogger.Infof(msg, args...) 30 } 31 32 // CreateTracer create a tracer 33 func CreateTracer(name string) (opentracing.Tracer, error) { 34 cfg := &config.Configuration{ 35 ServiceName: name, 36 37 // If tracing is disabled, use a NOP trace implementation 38 Disabled: !tracing, 39 40 // Note that span logging reporter option cannot be enabled as 41 // it pollutes the output stream which causes (atleast) the 42 // "state" command to fail under Docker. 43 Sampler: &config.SamplerConfig{ 44 Type: "const", 45 Param: 1, 46 }, 47 48 // Ensure that Jaeger logs each span. 49 // This is essential as it is used by: 50 // 51 // https: //github.com/kata-containers/tests/blob/master/tracing/tracing-test.sh 52 Reporter: &config.ReporterConfig{ 53 LogSpans: tracing, 54 }, 55 } 56 57 logger := traceLogger{} 58 59 tracer, closer, err := cfg.NewTracer(config.Logger(logger)) 60 if err != nil { 61 return nil, err 62 } 63 64 // save for stopTracing()'s exclusive use 65 tracerCloser = closer 66 67 // Seems to be essential to ensure non-root spans are logged 68 opentracing.SetGlobalTracer(tracer) 69 70 return tracer, nil 71 } 72 73 // StopTracing ends all tracing, reporting the spans to the collector. 74 func StopTracing(ctx context.Context) { 75 if !tracing { 76 return 77 } 78 79 span := opentracing.SpanFromContext(ctx) 80 81 if span != nil { 82 span.Finish() 83 } 84 85 // report all possible spans to the collector 86 if tracerCloser != nil { 87 tracerCloser.Close() 88 } 89 } 90 91 // Trace creates a new tracing span based on the specified name and parent 92 // context. 93 func Trace(parent context.Context, name string) (opentracing.Span, context.Context) { 94 span, ctx := opentracing.StartSpanFromContext(parent, name) 95 96 span.SetTag("source", "runtime") 97 span.SetTag("component", "cli") 98 99 // This is slightly confusing: when tracing is disabled, trace spans 100 // are still created - but the tracer used is a NOP. Therefore, only 101 // display the message when tracing is really enabled. 102 if tracing { 103 // This log message is *essential*: it is used by: 104 // https: //github.com/kata-containers/tests/blob/master/tracing/tracing-test.sh 105 kataUtilsLogger.Debugf("created span %v", span) 106 } 107 108 return span, ctx 109 }