Que fait sentry-go-sdk ?
Configuration complète du SDK Sentry pour Go. À utiliser lorsque l'utilisateur demande "ajouter Sentry à Go", "installer sentry-go", "configurer Sentry dans Go", ou configurer la surveillance des erreurs, le tracing, la journalisation, les métriques ou les crons pour des applications Go. Prend en charge net/http, Gin, Echo, Fiber, FastHTTP, Iris, Negroni et gRPC.
Installation
go get github.com/getsentry/sentry-go
Configuration de base
Initialisez le client Sentry au démarrage de votre application (dans main.go) :
package main
import (
"log"
"github.com/getsentry/sentry-go"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
// Définir le taux d'échantillonnage de tracing, entre 0.0 et 1.0
TracesSampleRate: 1.0,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// Utilisez defer pour vider les événements avant l'arrêt de l'application
defer sentry.Flush(2 * time.Second)
}
Surveillance des erreurs
Signalez manuellement les erreurs :
import "github.com/getsentry/sentry-go"
func maFonction() {
err := operationRisquee()
if err != nil {
sentry.CaptureException(err)
}
}
Intégration avec net/http
Utilisez le middleware sentryhttp :
import (
"net/http"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
)
func main() {
sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
handler := sentryhttp.New(sentryhttp.Options{})
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Votre logique
})
// Utilisez le middleware
http.Handle("/", handler.Handle(mux))
http.ListenAndServe(":8080", nil)
}
Intégration avec Gin
import (
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
app := gin.Default()
app.Use(sentrygin.New(sentrygin.Options{}))
app.GET("/", func(c *gin.Context) {
// Votre logique
})
app.Run()
}
Intégration avec Echo
import (
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
e := echo.New()
e.Use(sentryecho.New(sentryecho.Options{}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Bonjour, Monde!")
})
e.Logger.Fatal(e.Start(":1323"))
}
Intégration avec Fiber
import (
"github.com/gofiber/fiber/v2"
"github.com/getsentry/sentry-go"
sentryfiber "github.com/getsentry/sentry-go/fiber"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
app := fiber.New()
app.Use(sentryfiber.New(sentryfiber.Options{}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Bonjour, Monde!")
})
app.Listen(":3000")
}
Intégration avec FastHTTP
import (
"github.com/getsentry/sentry-go"
sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
"github.com/valyala/fasthttp"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
handler := sentryfasthttp.New(sentryfasthttp.Options{})
fastHandler := func(ctx *fasthttp.RequestCtx) {
// Votre logique
}
fasthttp.ListenAndServe(":8080", handler.Handler(fastHandler))
}
Intégration avec Iris
import (
"github.com/kataras/iris/v12"
"github.com/getsentry/sentry-go"
sentryiris "github.com/getsentry/sentry-go/iris"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
app := iris.New()
app.Use(sentryiris.New(sentryiris.Options{}))
app.Get("/", func(ctx iris.Context) {
ctx.WriteString("Bonjour, Iris!")
})
app.Listen(":8080")
}
Intégration avec Negroni
import (
"github.com/getsentry/sentry-go"
sentrynegroni "github.com/getsentry/sentry-go/negroni"
"github.com/urfave/negroni"
"net/http"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
n := negroni.Classic()
n.Use(negroni.HandlerFunc(sentrynegroni.Handler))
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Bonjour, Monde!"))
})
n.UseHandler(mux)
n.Run(":8080")
}
Intégration avec gRPC
import (
"github.com/getsentry/sentry-go"
sentrygrpc "github.com/getsentry/sentry-go/grpc"
"google.golang.org/grpc"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
server := grpc.NewServer(
grpc.UnaryInterceptor(sentrygrpc.UnaryServerInterceptor()),
grpc.StreamInterceptor(sentrygrpc.StreamServerInterceptor()),
)
// Enregistrez vos services gRPC ici
}
Tracing
Activez le tracing en définissant TracesSampleRate dans l'initialisation :
sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
TracesSampleRate: 0.25, // 25% des transactions sont tracées
})
Journalisation
Utilisez les niveaux de journalisation pour les événements :
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelWarning)
sentry.CaptureException(err)
})
Métriques
Envoyez des métriques personnalisées :
sentry.CaptureMessage("métrique personnalisée", func(hint *sentry.EventHint) {
hint.Event.Tags = map[string]string{"type": "exemple"}
})
Crons
Surveillez les tâches cron avec Sentry Cron Monitoring :
func main() {
// Initialisez Sentry
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/0",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// Créez un monitor de cron
monitor := sentry.NewCheckIn(
&sentry.CheckIn{
ID: "mon-cron",
Status: sentry.CheckInStatusInProgress,
MonitorSlug: "mon-slug-cron",
},
)
defer monitor.Finish()
// Votre logique cron ici
}
Variables d'environnement
SENTRY_DSN: Définit le DSN pour l'initialisation automatique.SENTRY_ENVIRONMENT: Définit l'environnement (par exemple, production, staging).SENTRY_RELEASE: Définit la version de la release.SENTRY_TRACES_SAMPLE_RATE: Définit le taux d'échantillonnage de tracing.
Documentation supplémentaire
Consultez la documentation officielle de Sentry pour Go pour plus d'informations sur les fonctionnalités avancées, y compris les profils de performance et les alertes personnalisées.