Connect Gin to Bugsink
When a Gin handler panics, Bugsink shows you the error, the application frame that raised it, and the HTTP request that reached the route.
This guide covers the initial setup and Gin’s automatic capture of handler panics. You’ll add the SDK middleware to an existing router, 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’s Gin integration to your module. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your Bugsink project:
go get github.com/getsentry/sentry-go/gingo get github.com/getsentry/sentry-go/gin
Initialize the SDK
Import the base SDK and Gin integration where you create the router:
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go/gin" "github.com/gin-gonic/gin""github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go/gin" "github.com/gin-gonic/gin"
Initialize the SDK before starting Gin. Reading the DSN from the environment keeps it out of source control, while the deferred flush gives queued events a chance to finish during a normal shutdown:
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 service runs:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
Add the Gin middleware
Add Gin’s recovery middleware before the SDK middleware. The SDK records the
panic and request, then Repanic passes the panic outward so Gin’s recovery
middleware still produces the HTTP 500 response:
router := gin.New() router.Use(gin.Logger(), gin.Recovery()) router.Use(sentrygin.New(sentrygin.Options{ Repanic: true, }))router := gin.New() router.Use(gin.Logger(), gin.Recovery()) router.Use(sentrygin.New(sentrygin.Options{ Repanic: true, }))
Register your routes after the middleware so every matching request runs through it.
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 the requested product is missing:
func checkout(context *gin.Context) { sku := context.Param("sku") if _, exists := inventory[sku]; !exists { panic(ProductNotFound{SKU: sku}) } context.JSON(http.StatusOK, gin.H{"reserved": sku}) }func checkout(context *gin.Context) { sku := context.Param("sku") if _, exists := inventory[sku]; !exists { panic(ProductNotFound{SKU: sku}) } context.JSON(http.StatusOK, gin.H{"reserved": sku}) }
The error value gives the SDK an exception type and panic stack. A plain string panic becomes a fatal message without a stack by default.
Start Gin as usual, then request a product that does not exist:
go run . :::bash curl http://127.0.0.1:8080/checkout/gin-missinggo run . :::bash curl http://127.0.0.1:8080/checkout/gin-missing
Gin 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 failing checkout handler and includes the GET request.
main.ProductNotFound
That’s it: panics from Gin handlers will now be reported to Bugsink. The route stays ordinary application code while the middleware adds its panic stack and request context.
For handled errors, messages, tags, structured context, and breadcrumbs, continue with the Go guide.
