1package sentry 2 3import ( 4 "context" 5 "time" 6) 7 8// Version is the version of the SDK. 9const Version = "0.11.0" 10 11// apiVersion is the minimum version of the Sentry API compatible with the 12// sentry-go SDK. 13const apiVersion = "7" 14 15// userAgent is the User-Agent of outgoing HTTP requests. 16const userAgent = "sentry-go/" + Version 17 18// Init initializes the SDK with options. The returned error is non-nil if 19// options is invalid, for instance if a malformed DSN is provided. 20func Init(options ClientOptions) error { 21 hub := CurrentHub() 22 client, err := NewClient(options) 23 if err != nil { 24 return err 25 } 26 hub.BindClient(client) 27 return nil 28} 29 30// AddBreadcrumb records a new breadcrumb. 31// 32// The total number of breadcrumbs that can be recorded are limited by the 33// configuration on the client. 34func AddBreadcrumb(breadcrumb *Breadcrumb) { 35 hub := CurrentHub() 36 hub.AddBreadcrumb(breadcrumb, nil) 37} 38 39// CaptureMessage captures an arbitrary message. 40func CaptureMessage(message string) *EventID { 41 hub := CurrentHub() 42 return hub.CaptureMessage(message) 43} 44 45// CaptureException captures an error. 46func CaptureException(exception error) *EventID { 47 hub := CurrentHub() 48 return hub.CaptureException(exception) 49} 50 51// CaptureEvent captures an event on the currently active client if any. 52// 53// The event must already be assembled. Typically code would instead use 54// the utility methods like CaptureException. The return value is the 55// event ID. In case Sentry is disabled or event was dropped, the return value will be nil. 56func CaptureEvent(event *Event) *EventID { 57 hub := CurrentHub() 58 return hub.CaptureEvent(event) 59} 60 61// Recover captures a panic. 62func Recover() *EventID { 63 if err := recover(); err != nil { 64 hub := CurrentHub() 65 return hub.Recover(err) 66 } 67 return nil 68} 69 70// RecoverWithContext captures a panic and passes relevant context object. 71func RecoverWithContext(ctx context.Context) *EventID { 72 if err := recover(); err != nil { 73 var hub *Hub 74 75 if HasHubOnContext(ctx) { 76 hub = GetHubFromContext(ctx) 77 } else { 78 hub = CurrentHub() 79 } 80 81 return hub.RecoverWithContext(ctx, err) 82 } 83 return nil 84} 85 86// WithScope is a shorthand for CurrentHub().WithScope. 87func WithScope(f func(scope *Scope)) { 88 hub := CurrentHub() 89 hub.WithScope(f) 90} 91 92// ConfigureScope is a shorthand for CurrentHub().ConfigureScope. 93func ConfigureScope(f func(scope *Scope)) { 94 hub := CurrentHub() 95 hub.ConfigureScope(f) 96} 97 98// PushScope is a shorthand for CurrentHub().PushScope. 99func PushScope() { 100 hub := CurrentHub() 101 hub.PushScope() 102} 103 104// PopScope is a shorthand for CurrentHub().PopScope. 105func PopScope() { 106 hub := CurrentHub() 107 hub.PopScope() 108} 109 110// Flush waits until the underlying Transport sends any buffered events to the 111// Sentry server, blocking for at most the given timeout. It returns false if 112// the timeout was reached. In that case, some events may not have been sent. 113// 114// Flush should be called before terminating the program to avoid 115// unintentionally dropping events. 116// 117// Do not call Flush indiscriminately after every call to CaptureEvent, 118// CaptureException or CaptureMessage. Instead, to have the SDK send events over 119// the network synchronously, configure it to use the HTTPSyncTransport in the 120// call to Init. 121func Flush(timeout time.Duration) bool { 122 hub := CurrentHub() 123 return hub.Flush(timeout) 124} 125 126// LastEventID returns an ID of last captured event. 127func LastEventID() EventID { 128 hub := CurrentHub() 129 return hub.LastEventID() 130} 131