Skip to content

Instantly share code, notes, and snippets.

@mfridman
Created July 29, 2019 02:55
Show Gist options
  • Save mfridman/31e88e50b48680fad8ebb0ae3fb77e57 to your computer and use it in GitHub Desktop.
Save mfridman/31e88e50b48680fad8ebb0ae3fb77e57 to your computer and use it in GitHub Desktop.
capture sentry alert with error
func (a *alertWrapper) Error(err error) error {
// sentry Go SDK uses asynchronous transport, if fatal (os.Exit) or panic is called after the alert
// the event might get dropped, so we call Flush explicitly.
defer sentry.Flush(time.Second * 2)
hub := sentry.CurrentHub().Clone()
ev := sentry.NewEvent()
ev.Level = sentry.LevelError
// add custom tags here.. optional
ev.Tags = map[string]string{}
ev.Message = err.Error()
ev.Extra = map[string]interface{}{
"Version": runtime.Version(),
"NumCPU": runtime.NumCPU(),
"GOMAXPROCS": runtime.GOMAXPROCS(0),
"NumGoroutine": runtime.NumGoroutine(),
}
stack := sentry.ExtractStacktrace(err)
ev.Exception = []sentry.Exception{{
Type: err.Error(),
Value: fmt.Sprintf("%T", err),
Stacktrace: stack,
}}
if _, ok := err.(interface{ Cause() error }); ok {
e := errors.Cause(err)
ev.Exception[0].Type = e.Error()
ev.Exception[0].Value = fmt.Sprintf("%T", e)
}
hub.CaptureEvent(ev)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment