Documentation
¶
Overview ¶
Package bugsnag lets you monitor unhandled panics and expected errors in your golang code. For more information see https://github.com/bugsnag/bugsnag-go/
Index ¶
- Constants
- Variables
- func AutoNotify(rawData ...interface{})
- func Configure(config Configuration)
- func Handler(h http.Handler, rawData ...interface{}) http.Handler
- func HandlerFunc(h http.HandlerFunc, rawData ...interface{}) http.HandlerFunc
- func Notify(err error, rawData ...interface{}) error
- func OnBeforeNotify(callback func(event *Event, config *Configuration) error)
- func Recover(rawData ...interface{})
- type Configuration
- type Context
- type Event
- type MetaData
- type Notifier
- type User
Examples ¶
Constants ¶
const VERSION = "1.0"
The current version of the notifier
Variables ¶
var ( SeverityError = severity{"error"} SeverityWarning = severity{"warning"} SeverityInfo = severity{"info"} )
Tags used to mark the severity of the error in the Bugsnag dashboard. You can pass these to Notify or to any other function accepting rawData.
Functions ¶
func AutoNotify ¶
func AutoNotify(rawData ...interface{})
defer AutoNotify notifies Bugsnag about any panic()s. It then re-panics() so that existing error handling continues to work. The rawData is used to add information to the notification, see Notify for more information.
Example ¶
return func(w http.ResponseWriter, request *http.Request) { defer AutoNotify(request, Context{"createAccount"}) createAccount(w, request) }
Output:
func Configure ¶
func Configure(config Configuration)
Configure Bugsnag. The most important setting is the APIKey. This must be called before any other function on Bugsnag, and should be called as early as possible in your program.
Example ¶
Configure(Configuration{ APIKey: "YOUR_API_KEY_HERE", ReleaseStage: "production", // See Configuration{} for other fields })
Output:
func Handler ¶
Handler wraps the HTTP handler in bugsnag.AutoNotify(). It includes details about the HTTP request in all error reports. If you don't pass a handler, the default http handlers will be used.
Example ¶
// Set up your http handlers as usual http.HandleFunc("/", handleGet) // use bugsnag.Handler(nil) to wrap the default http handlers // so that Bugsnag is automatically notified about panics. http.ListenAndServe(":1234", Handler(nil))
Output:
Example (CustomHandlers) ¶
// If you're using custom handlers, wrap the handlers explicitly. handler := http.NewServeMux() http.HandleFunc("/", handleGet) // use bugsnag.Handler(handler) to wrap the handlers so that Bugsnag is // automatically notified about panics http.ListenAndServe(":1234", Handler(handler))
Output:
Example (CustomServer) ¶
// If you're using a custom server, set the handlers explicitly. http.HandleFunc("/", handleGet) srv := http.Server{ Addr: ":1234", ReadTimeout: 10 * time.Second, // use bugsnag.Handler(nil) to wrap the default http handlers // so that Bugsnag is automatically notified about panics. Handler: Handler(nil), } srv.ListenAndServe()
Output:
func HandlerFunc ¶
func HandlerFunc(h http.HandlerFunc, rawData ...interface{}) http.HandlerFunc
HandlerFunc wraps a HTTP handler in bugsnag.AutoNotify(). It includes details about the HTTP request in all error reports. If you've wrapped your server in an http.Handler, you don't also need to wrap each function.
func Notify ¶
Notify sends an error to Bugsnag. The rawData can be anything supported by Bugsnag, e.g. User, Context, SeverityError, MetaData, Configuration, or anything supported by your custom middleware. Unsupported values will be silently ignored.
Example ¶
_, err := net.Listen("tcp", ":80") if err != nil { Notify(err) }
Output:
func OnBeforeNotify ¶
func OnBeforeNotify(callback func(event *Event, config *Configuration) error)
OnBeforeNotify adds a callback to be run before a notification is sent to Bugsnag. It can be used to modify the event or the config to be used. If you want to prevent the event from being sent to bugsnag, return an error that explains why the notification was cancelled.
Example ¶
OnBeforeNotify(func(event *Event, config *Configuration) error { // Search all the RawData for any *Job pointers that we're passed in // to bugsnag.Notify() and friends. for _, datum := range event.RawData { if job, ok := datum.(*Job); ok { // don't notify bugsnag about errors in retries if job.Retry { return fmt.Errorf("bugsnag middleware: not notifying about job retry") } // add the job as a tab on Bugsnag.com event.MetaData.AddStruct("Job", job) // set the user correctly event.User = &User{Id: job.UserId, Email: job.UserEmail} } } // continue notifying as normal return nil })
Output:
Types ¶
type Configuration ¶
type Configuration struct { // The API key, e.g. "c9d60ae4c7e70c4b6c4ebd3e8056d2b8" APIKey string // The Bugsnag endpoint, default "https://notify.bugsnag.com/" Endpoint string // The current release stage ReleaseStage string // The currently running version of the app AppVersion string // The hostname of the current server Hostname string // Release stages to notify in, default nil implies all release stages. NotifyReleaseStages []string // packages to consider in-project, default: {"main*"} ProjectPackages []string // keys to filter out of meta-data, default: {"password", "secret"} ParamsFilters []string // A function to install a PanicHandler, defaults to panicwrap. PanicHandler func() // The logger to use, defaults to the global logger Logger *log.Logger // The http Transport to use, defaults to the default http Transport Transport http.RoundTripper // Whether bugsnag should notify synchronously, default: false Synchronous bool }
var Config Configuration
The configuration for the default bugsnag notifier.
type Context ¶
type Context struct {
String string
}
Sets the context of the error in Bugsnag. You can pass this to Notify or any other function accepting rawData.
type Event ¶
type Event struct { // The original error that caused this event, not sent to Bugsnag Error *errors.Error // The rawData affecting this error, not sent to Bugsnag RawData []interface{} // The error class to be sent to Bugsnag. This defaults to the type name of the Error ErrorClass string // The error message to be sent to Bugsnag. This defaults to the return value of Error.Error() Message string // The stacktrrace of the error to be sent to Bugsnag. Stacktrace []stackFrame // The context to be sent to Bugsnag. This should be set to the part of the app that was running, // e.g. for http requests, set it to the path. Context string // The severity of the error. Can be SeverityError, SeverityWarning or SeverityInfo Severity severity // The grouping hash is used to override Bugsnag's grouping. Set this if you'd like all errors with // the same grouping hash to group together in the dashboard. GroupingHash string // The searchable user data to send to Bugsnag. User *User // Other meta-data to send to Bugsnag. Appears as a set of tabbed tables in the dashboard. MetaData MetaData }
An event to send to Bugsnag. This is passed through the middleware stack.
type MetaData ¶
MetaData is added to the Bugsnag dashboard in tabs. Each tab is a map of strings -> values. You can pass MetaData to Notify and any other function that accepts RawData
Example ¶
notifier.Notify(errors.Errorf("hi world"), MetaData{"Account": { "id": account.Id, "name": account.Name, "paying?": account.Plan.Premium, }})
Output:
type Notifier ¶
type Notifier struct { Config *Configuration RawData []interface{} }
func New ¶
func New(rawData ...interface{}) *Notifier
Creates a new notifier. You can pass an instance of bugsnag.Configuration in rawData to change the configuration. Other values of rawData will be passed to Notify.
func (*Notifier) AutoNotify ¶
func (notifier *Notifier) AutoNotify(rawData ...interface{})
defer AutoNotify() sends any panics that happen to Bugsnag, along with any rawData you set here. After the notification is sent, panic() is called again with the same error so that the panic() bubbles out.
type User ¶
type User struct { Id string `json:"id,omitempty"` Name string `json:"name,omitempty"` Email string `json:"email,omitempty"` }
Sets the searchable user-data on Bugsnag. The Id is also used to determine the number of users affected by a bug. You can pass this to Notify or any other function accepting rawData.