github.com/lulzWill/go-agent@v2.1.2+incompatible/examples/_pkgerrors/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 newrelic "github.com/lulzWill/go-agent" 9 "github.com/lulzWill/go-agent/_integrations/nrpkgerrors" 10 "github.com/pkg/errors" 11 ) 12 13 type sampleError string 14 15 func (e sampleError) Error() string { 16 return string(e) 17 } 18 19 func alpha() error { 20 return errors.WithStack(sampleError("alpha is the cause")) 21 } 22 23 func beta() error { 24 return errors.WithStack(alpha()) 25 } 26 27 func gamma() error { 28 return errors.Wrap(beta(), "gamma was involved") 29 } 30 31 func mustGetEnv(key string) string { 32 if val := os.Getenv(key); "" != val { 33 return val 34 } 35 panic(fmt.Sprintf("environment variable %s unset", key)) 36 } 37 38 func main() { 39 cfg := newrelic.NewConfig("pkg/errors app", mustGetEnv("NEW_RELIC_LICENSE_KEY")) 40 cfg.Logger = newrelic.NewDebugLogger(os.Stdout) 41 app, err := newrelic.NewApplication(cfg) 42 if nil != err { 43 fmt.Println(err) 44 os.Exit(1) 45 } 46 47 if err := app.WaitForConnection(5 * time.Second); nil != err { 48 fmt.Println(err) 49 } 50 51 txn := app.StartTransaction("has-error", nil, nil) 52 e := gamma() 53 txn.NoticeError(nrpkgerrors.Wrap(e)) 54 txn.End() 55 56 app.Shutdown(10 * time.Second) 57 }