Report handler panics to Sentry when SENTRY_DSN is set (closes #95)
check / check (push) Successful in 3m6s
check / check (push) Successful in 3m6s
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 was merged in pull request #107.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/netwatch/internal/config"
|
||||
"sneak.berlin/go/netwatch/internal/globals"
|
||||
@@ -15,6 +17,7 @@ import (
|
||||
"sneak.berlin/go/netwatch/internal/reportbuf"
|
||||
"sneak.berlin/go/netwatch/internal/server"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/fx/fxtest"
|
||||
)
|
||||
@@ -196,6 +199,70 @@ func TestMetricsInTwoServers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSentry: with SENTRY_DSN empty there is no Sentry client. With it
|
||||
// pointing at a local server standing in for Sentry, a panic in a
|
||||
// handler reaches that server, and the request still gets the 500 from
|
||||
// the panic recovery.
|
||||
func TestSentry(t *testing.T) {
|
||||
const panicMessage = "handler panic for TestSentry"
|
||||
|
||||
// sentry.Init sets the client for the whole process; take it away
|
||||
// again so that no other test reports to Sentry.
|
||||
t.Cleanup(func() { sentry.CurrentHub().BindClient(nil) })
|
||||
|
||||
t.Setenv("SENTRY_DSN", "")
|
||||
newServer(t)
|
||||
|
||||
if sentry.CurrentHub().Client() != nil {
|
||||
t.Fatal("a Sentry client exists with SENTRY_DSN empty")
|
||||
}
|
||||
|
||||
// The body of the first request the stand-in for Sentry receives.
|
||||
received := make(chan string, 1)
|
||||
|
||||
sentryServer := httptest.NewServer(http.HandlerFunc(
|
||||
func(_ http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
|
||||
select {
|
||||
case received <- string(body):
|
||||
default:
|
||||
}
|
||||
},
|
||||
))
|
||||
defer sentryServer.Close()
|
||||
|
||||
t.Setenv("SENTRY_DSN",
|
||||
"http://key@"+sentryServer.Listener.Addr().String()+"/1")
|
||||
|
||||
srv := newServer(t)
|
||||
srv.SetupRoutes()
|
||||
srv.Router().Get("/panic", func(http.ResponseWriter, *http.Request) {
|
||||
panic(panicMessage)
|
||||
})
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequestWithContext(t.Context(),
|
||||
http.MethodGet, "/panic", http.NoBody)
|
||||
srv.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want %d",
|
||||
rec.Code, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Sentry sends from a goroutine of its own.
|
||||
select {
|
||||
case body := <-received:
|
||||
if !strings.Contains(body, panicMessage) {
|
||||
t.Fatalf("the Sentry server received no report of the panic:\n%s",
|
||||
body)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("nothing reached the Sentry server")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHealthCheckRejectsOversizeBody sends the health check, which
|
||||
// never reads its body, a body one byte over the limit. Only the
|
||||
// router-wide body limit can reject it.
|
||||
|
||||
Reference in New Issue
Block a user