Connect net/http to Bugsink
When a Go HTTP handler panics, Bugsink gives you the error, the application stack that raised it, and the request that reached the handler.
This guide covers the initial setup and automatic panic capture for servers
built with Go’s standard net/http package. You’ll add the SDK middleware to
an existing server, then make one request fail to confirm that it reaches
Bugsink.
To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the SDK
Add the open-source Sentry SDK to your Go module. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your Bugsink project:
go get github.com/getsentry/sentry-gogo get github.com/getsentry/sentry-go
Initialize the SDK
Import the SDK and its net/http middleware alongside the standard packages
your server already uses:
import ( "encoding/json" "fmt" "log" "net/http" "os" "time" "github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go/http" )import ( "encoding/json" "fmt" "log" "net/http" "os" "time" "github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go/http" )
Initialize the SDK before starting the server. Reading the DSN from the
environment keeps it out of source control; Flush gives pending events a
chance to finish when the process shuts down normally:
err := sentry.Init(sentry.ClientOptions{ Dsn: os.Getenv("SENTRY_DSN"), }) if err != nil { log.Fatal(err) } defer sentry.Flush(5 * time.Second)err := sentry.Init(sentry.ClientOptions{ Dsn: os.Getenv("SENTRY_DSN"), }) if err != nil { log.Fatal(err) } defer sentry.Flush(5 * time.Second)
Set SENTRY_DSN in the environment where the server runs:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
Wrap the HTTP handler
Wrap your router or handler with sentryhttp. The middleware gives each
request its own SDK scope, adds the HTTP request to captured errors, and
reports panics raised while the handler is running:
sentryHandler := sentryhttp.New(sentryhttp.Options{ Repanic: true, }) address := ":" + getenv("PORT", "8080") log.Printf("Inventory application listening on %s", address) log.Fatal(http.ListenAndServe(address, recoverWithErrorResponse(sentryHandler.Handle(mux))))sentryHandler := sentryhttp.New(sentryhttp.Options{ Repanic: true, }) address := ":" + getenv("PORT", "8080") log.Printf("Inventory application listening on %s", address) log.Fatal(http.ListenAndServe(address, recoverWithErrorResponse(sentryHandler.Handle(mux))))
Repanic passes the panic on after it has been reported. Use it when an outer
recovery handler—recoverWithErrorResponse here—still needs to choose the HTTP
response. Leave it off if the SDK middleware should recover the panic itself.
Trigger a handler panic
There is no Bugsink call inside a handler. To verify the setup, this checkout handler panics with an error value when a product is missing:
func checkout(response http.ResponseWriter, request *http.Request) { sku := request.PathValue("sku") if _, exists := inventory[sku]; !exists { panic(ProductNotFound{SKU: sku}) } response.Header().Set("Content-Type", "application/json") fmt.Fprintf(response, `{"reserved":%q}`, sku) }func checkout(response http.ResponseWriter, request *http.Request) { sku := request.PathValue("sku") if _, exists := inventory[sku]; !exists { panic(ProductNotFound{SKU: sku}) } response.Header().Set("Content-Type", "application/json") fmt.Fprintf(response, `{"reserved":%q}`, sku) }
Using an error value matters: the SDK records it as an exception with the panic stack. A plain string panic becomes a fatal message without a stack by default.
Start the server as usual, then request a product that does not exist:
go run . :::bash curl http://127.0.0.1:8080/checkout/go-missinggo run . :::bash curl http://127.0.0.1:8080/checkout/go-missing
The application returns HTTP 500 and the middleware reports the panic.
See the error in Bugsink
Open your Bugsink project and select the new main.ProductNotFound issue. Its
event identifies the checkout frame and includes the GET request.
main.ProductNotFound
That’s it: panics from wrapped HTTP handlers will now be reported to Bugsink. Your handlers remain ordinary Go code, while the middleware adds the panic stack and request context.
For handled errors, messages, tags, structured context, and breadcrumbs, continue with the Go guide.
