Report handler panics to Sentry when SENTRY_DSN is set (closes #95)
check / check (push) Successful in 2m36s
check / check (push) Successful in 2m36s
With SENTRY_DSN set, the server initialises sentry-go with the release netwatch-server-<version>, adds the sentryhttp middleware with Repanic as the last router-wide middleware, after the timeout, and flushes Sentry for 2 seconds on shutdown. A DSN Sentry refuses stops the start with an error naming SENTRY_DSN. With it empty, nothing is set up. The metrics middleware stays on the matched routes only, so it runs inside the Sentry middleware rather than before it. Model: opus-5-5
This commit is contained in:
@@ -7,8 +7,10 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/netwatch/internal/config"
|
||||
"sneak.berlin/go/netwatch/internal/globals"
|
||||
@@ -16,10 +18,15 @@ import (
|
||||
"sneak.berlin/go/netwatch/internal/logger"
|
||||
"sneak.berlin/go/netwatch/internal/middleware"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
// sentryFlushTimeout is how long shutdown waits for Sentry to send
|
||||
// what it still holds.
|
||||
const sentryFlushTimeout = 2 * time.Second
|
||||
|
||||
// Params defines the dependencies for Server.
|
||||
type Params struct {
|
||||
fx.In
|
||||
@@ -56,6 +63,11 @@ func New(
|
||||
s.log = params.Logger.Get()
|
||||
s.shutdowner = params.Shutdowner
|
||||
|
||||
err := s.enableSentry()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(_ context.Context) error {
|
||||
// Build the router and http.Server synchronously
|
||||
@@ -87,10 +99,37 @@ func (s *Server) ServeHTTP(
|
||||
s.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// enableSentry sets Sentry up when SENTRY_DSN is set, so that
|
||||
// SetupRoutes can report panics to it. With SENTRY_DSN empty it does
|
||||
// nothing. A DSN Sentry refuses stops the start.
|
||||
func (s *Server) enableSentry() error {
|
||||
if s.params.Config.SentryDSN == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := sentry.Init(sentry.ClientOptions{
|
||||
Dsn: s.params.Config.SentryDSN,
|
||||
Release: s.params.Globals.Appname + "-" + s.params.Globals.Version,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("SENTRY_DSN: %w", err)
|
||||
}
|
||||
|
||||
s.log.Info("sentry error reporting activated")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// shutdown gracefully stops the HTTP server within the
|
||||
// deadline of the context fx provides for OnStop.
|
||||
// deadline of the context fx provides for OnStop, then gives
|
||||
// Sentry, if set up, time to send what it still holds.
|
||||
func (s *Server) shutdown(ctx context.Context) error {
|
||||
err := s.httpServer.Shutdown(ctx)
|
||||
|
||||
if s.params.Config.SentryDSN != "" {
|
||||
sentry.Flush(sentryFlushTimeout)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
s.log.Error("server clean shutdown failed", "error", err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user