Created
July 29, 2019 02:55
-
-
Save mfridman/31e88e50b48680fad8ebb0ae3fb77e57 to your computer and use it in GitHub Desktop.
capture sentry alert with error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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