2015-08-02 11:38:08 +02:00
|
|
|
/*
|
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses)/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package probe implements a simple mechanism to trace and return errors in large programs.
|
|
|
|
package probe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2015-08-23 03:34:00 +02:00
|
|
|
"github.com/dustin/go-humanize"
|
2015-08-02 11:38:08 +02:00
|
|
|
)
|
|
|
|
|
2015-10-25 19:11:29 +01:00
|
|
|
var (
|
|
|
|
// Root path to the project's source.
|
|
|
|
rootPath string
|
|
|
|
// App specific info to be included reporting.
|
|
|
|
appInfo map[string]string
|
|
|
|
)
|
2015-10-24 04:59:08 +02:00
|
|
|
|
2015-10-25 19:11:29 +01:00
|
|
|
// Init initializes probe. It is typically called once from the main()
|
|
|
|
// function or at least from any source file placed at the top level
|
|
|
|
// source directory.
|
|
|
|
func Init() {
|
|
|
|
// Root path is automatically determined from the calling function's source file location.
|
2015-10-24 04:59:08 +02:00
|
|
|
// Catch the calling function's source file path.
|
|
|
|
_, file, _, _ := runtime.Caller(1)
|
|
|
|
// Save the directory alone.
|
|
|
|
rootPath = filepath.Dir(file)
|
2015-10-25 19:11:29 +01:00
|
|
|
|
|
|
|
appInfo = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAppInfo sets app speific key:value to report additionally during call trace dump.
|
|
|
|
// Eg. SetAppInfo("ReleaseTag", "RELEASE_42_0")
|
|
|
|
// SetAppInfo("Version", "42.0")
|
|
|
|
// SetAppInfo("Commit", "00611fb")
|
|
|
|
func SetAppInfo(key, value string) {
|
|
|
|
appInfo[key] = value
|
2015-10-24 04:59:08 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 11:38:08 +02:00
|
|
|
// GetSysInfo returns useful system statistics.
|
|
|
|
func GetSysInfo() map[string]string {
|
|
|
|
host, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
host = ""
|
|
|
|
}
|
|
|
|
memstats := &runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
return map[string]string{
|
|
|
|
"host.name": host,
|
|
|
|
"host.os": runtime.GOOS,
|
|
|
|
"host.arch": runtime.GOARCH,
|
|
|
|
"host.lang": runtime.Version(),
|
|
|
|
"host.cpus": strconv.Itoa(runtime.NumCPU()),
|
|
|
|
"mem.used": humanize.Bytes(memstats.Alloc),
|
|
|
|
"mem.total": humanize.Bytes(memstats.Sys),
|
|
|
|
"mem.heap.used": humanize.Bytes(memstats.HeapAlloc),
|
|
|
|
"mem.heap.total": humanize.Bytes(memstats.HeapSys),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 03:10:42 +02:00
|
|
|
// TracePoint container for individual trace entries in overall call trace
|
2015-08-19 09:58:05 +02:00
|
|
|
type TracePoint struct {
|
2015-09-18 03:10:42 +02:00
|
|
|
Line int `json:"line,omitempty"`
|
|
|
|
Filename string `json:"file,omitempty"`
|
|
|
|
Function string `json:"func,omitempty"`
|
|
|
|
Env map[string][]string `json:"env,omitempty"`
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements tracing error functionality.
|
|
|
|
type Error struct {
|
2015-08-19 04:30:17 +02:00
|
|
|
lock sync.RWMutex
|
2015-08-19 08:37:10 +02:00
|
|
|
Cause error `json:"cause,omitempty"`
|
2015-08-19 09:58:05 +02:00
|
|
|
CallTrace []TracePoint `json:"trace,omitempty"`
|
2015-08-19 08:37:10 +02:00
|
|
|
SysInfo map[string]string `json:"sysinfo,omitempty"`
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 03:10:42 +02:00
|
|
|
// NewError function instantiates an error probe for tracing.
|
|
|
|
// Default ``error`` (golang's error interface) is injected in
|
|
|
|
// only once. Rest of the time, you trace the return path with
|
|
|
|
// ``probe.Trace`` and finally handling them at top level
|
|
|
|
//
|
|
|
|
// Following dummy code talks about how one can pass up the
|
|
|
|
// errors and put them in CallTrace.
|
|
|
|
//
|
|
|
|
// func sendError() *probe.Error {
|
|
|
|
// return probe.NewError(errors.New("Help Needed"))
|
|
|
|
// }
|
|
|
|
// func recvError() *probe.Error {
|
|
|
|
// return sendError().Trace()
|
|
|
|
// }
|
|
|
|
// if err := recvError(); err != nil {
|
|
|
|
// log.Fatalln(err.Trace())
|
|
|
|
// }
|
|
|
|
//
|
2015-08-08 08:47:22 +02:00
|
|
|
func NewError(e error) *Error {
|
2015-08-04 01:17:21 +02:00
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-19 09:58:05 +02:00
|
|
|
Err := Error{lock: sync.RWMutex{}, Cause: e, CallTrace: []TracePoint{}, SysInfo: GetSysInfo()}
|
2015-10-25 19:11:29 +01:00
|
|
|
return Err.trace() // Skip NewError and only instead register the NewError's caller.
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 03:10:42 +02:00
|
|
|
// Trace records the point at which it is invoked.
|
|
|
|
// Stack traces are important for debugging purposes.
|
2015-08-02 11:38:08 +02:00
|
|
|
func (e *Error) Trace(fields ...string) *Error {
|
2015-08-03 10:46:50 +02:00
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-02 11:38:08 +02:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
2015-08-03 05:31:58 +02:00
|
|
|
return e.trace(fields...)
|
|
|
|
}
|
|
|
|
|
2015-10-25 19:11:29 +01:00
|
|
|
// trace records caller's caller. It is intended for probe's own
|
|
|
|
// internal use. Take a look at probe.NewError for example.
|
2015-08-03 05:31:58 +02:00
|
|
|
func (e *Error) trace(fields ...string) *Error {
|
2015-09-18 11:59:57 +02:00
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-03 05:31:58 +02:00
|
|
|
pc, file, line, _ := runtime.Caller(2)
|
2015-08-02 11:38:08 +02:00
|
|
|
function := runtime.FuncForPC(pc).Name()
|
|
|
|
_, function = filepath.Split(function)
|
2015-10-24 04:59:08 +02:00
|
|
|
file = strings.TrimPrefix(file, rootPath+string(os.PathSeparator)) // trims project's root path.
|
2015-08-19 09:58:05 +02:00
|
|
|
tp := TracePoint{}
|
2015-08-02 11:38:08 +02:00
|
|
|
if len(fields) > 0 {
|
2015-08-19 09:58:05 +02:00
|
|
|
tp = TracePoint{Line: line, Filename: file, Function: function, Env: map[string][]string{"Tags": fields}}
|
2015-08-02 11:38:08 +02:00
|
|
|
} else {
|
2015-08-19 09:58:05 +02:00
|
|
|
tp = TracePoint{Line: line, Filename: file, Function: function}
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
2015-08-19 04:30:17 +02:00
|
|
|
e.CallTrace = append(e.CallTrace, tp)
|
2015-08-02 11:38:08 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-09-18 03:10:42 +02:00
|
|
|
// Untrace erases last known trace entry.
|
2015-08-24 12:35:24 +02:00
|
|
|
func (e *Error) Untrace() *Error {
|
2015-08-03 10:46:50 +02:00
|
|
|
if e == nil {
|
2015-08-24 12:35:24 +02:00
|
|
|
return nil
|
2015-08-03 10:46:50 +02:00
|
|
|
}
|
2015-08-02 11:38:08 +02:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
2015-08-19 04:30:17 +02:00
|
|
|
l := len(e.CallTrace)
|
2015-08-02 11:38:08 +02:00
|
|
|
if l == 0 {
|
2015-08-24 12:35:24 +02:00
|
|
|
return nil
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
2015-08-19 04:30:17 +02:00
|
|
|
e.CallTrace = e.CallTrace[:l-1]
|
2015-08-24 12:35:24 +02:00
|
|
|
return e
|
2015-08-19 04:30:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 08:37:10 +02:00
|
|
|
// ToGoError returns original error message.
|
2015-08-19 04:30:17 +02:00
|
|
|
func (e *Error) ToGoError() error {
|
2015-09-18 11:59:57 +02:00
|
|
|
if e == nil || e.Cause == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-19 04:30:17 +02:00
|
|
|
return e.Cause
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns error message.
|
|
|
|
func (e *Error) String() string {
|
2015-08-19 04:30:17 +02:00
|
|
|
if e == nil || e.Cause == nil {
|
2015-08-03 10:46:50 +02:00
|
|
|
return "<nil>"
|
|
|
|
}
|
2015-08-02 20:57:47 +02:00
|
|
|
e.lock.RLock()
|
|
|
|
defer e.lock.RUnlock()
|
|
|
|
|
2015-08-19 04:30:17 +02:00
|
|
|
if e.Cause != nil {
|
2015-09-29 19:04:09 +02:00
|
|
|
str := e.Cause.Error()
|
2015-08-20 07:40:05 +02:00
|
|
|
callLen := len(e.CallTrace)
|
|
|
|
for i := callLen - 1; i >= 0; i-- {
|
|
|
|
if len(e.CallTrace[i].Env) > 0 {
|
2015-09-29 19:04:09 +02:00
|
|
|
str += fmt.Sprintf("\n (%d) %s:%d %s(..) Tags: [%s]",
|
2015-08-20 07:40:05 +02:00
|
|
|
i, e.CallTrace[i].Filename, e.CallTrace[i].Line, e.CallTrace[i].Function, strings.Join(e.CallTrace[i].Env["Tags"], ", "))
|
2015-08-03 10:46:50 +02:00
|
|
|
} else {
|
2015-09-29 19:04:09 +02:00
|
|
|
str += fmt.Sprintf("\n (%d) %s:%d %s(..)",
|
2015-08-20 07:40:05 +02:00
|
|
|
i, e.CallTrace[i].Filename, e.CallTrace[i].Line, e.CallTrace[i].Function)
|
2015-08-03 10:46:50 +02:00
|
|
|
}
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|
|
|
|
|
2015-10-25 19:11:29 +01:00
|
|
|
str += "\n "
|
|
|
|
|
|
|
|
for key, value := range appInfo {
|
|
|
|
str += key + ":" + value + " | "
|
|
|
|
}
|
|
|
|
|
|
|
|
str += "Host:" + e.SysInfo["host.name"] + " | "
|
2015-08-19 04:30:17 +02:00
|
|
|
str += "OS:" + e.SysInfo["host.os"] + " | "
|
|
|
|
str += "Arch:" + e.SysInfo["host.arch"] + " | "
|
|
|
|
str += "Lang:" + e.SysInfo["host.lang"] + " | "
|
|
|
|
str += "Mem:" + e.SysInfo["mem.used"] + "/" + e.SysInfo["mem.total"] + " | "
|
|
|
|
str += "Heap:" + e.SysInfo["mem.heap.used"] + "/" + e.SysInfo["mem.heap.total"]
|
2015-08-02 11:38:08 +02:00
|
|
|
|
2015-08-19 04:30:17 +02:00
|
|
|
return str
|
2015-08-03 10:46:50 +02:00
|
|
|
}
|
|
|
|
return "<nil>"
|
2015-08-02 11:38:08 +02:00
|
|
|
}
|