@@ -16,7 +16,10 @@ import (
1616 "sync"
1717 "time"
1818
19+ "github.com/opentracing/opentracing-go"
20+ otLog "github.com/opentracing/opentracing-go/log"
1921 log "github.com/sirupsen/logrus"
22+ "github.com/uber/jaeger-client-go/config"
2023 "golang.org/x/crypto/ssh/terminal"
2124 "gopkg.in/natefinch/lumberjack.v2"
2225)
@@ -41,6 +44,12 @@ type LogParams struct {
4144 Format string
4245}
4346
47+ type Logr struct {
48+ ctx context.Context
49+ logEntry * log.Entry
50+ cl io.Closer
51+ }
52+
4453var (
4554 logParams LogParams
4655 initMutex sync.Mutex
@@ -163,8 +172,28 @@ func updateLogParamsFromEnv() {
163172 }
164173}
165174
175+ //Initalizes opentracing tracing
176+ func InitOpentracing (service string ) (opentracing.Tracer , io.Closer ) {
177+ cfg := & config.Configuration {
178+ ServiceName : service ,
179+ Sampler : & config.SamplerConfig {
180+ Type : "const" ,
181+ Param : 1 ,
182+ },
183+ Reporter : & config.ReporterConfig {
184+ LogSpans : true ,
185+ },
186+ }
187+ //add tracer as a input of NewTracer so that the logspans declared true above will work
188+ tracer , closer , err := cfg .NewTracer ()
189+ if err != nil {
190+ panic (fmt .Sprintf ("ERROR: cannot init tracing: %v\n " , err ))
191+ }
192+ return tracer , closer
193+ }
194+
166195// Initialize logging with given params
167- func InitLogging (logName string , params * LogParams , alsoLogToStderr bool ) (err error ) {
196+ func InitLogging (logName string , params * LogParams , alsoLogToStderr bool , initTracing bool ) (err error , l * Logr ) {
168197 initMutex .Lock ()
169198 defer initMutex .Unlock ()
170199
@@ -190,23 +219,27 @@ func InitLogging(logName string, params *LogParams, alsoLogToStderr bool) (err e
190219 // No output except for the hooks
191220 log .SetOutput (ioutil .Discard )
192221
222+ //Default Logr
223+ logEntry := sourced ()
224+ lg := Logr {nil , logEntry , nil }
225+
193226 if logParams .GetFile () != "" {
194227 err = AddFileHook ()
195228 if err != nil {
196- return err
229+ return err , & lg
197230 }
198231 }
199232 if alsoLogToStderr {
200233 err = AddConsoleHook ()
201234 if err != nil {
202- return err
235+ return err , & lg
203236 }
204237 }
205238
206239 // Set log level
207240 level , err := log .ParseLevel (logParams .GetLevel ())
208241 if err != nil {
209- return err
242+ return err , & lg
210243 }
211244 log .SetLevel (level )
212245
@@ -217,7 +250,64 @@ func InitLogging(logName string, params *LogParams, alsoLogToStderr bool) (err e
217250 "alsoLogToStderr" : alsoLogToStderr ,
218251 }).Info ("Initialized logging." )
219252
220- return nil
253+ //initializes tracing capabilites if true
254+ if initTracing {
255+ //Initializing the tracer
256+ tracer , closer := InitOpentracing ("CSI-Driver" )
257+ opentracing .SetGlobalTracer (tracer )
258+
259+ //Span Initialized with default context
260+ span := tracer .StartSpan ("CSI-Driver" )
261+ log .Printf ("Span Context --- Traceid:Spanid:ParentSpanid:Flags : %v" , span .Context ())
262+ ctx := opentracing .ContextWithSpan (context .Background (), span )
263+ logEntry := sourced ()
264+ l := Logr {ctx , logEntry , closer }
265+
266+ l .LogToTrace ("Info" , "Tracing Initialized" )
267+ defer span .Finish ()
268+
269+ return nil , & l
270+ }
271+
272+ return nil , & lg
273+ }
274+
275+ func (l * Logr ) CloseTracer () {
276+ l .cl .Close ()
277+ }
278+
279+ //Logs given string to tracer
280+ func (l * Logr ) LogToTrace (level , msg string ) {
281+ span := opentracing .SpanFromContext (l .ctx )
282+ //fmt.Print("In LogToTrace")
283+ if span != nil {
284+ span .LogFields (otLog .String ("event" , msg ))
285+ }
286+ if span == nil {
287+ fmt .Print ("Span is nil" )
288+ }
289+ span .Finish ()
290+ }
291+
292+ //Sets context of called Logr to given context
293+ func (l * Logr ) SetContext (context context.Context ) {
294+ l .ctx = context
295+ }
296+
297+ //Starts and returns a span for the inputted Logr
298+ func (l * Logr ) StartContext (spanName string ) (s opentracing.Span ) {
299+ s = opentracing .SpanFromContext (l .ctx )
300+ if s == nil || s .BaggageItem (spanName ) == "" {
301+ s = opentracing .StartSpan (spanName )
302+ s .SetBaggageItem (spanName , "true" )
303+ l .ctx = opentracing .ContextWithSpan (context .Background (), s )
304+ }
305+ return s
306+ }
307+
308+ //Ends the inputted span
309+ func EndContext (span opentracing.Span ) {
310+ span .Finish ()
221311}
222312
223313func AddConsoleHook () error {
@@ -455,7 +545,6 @@ func IsSensitive(key string) bool {
455545 "token" ,
456546 "accesskey" ,
457547 "passphrase" ,
458-
459548 }
460549 key = strings .ToLower (key )
461550 for _ , bad := range badWords {
@@ -506,24 +595,42 @@ func sourced() *log.Entry {
506595 return log .WithField ("file" , fmt .Sprintf ("%s:%d" , file , line ))
507596}
508597
509- // Trace logs a message at level Trace on the standard logger.
510598func Trace (args ... interface {}) {
511599 sourced ().Trace (args ... )
512600}
513601
514- // Debug logs a message at level Debug on the standard logger.
602+ // Trace logs a message at level Trace on the standard logger.
603+ func (lg * Logr ) Trace (args ... interface {}) {
604+ lg .logEntry .Trace (args ... )
605+ str := fmt .Sprintf ("%v" , args )
606+ lg .LogToTrace ("Trace" , str )
607+ }
608+
515609func Debug (args ... interface {}) {
516- sourced ().Debug (args ... )
610+ sourced ().Trace (args ... )
611+ }
612+
613+ // Debug logs a message at level Debug on the standard logger.
614+ func (lg * Logr ) Debug (args ... interface {}) {
615+ lg .logEntry .Debug (args ... )
616+ str := fmt .Sprintf ("%v" , args )
617+ lg .LogToTrace ("Debug" , str )
517618}
518619
519620// Print logs a message at level Info on the standard logger.
520621func Print (args ... interface {}) {
521622 sourced ().Print (args ... )
522623}
523624
524- // Info logs a message at level Info on the standard logger.
525625func Info (args ... interface {}) {
526- sourced ().Info (args ... )
626+ sourced ().Trace (args ... )
627+ }
628+
629+ // Info logs a message at level Info on the standard logger.
630+ func (lg * Logr ) Info (args ... interface {}) {
631+ lg .logEntry .Info (args ... )
632+ str := fmt .Sprintf ("%v" , args )
633+ lg .LogToTrace ("Info" , str )
527634}
528635
529636// Warn logs a message at level Warn on the standard logger.
0 commit comments