From 577d10674da9e06eaaa2b9330b9f54113f73431e Mon Sep 17 00:00:00 2001 From: kannappanr <30541348+kannappanr@users.noreply.github.com> Date: Mon, 18 Jun 2018 12:04:46 -0700 Subject: [PATCH] Cleanup stack trace in error logs (#6045) Add compile time GOROOT path to the list of prefix of file paths to be removed. Add webhandler function names to the slice that stores function names to terminate logging. --- buildscripts/gen-ldflags.go | 1 + cmd/build-constants.go | 3 +++ cmd/gateway-main.go | 2 +- cmd/logger/logger.go | 50 ++++++++++++++++++++++++++++++------- cmd/logger/utils.go | 13 ++++++++++ cmd/server-main.go | 2 +- 6 files changed, 60 insertions(+), 11 deletions(-) diff --git a/buildscripts/gen-ldflags.go b/buildscripts/gen-ldflags.go index 6ea04b742..497bc11f9 100644 --- a/buildscripts/gen-ldflags.go +++ b/buildscripts/gen-ldflags.go @@ -33,6 +33,7 @@ func genLDFlags(version string) string { ldflagsStr += " -X github.com/minio/minio/cmd.CommitID=" + commitID() ldflagsStr += " -X github.com/minio/minio/cmd.ShortCommitID=" + commitID()[:12] ldflagsStr += " -X github.com/minio/minio/cmd.GOPATH=" + os.Getenv("GOPATH") + ldflagsStr += " -X github.com/minio/minio/cmd.GOROOT=" + os.Getenv("GOROOT") return ldflagsStr } diff --git a/cmd/build-constants.go b/cmd/build-constants.go index ff2a55f57..8d90168ec 100644 --- a/cmd/build-constants.go +++ b/cmd/build-constants.go @@ -22,6 +22,9 @@ var ( // GOPATH - GOPATH value at the time of build. GOPATH = "" + // GOROOT - GOROOT value at the time of build. + GOROOT = "" + // Go get development tag. goGetTag = "DEVELOPMENT.GOGET" diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index fae5535df..60c9ef845 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -34,7 +34,7 @@ import ( ) func init() { - logger.Init(GOPATH) + logger.Init(GOPATH, GOROOT) logger.RegisterUIError(fmtError) } diff --git a/cmd/logger/logger.go b/cmd/logger/logger.go index 7d779ec14..b815452a6 100644 --- a/cmd/logger/logger.go +++ b/cmd/logger/logger.go @@ -52,6 +52,25 @@ var matchingFuncNames = [...]string{ "http.HandlerFunc.ServeHTTP", "cmd.serverMain", "cmd.StartGateway", + "cmd.(*webAPIHandlers).ListBuckets", + "cmd.(*webAPIHandlers).MakeBucket", + "cmd.(*webAPIHandlers).DeleteBucket", + "cmd.(*webAPIHandlers).ListObjects", + "cmd.(*webAPIHandlers).RemoveObject", + "cmd.(*webAPIHandlers).Login", + "cmd.(*webAPIHandlers).GenerateAuth", + "cmd.(*webAPIHandlers).SetAuth", + "cmd.(*webAPIHandlers).GetAuth", + "cmd.(*webAPIHandlers).CreateURLToken", + "cmd.(*webAPIHandlers).Upload", + "cmd.(*webAPIHandlers).Download", + "cmd.(*webAPIHandlers).DownloadZip", + "cmd.(*webAPIHandlers).GetBucketPolicy", + "cmd.(*webAPIHandlers).ListAllBucketPolicies", + "cmd.(*webAPIHandlers).SetBucketPolicy", + "cmd.(*webAPIHandlers).PresignedGet", + "cmd.(*webAPIHandlers).ServerInfo", + "cmd.(*webAPIHandlers).StorageInfo", // add more here .. } @@ -142,22 +161,24 @@ func RegisterUIError(f func(string, error, bool) string) { // and GOROOT directories. Also append github.com/minio/minio // This is done to clean up the filename, when stack trace is // displayed when an error happens. -func Init(goPath string) { +func Init(goPath string, goRoot string) { var goPathList []string + var goRootList []string var defaultgoPathList []string + var defaultgoRootList []string + pathSeperator := ":" // Add all possible GOPATH paths into trimStrings // Split GOPATH depending on the OS type if runtime.GOOS == "windows" { - goPathList = strings.Split(goPath, ";") - defaultgoPathList = strings.Split(build.Default.GOPATH, ";") - } else { - // All other types of OSs - goPathList = strings.Split(goPath, ":") - defaultgoPathList = strings.Split(build.Default.GOPATH, ":") - + pathSeperator = ";" } + goPathList = strings.Split(goPath, pathSeperator) + goRootList = strings.Split(goRoot, pathSeperator) + defaultgoPathList = strings.Split(build.Default.GOPATH, pathSeperator) + defaultgoRootList = strings.Split(build.Default.GOROOT, pathSeperator) + // Add trim string "{GOROOT}/src/" into trimStrings trimStrings = []string{filepath.Join(runtime.GOROOT(), "src") + string(filepath.Separator)} @@ -167,10 +188,21 @@ func Init(goPath string) { trimStrings = append(trimStrings, filepath.Join(goPathString, "src")+string(filepath.Separator)) } + for _, goRootString := range goRootList { + trimStrings = append(trimStrings, filepath.Join(goRootString, "src")+string(filepath.Separator)) + } + for _, defaultgoPathString := range defaultgoPathList { trimStrings = append(trimStrings, filepath.Join(defaultgoPathString, "src")+string(filepath.Separator)) } + for _, defaultgoRootString := range defaultgoRootList { + trimStrings = append(trimStrings, filepath.Join(defaultgoRootString, "src")+string(filepath.Separator)) + } + + // Remove duplicate entries. + trimStrings = uniqueEntries(trimStrings) + // Add "github.com/minio/minio" as the last to cover // paths like "{GOROOT}/src/github.com/minio/minio" // and "{GOPATH}/src/github.com/minio/minio" @@ -200,7 +232,7 @@ func getTrace(traceLevel int) []string { var trace []string pc, file, lineNumber, ok := runtime.Caller(traceLevel) - for ok { + for ok && file != "" { // Clean up the common prefixes file = trimTrace(file) // Get the function name diff --git a/cmd/logger/utils.go b/cmd/logger/utils.go index a18186925..4368e4daf 100644 --- a/cmd/logger/utils.go +++ b/cmd/logger/utils.go @@ -50,3 +50,16 @@ func ansiSaveAttributes() { func ansiRestoreAttributes() { ansiEscape("8") } + +func uniqueEntries(paths []string) []string { + found := map[string]bool{} + unqiue := []string{} + + for v := range paths { + if _, ok := found[paths[v]]; !ok { + found[paths[v]] = true + unqiue = append(unqiue, paths[v]) + } + } + return unqiue +} diff --git a/cmd/server-main.go b/cmd/server-main.go index a9dbdf250..b9147096d 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -33,7 +33,7 @@ import ( ) func init() { - logger.Init(GOPATH) + logger.Init(GOPATH, GOROOT) logger.RegisterUIError(fmtError) }