Resolves #18. Log panics to the Sentry

This commit is contained in:
ErickSkrauch 2020-04-20 17:12:58 +03:00
parent d9fbfe658a
commit bca1436baf
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
2 changed files with 24 additions and 4 deletions

View File

@ -66,6 +66,8 @@ func newSentry(config *viper.Viper) (*raven.Client, error) {
ravenClient.SetDefaultLoggerName("sentry-watchdog-receiver") ravenClient.SetDefaultLoggerName("sentry-watchdog-receiver")
ravenClient.SetRelease(version.Version()) ravenClient.SetRelease(version.Version())
raven.DefaultClient = ravenClient
return ravenClient, nil return ravenClient, nil
} }

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/getsentry/raven-go"
"github.com/goava/di" "github.com/goava/di"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -29,11 +30,28 @@ func newAuthenticator(config *viper.Viper, emitter Emitter) (*JwtAuth, error) {
}, nil }, nil
} }
func newServer(config *viper.Viper, handler http.Handler) *http.Server { type serverParams struct {
config.SetDefault("server.host", "") di.Inject
config.SetDefault("server.port", 80)
address := fmt.Sprintf("%s:%d", config.GetString("server.host"), config.GetInt("server.port")) Config *viper.Viper `di:""`
Handler http.Handler `di:""`
Sentry *raven.Client `di:"" optional:"true"`
}
func newServer(params serverParams) *http.Server {
params.Config.SetDefault("server.host", "")
params.Config.SetDefault("server.port", 80)
handler := params.Handler
if params.Sentry != nil {
// raven.Recoverer uses DefaultClient and nothing can be done about it
// To avoid code duplication, if the Sentry service is successfully initiated,
// it will also replace DefaultClient, so raven.Recoverer will work with the instance
// created in the application constructor
handler = raven.Recoverer(handler)
}
address := fmt.Sprintf("%s:%d", params.Config.GetString("server.host"), params.Config.GetInt("server.port"))
server := &http.Server{ server := &http.Server{
Addr: address, Addr: address,
ReadTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second,