Go
Install
go get github.com/getsentry/sentry-go
Initialize
package main
import (
"log"
"time"
"github.com/getsentry/sentry-go"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://<public_key>@<your-errora-host>/<project_id>",
Environment: "production",
Release: "myapp@1.4.2",
EnableTracing: true,
TracesSampleRate: 0.2,
ProfilesSampleRate: 1.0,
AttachStacktrace: true,
SendDefaultPII: false,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
run()
}
Flush matters in Go: the transport is asynchronous, so a process that exits immediately
after a panic can drop the event.
net/http
sentryHandler := sentryhttp.New(sentryhttp.Options{Repanic: true})
http.Handle("/", sentryHandler.Handle(mux))
Gin: sentrygin.New(...); Echo: sentryecho.New(...); Fiber: sentryfiber.New(...).
Capturing manually
if err != nil {
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetTag("area", "ledger")
scope.SetContext("order", map[string]any{"id": order.ID})
sentry.CaptureException(err)
})
}
defer func() {
if r := recover(); r != nil {
sentry.CurrentHub().Recover(r)
sentry.Flush(2 * time.Second)
panic(r)
}
}()
Tracing spans
ctx := context.Background()
span := sentry.StartSpan(ctx, "task", sentry.WithTransactionName("nightly-settlement"))
defer span.Finish()
child := sentry.StartSpan(span.Context(), "db.query")
// … work …
child.Finish()
Cron check-ins
checkinID := sentry.CaptureCheckIn(&sentry.CheckIn{
MonitorSlug: "nightly-settlement",
Status: sentry.CheckInStatusInProgress,
}, &sentry.MonitorConfig{
Schedule: sentry.CrontabSchedule("0 3 * * *"),
CheckInMargin: 5,
MaxRuntime: 30,
Timezone: "Asia/Tehran",
})
// … run the job …
sentry.CaptureCheckIn(&sentry.CheckIn{
ID: *checkinID,
MonitorSlug: "nightly-settlement",
Status: sentry.CheckInStatusOK,
}, nil)
See Cron monitoring.
Verify
sentry.CaptureException(errors.New("Errora smoke test"))
sentry.Flush(2 * time.Second)