chore(backend): use go-sentry middleware for backend

This commit is contained in:
Łukasz Mierzwa
2021-03-05 14:04:50 +00:00
committed by Łukasz Mierzwa
parent 119e762985
commit 891cc8a87c
8 changed files with 12 additions and 80 deletions
+9 -2
View File
@@ -19,13 +19,14 @@ import (
"syscall"
"time"
"github.com/getsentry/sentry-go"
"github.com/prymitive/karma/internal/alertmanager"
"github.com/prymitive/karma/internal/config"
"github.com/prymitive/karma/internal/models"
"github.com/prymitive/karma/internal/transform"
"github.com/prymitive/karma/internal/uri"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
@@ -84,8 +85,12 @@ func getViewURL(sub string) string {
func setupRouter(router *chi.Mux) {
_ = mime.AddExtensionType(".ico", "image/x-icon")
sentryMiddleware := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
router.Use(promMiddleware)
router.Use(sentryRecovery)
router.Use(sentryMiddleware.Handle)
router.Use(middleware.RealIP)
compressor := middleware.NewCompressor(flate.DefaultCompression)
@@ -383,6 +388,8 @@ func mainSetup(errorHandling pflag.ErrorHandling) (*chi.Mux, error) {
log.Error().Err(err).Str("dsn", config.Config.Sentry.Public).Msg("Sentry initialization failed")
return nil, fmt.Errorf("sentry configuration is invalid")
}
log.Info().Msg("Sentry enabled")
defer sentry.Flush(time.Second)
}
setupRouter(router)
-32
View File
@@ -1,32 +0,0 @@
package main
import (
"errors"
"fmt"
"net/http"
"runtime/debug"
"github.com/getsentry/raven-go"
"github.com/rs/zerolog/log"
)
// copied from https://github.com/loikg/ravenchi, adapted for chi v5
func sentryRecovery(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
log.Error().Err(fmt.Errorf("%+v", rval)).Bytes("stack", debug.Stack()).Msg("Panic")
rvalStr := fmt.Sprint(rval)
var packet *raven.Packet
if err, ok := rval.(error); ok {
packet = raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.GetOrNewStacktrace(err, 2, 3, nil)), raven.NewHttp(r))
} else {
packet = raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)), raven.NewHttp(r))
}
raven.Capture(packet, nil)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}()
handler.ServeHTTP(w, r)
})
}
-40
View File
@@ -1,40 +0,0 @@
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
)
func TestSentryRecovery(t *testing.T) {
r := chi.NewRouter()
r.Use(sentryRecovery)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
panic("catched")
})
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusInternalServerError {
t.Errorf("GET / returned status %d", resp.Code)
}
}
func TestSentryRecoveryWithError(t *testing.T) {
r := chi.NewRouter()
r.Use(sentryRecovery)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
panic(errors.New("catched error"))
})
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusInternalServerError {
t.Errorf("GET / returned status %d", resp.Code)
}
}
@@ -204,4 +204,5 @@ level=info msg=" collapseGroups: expanded"
level=info msg=" multiGridLabel: cluster"
level=info msg=" multiGridSortReverse: true"
level=info msg="Configured Alertmanager source" name=ro proxy=false readonly=true uri=http://127.0.0.1:9093
level=info msg="Sentry enabled"
level=info msg="Configuration is valid"
@@ -231,6 +231,7 @@ level=info msg="Configured Alertmanager source" name=ha1 proxy=true readonly=fal
level=info msg="Configured Alertmanager source" name=ha2 proxy=false readonly=true uri=http://127.0.0.1:9094
level=info msg="Configured Alertmanager source" name=local proxy=true readonly=false uri=http://foo:xxx@127.0.0.1:9095
level=info msg="Configured Alertmanager source" name=client-auth proxy=false readonly=false uri=https://127.0.0.1:9096
level=info msg="Sentry enabled"
level=info msg="Setting up proxy endpoints" alertmanager=ha1
level=info msg="Setting up proxy endpoints" alertmanager=local
level=info msg="Configuration is valid"
@@ -113,6 +113,7 @@ level=info msg=" collapseGroups: collapsedOnMobile"
level=info msg=" multiGridLabel: \"\""
level=info msg=" multiGridSortReverse: false"
level=info msg="Configured Alertmanager source" name=default proxy=false readonly=false uri=http://127.0.0.1:9093
level=info msg="Sentry enabled"
level=info msg="Writing PID file" path=karma.pid
level=info msg="Initial Alertmanager collection"
level=info msg="Pulling latest alerts and silences from Alertmanager"
-2
View File
@@ -4,11 +4,9 @@ go 1.16
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 // indirect
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08
github.com/elazarl/go-bindata-assetfs v1.0.1
github.com/fvbommel/sortorder v1.0.2
github.com/getsentry/raven-go v0.2.0
github.com/getsentry/sentry-go v0.10.0
github.com/go-chi/chi/v5 v5.0.0
github.com/go-chi/cors v1.1.1
-4
View File
@@ -52,8 +52,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 h1:uH66TXeswKn5PW5zdZ39xEwfS9an067BirqA+P4QaLI=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
@@ -105,8 +103,6 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo=
github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/getsentry/sentry-go v0.10.0 h1:6gwY+66NHKqyZrdi6O2jGdo7wGdo9b3B69E01NFgT5g=
github.com/getsentry/sentry-go v0.10.0/go.mod h1:kELm/9iCblqUYh+ZRML7PNdCvEuw24wBvJPYyi86cws=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=