fix(sentry): report correct HTTP path in traces
Alan Pearce alan@alanpearce.eu
Sun, 19 Jan 2025 01:19:15 +0100
2 files changed, 35 insertions(+), 11 deletions(-)
A internal/sentryhttp/http.go
@@ -0,0 +1,29 @@+package sentryhttp + +import ( + "net/http" + + sentryhttp "github.com/getsentry/sentry-go/http" +) + +type ServeMux struct { + sentryHandler *sentryhttp.Handler + *http.ServeMux +} + +func NewServeMux() *ServeMux { + return &ServeMux{ + sentryHandler: sentryhttp.New(sentryhttp.Options{ + Repanic: true, + }), + ServeMux: http.NewServeMux(), + } +} + +func (sm *ServeMux) Handle(pattern string, handler http.Handler) { + sm.ServeMux.Handle(pattern, sm.sentryHandler.Handle(handler)) +} + +func (sm *ServeMux) HandleFunc(pattern string, handler http.HandlerFunc) { + sm.ServeMux.HandleFunc(pattern, sm.sentryHandler.HandleFunc(handler)) +}
M internal/server/mux.go → internal/server/mux.go
@@ -17,9 +17,9 @@ "go.alanpearce.eu/searchix/internal/config" search "go.alanpearce.eu/searchix/internal/index" "go.alanpearce.eu/searchix/internal/opensearch" "go.alanpearce.eu/searchix/internal/pagination" + "go.alanpearce.eu/searchix/internal/sentryhttp" "go.alanpearce.eu/x/log" - sentryhttp "github.com/getsentry/sentry-go/http" "github.com/osdevisnot/sorvor/pkg/livereload" "github.com/pkg/errors" ) @@ -65,15 +65,12 @@ } if index == nil { return nil, errors.New("index is nil") } - sentryHandler := sentryhttp.New(sentryhttp.Options{ - Repanic: true, - }) sortSources(cfg.Importer.Sources) errorHandler := createErrorHandler(cfg, log) top := http.NewServeMux() - mux := http.NewServeMux() + mux := sentryhttp.NewServeMux() mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) { indexData := components.TemplateData{ ExtraHeadHTML: cfg.Web.ExtraHeadHTML, @@ -389,12 +386,10 @@ } top.Handle("/", AddHeadersMiddleware( - sentryHandler.Handle( - wrapHandlerWithLogging(mux, wrappedHandlerOptions{ - defaultHostname: cfg.Web.BaseURL.Hostname(), - logger: log, - }), - ), + wrapHandlerWithLogging(mux, wrappedHandlerOptions{ + defaultHostname: cfg.Web.BaseURL.Hostname(), + logger: log, + }), cfg, ), )