You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
minio/pkg/iodine/iodine.go

230 lines
5.4 KiB

10 years ago
/*
10 years ago
* Iodine, (C) 2015 Minio, Inc.
10 years ago
*
* 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 iodine
import (
"bytes"
"encoding/json"
"errors"
10 years ago
"fmt"
"os"
"path/filepath"
"reflect"
10 years ago
"runtime"
"strconv"
10 years ago
"strings"
"sync"
"github.com/dustin/go-humanize"
10 years ago
)
// Error is the iodine error which contains a pointer to the original error
10 years ago
// and stack traces.
type Error struct {
10 years ago
EmbeddedError error `json:"-"`
ErrorMessage string
ErrorType string
10 years ago
Stack []StackEntry
}
10 years ago
// StackEntry contains the entry in the stack trace
10 years ago
type StackEntry struct {
Host string
File string
Func string
10 years ago
Line int
Data map[string]string
}
10 years ago
var gopath string
var globalState = struct {
sync.RWMutex
m map[string]string
}{m: make(map[string]string)}
10 years ago
// SetGlobalState - set global state
func SetGlobalState(key, value string) {
globalState.Lock()
globalState.m[key] = value
globalState.Unlock()
}
10 years ago
// ClearGlobalState - clear info in globalState struct
func ClearGlobalState() {
globalState.Lock()
10 years ago
for k := range globalState.m {
delete(globalState.m, k)
}
globalState.Unlock()
}
10 years ago
// GetGlobalState - get map from globalState struct
func GetGlobalState() map[string]string {
result := make(map[string]string)
globalState.RLock()
for k, v := range globalState.m {
result[k] = v
}
globalState.RUnlock()
return result
}
10 years ago
// GetGlobalStateKey - get value for key from globalState struct
func GetGlobalStateKey(k string) string {
globalState.RLock()
result, _ := globalState.m[k]
globalState.RUnlock()
10 years ago
return result
}
// ToError returns the input if it is not an iodine error. It returns the embedded error if it is an iodine error. If nil, returns nil.
func ToError(err error) error {
switch err := err.(type) {
case nil:
{
return nil
}
case Error:
{
if err.EmbeddedError != nil {
return err.EmbeddedError
}
return errors.New(err.ErrorMessage)
}
default:
{
return err
}
}
}
// New - instantiate an error, turning it into an iodine error.
10 years ago
// Adds an initial stack trace.
func New(err error, data map[string]string) error {
10 years ago
if err != nil {
entry := createStackEntry()
var newErr Error
// check if error is wrapped
switch typedError := err.(type) {
case Error:
{
newErr = typedError
}
default:
{
newErr = Error{
EmbeddedError: err,
ErrorMessage: err.Error(),
ErrorType: reflect.TypeOf(err).String(),
Stack: []StackEntry{},
}
}
}
10 years ago
for k, v := range data {
entry.Data[k] = v
}
newErr.Stack = append(newErr.Stack, entry)
return newErr
10 years ago
}
10 years ago
return nil
10 years ago
}
10 years ago
// createStackEntry - create stack entries
10 years ago
func createStackEntry() StackEntry {
host, _ := os.Hostname()
pc, file, line, _ := runtime.Caller(2)
function := runtime.FuncForPC(pc).Name()
_, function = filepath.Split(function)
10 years ago
file = strings.TrimPrefix(file, gopath) // trim gopath from file
data := GetGlobalState()
for k, v := range getSystemData() {
data[k] = v
}
10 years ago
entry := StackEntry{
Host: host,
File: file,
Func: function,
10 years ago
Line: line,
10 years ago
Data: data,
10 years ago
}
return entry
}
10 years ago
func getSystemData() map[string]string {
host, err := os.Hostname()
if err != nil {
host = ""
}
memstats := &runtime.MemStats{}
runtime.ReadMemStats(memstats)
return map[string]string{
"sys.host": host,
"sys.os": runtime.GOOS,
"sys.arch": runtime.GOARCH,
"sys.go": runtime.Version(),
"sys.cpus": strconv.Itoa(runtime.NumCPU()),
"sys.mem.used": humanize.Bytes(memstats.Alloc),
"sys.mem.allocated": humanize.Bytes(memstats.TotalAlloc),
"sys.mem.heap.used": humanize.Bytes(memstats.HeapAlloc),
"sys.mem.heap.allocated": humanize.Bytes(memstats.HeapSys),
10 years ago
}
}
10 years ago
// Annotate an error with a stack entry and returns itself
//func (err *WrappedError) Annotate(info map[string]string) *WrappedError {
// entry := createStackEntry()
// for k, v := range info {
// entry.Data[k] = v
// }
// err.Stack = append(err.Stack, entry)
// return err
//}
10 years ago
10 years ago
// EmitJSON writes JSON output for the error
func (err Error) EmitJSON() ([]byte, error) {
10 years ago
return json.Marshal(err)
}
10 years ago
// EmitHumanReadable returns a human readable error message
func (err Error) EmitHumanReadable() string {
10 years ago
var errorBuffer bytes.Buffer
fmt.Fprintln(&errorBuffer, err.ErrorMessage)
10 years ago
for i, entry := range err.Stack {
prettyData, _ := json.Marshal(entry.Data)
fmt.Fprintln(&errorBuffer, "-", i, entry.Host+":"+entry.File+":"+strconv.Itoa(entry.Line)+" "+entry.Func+"():", string(prettyData))
10 years ago
}
return string(errorBuffer.Bytes())
}
10 years ago
// Emits the original error message
func (err Error) Error() string {
return err.EmitHumanReadable()
10 years ago
}
10 years ago
func init() {
_, iodineFile, _, _ := runtime.Caller(0)
iodineFile = filepath.Dir(iodineFile) // trim iodine.go
iodineFile = filepath.Dir(iodineFile) // trim iodine
iodineFile = filepath.Dir(iodineFile) // trim minio
gopath = filepath.Dir(iodineFile) + "/" // trim github.com
10 years ago
}