Install the Bugstack Go SDK

The SDK reports errors and panics to Bugstack, which reproduces each one as a failing test in an isolated sandbox and sends a proven fix as a pull request. Setup for net/http, Gin, Echo and Fiber, panic recovery and manual capture, with the full config reference.

Compatibility

Go 1.21+

Bugstack proves each fix by running your own tests with go test in its verification sandbox: the reproduction test fails before the change and passes after it, with the rest of your suite green. How the sandbox works.

Install

go get github.com/MasonBachmann7/bugstack-go

Initialize

Call bugstack.Init() in main() with your API key and the Bugstack endpoint, and defer bugstack.Flush(). Pass Endpoint explicitly: the published SDK does not default to it yet. The import path is github.com/MasonBachmann7/bugstack-go; the repository itself now lives at github.com/Bugstackai/Bugstack-Go.

package main

import (
    "os"

    bugstack "github.com/MasonBachmann7/bugstack-go"
)

func main() {
    bugstack.Init(bugstack.Config{
        APIKey:   os.Getenv("BUGSTACK_API_KEY"),
        Endpoint: "https://dashboard.bugstack.ai/api/ingest/sdk",
    })
    defer bugstack.Flush() // send what is queued before the program exits

    if err := riskyOperation(); err != nil {
        bugstack.CaptureError(err)
    }
}

net/http

Wrap your handler with middleware.NetHTTP. It reports a panic in a handler, then re-panics so net/http handles it as usual. Errors your handlers return or log are not reported unless you call bugstack.CaptureError.

// In main.go:
import bugstack "github.com/MasonBachmann7/bugstack-go"

endpoint := os.Getenv("BUGSTACK_ENDPOINT")
if endpoint == "" {
    endpoint = "https://dashboard.bugstack.ai/api/ingest/sdk"
}
bugstack.Init(bugstack.Config{
    APIKey:   os.Getenv("BUGSTACK_API_KEY"),
    Endpoint: endpoint,
})
defer bugstack.Flush()

// Report panics from your handlers:
import "github.com/MasonBachmann7/bugstack-go/middleware"

http.ListenAndServe(":8080", middleware.NetHTTP(mux))

Panic Recovery

Report a panic outside an HTTP handler, such as in a goroutine you start:

func handleJob(job Job) {
    defer bugstack.Recover() // reports the panic and recovers from it

    // If this panics, bugstack reports it
    process(job)
}

A Go fatal error (a deadlock, a concurrent map write) is not a panic and cannot be recovered or reported.

Gin, Echo and Fiber

Gin, Echo and Fiber: initialize as above, then add this small middleware. It reports a panic with its route and method, then re-panics so the framework's own recovery still answers with a 500. (The SDK's own Gin and Echo helpers do not compile against those frameworks in the current release, so use this instead.)

Gin

r := gin.Default()
// After gin.Default(), so gin's own recovery still answers with a 500:
r.Use(func(c *gin.Context) {
    defer func() {
        if rec := recover(); rec != nil {
            if client := bugstack.GetClient(); client != nil {
                client.RecoverPanic(rec, bugstack.WithRequest(&bugstack.RequestContext{
                    Route:  c.FullPath(),
                    Method: c.Request.Method,
                }))
            }
            panic(rec)
        }
    }()
    c.Next()
})

Echo

e.Use(middleware.Recover()) // answers with a 500 after bugstack has seen the panic
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        defer func() {
            if rec := recover(); rec != nil {
                if client := bugstack.GetClient(); client != nil {
                    client.RecoverPanic(rec, bugstack.WithRequest(&bugstack.RequestContext{
                        Route:  c.Path(),
                        Method: c.Request().Method,
                    }))
                }
                panic(rec)
            }
        }()
        return next(c)
    }
})

Fiber

import fiberrecover "github.com/gofiber/fiber/v2/middleware/recover"

app.Use(fiberrecover.New()) // answers with a 500 after bugstack has seen the panic
app.Use(func(c *fiber.Ctx) error {
    defer func() {
        if rec := recover(); rec != nil {
            if client := bugstack.GetClient(); client != nil {
                client.RecoverPanic(rec, bugstack.WithRequest(&bugstack.RequestContext{
                    Route:  c.Route().Path,
                    Method: c.Method(),
                }))
            }
            panic(rec)
        }
    }()
    return c.Next()
})

Configuration Reference

Every option is a field of bugstack.Config.

Option Type Default Description
APIKey string — Required. Your project API key (starts bsk_).
Endpoint string a placeholder address Where reports go. Set it to https://dashboard.bugstack.ai/api/ingest/sdk.
ProjectID string "" Explicit project identifier.
Environment string "production" Environment name sent with each report.
Enabled *bool true when unset Master switch. Turn capture off with bugstack.Bool(false).
Debug bool false Log SDK activity to stderr.
DryRun bool false Log events to stderr instead of sending them.
DeduplicationWindow float64 300 Seconds during which the same error is sent only once. Bugstack also folds repeats within 24 hours.
Timeout float64 5 HTTP timeout in seconds.
MaxRetries int 3 Attempts per report.
IgnoredErrors []string nil Substrings of error messages not to report.
BeforeSend func(*Event) *Event nil Change an event before it is sent, or return nil to drop it.
AutoFix bool false Sent with each report. What Bugstack does with a fix is set by the repository's autonomy level in Bugstack, not by this flag.

Data Control

BeforeSend hook example

Drop errors from a health check:

bugstack.Init(bugstack.Config{
    APIKey:   os.Getenv("BUGSTACK_API_KEY"),
    Endpoint: "https://dashboard.bugstack.ai/api/ingest/sdk",
    // Drop errors from the health check; send everything else.
    BeforeSend: func(e *bugstack.Event) *bugstack.Event {
        if e.Request != nil && e.Request.Route == "/health" {
            return nil
        }
        return e
    },
})

DryRun mode

Test your setup without sending anything:

bugstack.Init(bugstack.Config{
    APIKey:   os.Getenv("BUGSTACK_API_KEY"),
    Endpoint: "https://dashboard.bugstack.ai/api/ingest/sdk",
    DryRun:   true, // log each event to stderr instead of sending it
    Debug:    true,
})

Next Steps

Get started

Sign up with GitHub and start the 14-day trial, or book a demo and we set it up with you. Once the SDK is in, an uncaught server error starts a fix: specialist agents work out the cause and write a small change, it arrives as a pull request that runs through your CI, and what happens next depends on the autonomy level you set. Connecting the repository also starts repository scanning.

Get started Book a demo