Product How it works Features Security What is bugstack? SDKs JavaScript SDK Python SDK Go SDK Ruby SDK Docs Pricing Blog Book a demo
Log In Get started

Go SDK reference

Reference for github.com/MasonBachmann7/bugstack-go: setup for net/http, Gin, Echo and Fiber, panic recovery and manual capture, what it reports and troubleshooting.

Installation

$ go get github.com/MasonBachmann7/bugstack-go

net/http

Initialize in main(), defer Flush(), and wrap your handler with middleware.NetHTTP. It reports a panic in a handler, then re-panics so net/http handles it as usual. The import path is github.com/MasonBachmann7/bugstack-go; the repository now lives at github.com/Bugstackai/Bugstack-Go.

go
// 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))

Errors and goroutines

go
// Report an error you handle yourself:
if err := chargeCard(order); err != nil {
    bugstack.CaptureError(err)
    return err
}

// Report (and recover) a panic in a goroutine you start:
go func() {
    defer bugstack.Recover()
    processJob(job)
}()

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.)

go
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()
})
go
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)
    }
})
go
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()
})

What the SDK reports

A panic in a handler wrapped with middleware.NetHTTP or the Gin, Echo or Fiber middleware above, a panic in a function that runs defer bugstack.Recover(), and anything you pass to bugstack.CaptureError or bugstack.CaptureMessage. Errors your code returns or logs are not reported on their own, and a Go fatal error (a deadlock, a concurrent map write) is not a panic and cannot be reported.

Each report carries the message, stack trace, file and function, the route and method when it came through the middleware, the Go version and SDK version, and any metadata you pass. Call bugstack.Flush() before the program exits so queued reports are sent.

Capture API

  • bugstack.CaptureError(err, opts...): report an error. Returns true if it was accepted.
  • bugstack.CaptureMessage(msg, opts...): report a message.
  • bugstack.Recover(opts...): in a defer, report a panic and recover from it.
  • bugstack.WithRequest(&bugstack.RequestContext{...}) and bugstack.WithMetadata(map[string]any{...}): options for the calls above.
  • bugstack.Flush(): send what is queued and stop the client.

Configuration

Every field of bugstack.Config, with its type and default, is in the installation guide. The ones you are most likely to set: APIKey, Endpoint, Environment, Enabled (a *bool: use bugstack.Bool(false)), IgnoredErrors and BeforeSend.

After an error is reported

Specialist agents work out the cause and write the change. It arrives as a pull request your own CI runs on. What happens next depends on the autonomy level you set for the repository: new repositories start on Review, where you merge. Before Autopilot merges a crash fix, bugstack replays the failing request: it must fail before the fix and pass after; without that proof the pull request waits for you. Connecting the repository also starts repository scanning.

Common Go errors

Error patterns that come up often in production Go services. Each link opens a step-by-step fix guide:

Troubleshooting

No events in bugstack? Check that the API key is set when the process starts, and that endpoint is set to https://dashboard.bugstack.ai/api/ingest/sdk: the published SDK does not default to it yet. Turn on debug to see each send.

Events but no fix pull request? The bugstack GitHub App must be installed on the repository, and the files in the stack trace must be in that repository.

Too many duplicates? Each SDK sends the same error at most once every five minutes, and bugstack folds repeats within 24 hours into the same piece of work, so you get one pull request per problem.

Reports lost at exit? defer bugstack.Flush() in main() so queued reports are sent before the program ends.

Data and privacy

The SDK sends the error or panic message, its stack trace, the file and function, the route and method, and runtime details. It sends query parameters only if you pass them in a RequestContext, and it does not redact the message or stack trace: use BeforeSend to change or drop an event before it is sent. See privacy and data handling.

Versioning and compatibility

The SDK follows semver. It needs Go 1.21 or later and uses only the standard library.

Get started

Sign up with GitHub and start Pro, or book a demo and we set it up with you. Get started.

← Back to Documentation