go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/tracer.go (about)

     1  package instrumentation
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  	"sync"
    10  
    11  	"github.com/opentracing/opentracing-go"
    12  )
    13  
    14  var (
    15  	tracer       opentracing.Tracer = opentracing.NoopTracer{}
    16  	logger                          = log.New(ioutil.Discard, "", 0)
    17  	sourceRoot                      = ""
    18  	remoteConfig                    = map[string]interface{}{}
    19  
    20  	m sync.RWMutex
    21  )
    22  
    23  func SetTracer(t opentracing.Tracer) {
    24  	m.Lock()
    25  	defer m.Unlock()
    26  
    27  	tracer = t
    28  }
    29  
    30  func Tracer() opentracing.Tracer {
    31  	m.RLock()
    32  	defer m.RUnlock()
    33  
    34  	return tracer
    35  }
    36  
    37  func SetLogger(l *log.Logger) {
    38  	m.Lock()
    39  	defer m.Unlock()
    40  
    41  	logger = l
    42  }
    43  
    44  func Logger() *log.Logger {
    45  	m.RLock()
    46  	defer m.RUnlock()
    47  
    48  	return logger
    49  }
    50  
    51  func SetSourceRoot(root string) {
    52  	m.Lock()
    53  	defer m.Unlock()
    54  	// In windows the debug symbols and source root can be in different cases
    55  	if runtime.GOOS == "windows" {
    56  		root = strings.ToLower(root)
    57  	}
    58  	sourceRoot = root
    59  }
    60  
    61  func GetSourceRoot() string {
    62  	m.RLock()
    63  	defer m.RUnlock()
    64  
    65  	return sourceRoot
    66  }
    67  
    68  func SetRemoteConfiguration(config map[string]interface{}) {
    69  	m.Lock()
    70  	defer m.Unlock()
    71  
    72  	remoteConfig = config
    73  }
    74  
    75  func GetRemoteConfiguration() map[string]interface{} {
    76  	m.RLock()
    77  	defer m.RUnlock()
    78  
    79  	return remoteConfig
    80  }
    81  
    82  //go:noinline
    83  func GetCallerInsideSourceRoot(skip int) (pc uintptr, file string, line int, ok bool) {
    84  	isWindows := runtime.GOOS == "windows"
    85  	pcs := make([]uintptr, 64)
    86  	count := runtime.Callers(skip+2, pcs)
    87  	pcs = pcs[:count]
    88  	frames := runtime.CallersFrames(pcs)
    89  	for {
    90  		frame, more := frames.Next()
    91  		file := filepath.Clean(frame.File)
    92  		dir := filepath.Dir(file)
    93  		// In windows the debug symbols and source root can be in different cases
    94  		if isWindows {
    95  			dir = strings.ToLower(dir)
    96  		}
    97  		if strings.Index(dir, sourceRoot) != -1 {
    98  			return frame.PC, file, frame.Line, true
    99  		}
   100  		if !more {
   101  			break
   102  		}
   103  	}
   104  	return
   105  }