Negroni integration guide

Add Bugsnag to your Negroni applications.

This library supports Go 1.7 and above. To integrate with older versions of Go, check out our legacy integration.


Download the code via go get:

go get

Basic configuration

  1. Add bugsnagnegroni.AutoNotify immediately after the negroni.Recovery middleware in main.go. This causes unhandled panics to notify Bugsnag. If you are using negroni.Classic() to initialise Negroni, then it will automatically initialize Recovery for you, so just add the bugsnag AutoNotify after that call instead.

    import ""
    import ""
    func main() {
        // Existing code initializes Negroni as 'm'
        errorHandlerConfig := bugsnag.Configuration{
            // Your Bugsnag project API key
            APIKey:          "YOUR API KEY HERE",
            // The import paths for the Go packages containing your source files
            ProjectPackages: []string{"main", ""},

    You can find your API key in Project Settings.

  2. Use your bugsnag.Configuration if you need to notify about non-fatal errors or recover from panics within goroutines.

    type App struct {
        errorHandlerConfig bugsnag.Configuration
    // (app initialization) ...
    func MyHandler(w http.ResponseWriter, req *http.Request) {
        if err := processWhichMayErr() {
            bugsnag.Notify(err, ctx, app.errorHandlerConfig)

Using context.Context to manage HTTP session data

We strongly recommend passing in context.Context to appropriate Bugsnag calls. Passing the context.Context from *http.Request objects will automatically attach HTTP request data to your error reports, as well as session data that helps Bugsnag track the stability of your application. For more information around how to use context.Context with Bugsnag, see the context reference here.

Context can be retrieved from an HTTP request or created if outside a request (see session tracking for more details on sessions)

// Get context from an HTTP request
ctx = request.Context()
// or from manually starting a session outside a http request
ctx = bugsnag.StartSession(context.Background())

Reporting unhandled panics

After completing installation and basic configuration, unhandled panics will be automatically reported. Error data will begin to appear in your project’s dashboard. To ensure panics from goroutines are handled effectively, see reporting panics from goroutines.

This feature is enabled via the Configuration.PanicHandler, which forks and monitors the application as a sub-process, reporting any unhandled panics.

Reporting panics from goroutines

Since goroutines are generally non-blocking, panics captured on a goroutine may crash the application before having the opportunity to be delivered to Bugsnag unless intentionally handled.

Use bugsnag.AutoNotify() to notify bugsnag of a panic while letting the program continue to panic. This is useful if you’re using a framework that already has some handling of panics and you are retrofitting Bugsnag support.

go func(ctx context.Context) {
    defer bugsnag.AutoNotify(ctx)

    // ...

To avoid a panic in a goroutine from crashing your entire app, you can use bugsnag.Recover() to stop a panic from unwinding the stack any further. When Recover() is hit, it will send any current panic to Bugsnag and then stop panicking. This is most useful at the start of a goroutine:

go func(ctx context.Context) {
    defer bugsnag.Recover(ctx)

    // ...

See this reference to learn more about the ctx argument and the advantages of using it.

Reporting handled errors

Sometimes it is useful to manually notify Bugsnag of a problem. To do this, call bugsnag.Notify()

if err != nil {
    bugsnag.Notify(err, ctx)

When reporting handled errors, it’s often helpful to send us custom diagnostic data or to adjust the severity of particular errors. For more information, see the reporting handled errors reference.

Sending diagnostic data

Most functions in the Bugsnag API, including bugsnag.Notify(), bugsnag.Recover(), bugsnag.AutoNotify(), and bugsnag.Handler() let you attach data to the notifications that they send. To do this you pass in rawData, which can be any of the supported types listed here.

Custom metaData appears as tabs on error reports on your Bugsnag dashboard. You can set it by passing a bugsnag.MetaData object as rawData.

bugsnag.Notify(err, ctx,
        "Account": {
            "Name": Account.Name,
            "Paying": Account.Plan.Premium,

For more information, see the reporting handled errors reference.

See this reference to learn more about the ctx argument and the advantages of using it.

Identifying users

User data is searchable, and the Id powers the count of users affected. You can set which user an error affects by passing a bugsnag.User object as rawData.

bugsnag.Notify(err, ctx,
    bugsnag.User{Id: "1234", Name: "Conrad", Email: ""})

For more information, see the reporting handled errors reference.

See this reference to learn more about the ctx argument and the advantages of using it.

Session tracking

Bugsnag tracks the number of “sessions” that happen within your application. This allows you to compare stability scores between releases and helps you to understand the quality of your releases.

Sessions are captured and reported by default. This behavior can be disabled using the AutoCaptureSessions configuration option.

Tracking releases

Configure your app version to see the release that each error was introduced in.

    AppVersion: "1.2.3",

Then set up a build tool integration to enable linking to code in your source control provider from the releases dashboard, timeline annotations, and stack traces.

Next steps