From d11e183e525c370fb38be3bccf384f70c9cc701e Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Fri, 21 Feb 2020 13:05:28 +0100 Subject: [PATCH 1/2] clean filepath to fix windows path issue --- agent/agent.go | 7 +++++-- ast/sources.go | 3 +++ instrumentation/logging/logger.go | 2 ++ instrumentation/testing/tb.go | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index b6a0803b..2929c922 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path" + "path/filepath" "runtime" "strings" "sync" @@ -260,7 +261,7 @@ func NewAgent(options ...Option) (*Agent, error) { // Current folder wd, _ := os.Getwd() - agent.metadata[tags.CurrentFolder] = wd + agent.metadata[tags.CurrentFolder] = filepath.Clean(wd) // Hostname hostname, _ := os.Hostname() @@ -292,7 +293,9 @@ func NewAgent(options ...Option) (*Agent, error) { // Expand '~' in source root if sRoot, ok := agent.metadata[tags.SourceRoot]; ok { - if sRootEx, err := homedir.Expand(sRoot.(string)); err == nil { + cSRoot := sRoot.(string) + cSRoot = filepath.Clean(cSRoot) + if sRootEx, err := homedir.Expand(cSRoot); err == nil { agent.metadata[tags.SourceRoot] = sRootEx } } diff --git a/ast/sources.go b/ast/sources.go index 11cfb42b..a85fee45 100644 --- a/ast/sources.go +++ b/ast/sources.go @@ -4,6 +4,7 @@ import ( "go/ast" "go/parser" "go/token" + "path/filepath" "runtime" "strings" "sync" @@ -36,6 +37,7 @@ func GetFuncSourceFromCaller(skip int) (*MethodCodeBoundaries, error) { func GetFuncSourceForName(pc uintptr, name string) (*MethodCodeBoundaries, error) { mFunc := runtime.FuncForPC(pc) mFile, _ := mFunc.FileLine(pc) + mFile = filepath.Clean(mFile) fileCode, err := getCodesForFile(mFile) if err != nil { return nil, err @@ -47,6 +49,7 @@ func GetFuncSourceForName(pc uintptr, name string) (*MethodCodeBoundaries, error func GetFuncSource(pc uintptr) (*MethodCodeBoundaries, error) { mFunc := runtime.FuncForPC(pc) mFile, _ := mFunc.FileLine(pc) + mFile = filepath.Clean(mFile) fileCode, err := getCodesForFile(mFile) if err != nil { return nil, err diff --git a/instrumentation/logging/logger.go b/instrumentation/logging/logger.go index 1dfdce02..d9e95fcb 100644 --- a/instrumentation/logging/logger.go +++ b/instrumentation/logging/logger.go @@ -4,6 +4,7 @@ import ( "fmt" "io" stdlog "log" + "path/filepath" "regexp" "sync" "time" @@ -167,6 +168,7 @@ func (w *otWriter) storeLogRecord(item *logItem) { log.String(tags.EventMessage, item.message), } if item.file != "" && item.lineNumber != "" { + item.file = filepath.Clean(item.file) fields = append(fields, log.String(tags.EventSource, fmt.Sprintf("%s:%s", item.file, item.lineNumber))) } w.logRecords = append(w.logRecords, opentracing.LogRecord{ diff --git a/instrumentation/testing/tb.go b/instrumentation/testing/tb.go index 61041dea..95b5f657 100644 --- a/instrumentation/testing/tb.go +++ b/instrumentation/testing/tb.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/opentracing/opentracing-go/log" "go.undefinedlabs.com/scopeagent/tags" + "path/filepath" "runtime" ) @@ -219,11 +220,13 @@ func (test *Test) Helper() { func getSourceFileAndNumber() string { var source string if pc, file, line, ok := runtime.Caller(2); ok == true { + file = filepath.Clean(file) pcEntry := runtime.FuncForPC(pc).Entry() // Try to detect the patch function if isAPatchPointer(pcEntry) { // The monkey patching version adds 4 frames to the stack. if _, file, line, ok := runtime.Caller(6); ok == true { + file = filepath.Clean(file) source = fmt.Sprintf("%s:%d", file, line) } } else { From 138e6cf2b8de4e1c5e81064435a85ce08a0539b9 Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Fri, 21 Feb 2020 15:22:52 +0100 Subject: [PATCH 2/2] move clean method --- instrumentation/testing/tb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/testing/tb.go b/instrumentation/testing/tb.go index 95b5f657..b1a2f503 100644 --- a/instrumentation/testing/tb.go +++ b/instrumentation/testing/tb.go @@ -220,7 +220,6 @@ func (test *Test) Helper() { func getSourceFileAndNumber() string { var source string if pc, file, line, ok := runtime.Caller(2); ok == true { - file = filepath.Clean(file) pcEntry := runtime.FuncForPC(pc).Entry() // Try to detect the patch function if isAPatchPointer(pcEntry) { @@ -231,6 +230,7 @@ func getSourceFileAndNumber() string { } } else { // If we don't have monkey patching then we skip 2 frames + file = filepath.Clean(file) source = fmt.Sprintf("%s:%d", file, line) } }