From 8dfa2e6c7d2d5019c35070660b2960221e67a4dc Mon Sep 17 00:00:00 2001 From: FZambia Date: Sun, 22 Dec 2024 22:04:50 +0200 Subject: [PATCH] refactor fatals --- internal/app/grpc.go | 4 ++-- internal/app/mux.go | 22 +++++++++++----------- internal/app/origin.go | 2 +- internal/app/run.go | 37 ++++++++++++++++++------------------- internal/app/tls.go | 2 +- internal/logging/logging.go | 6 ++++-- internal/usage/usage.go | 3 +-- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/internal/app/grpc.go b/internal/app/grpc.go index dae74f09ba..1c956a9b25 100644 --- a/internal/app/grpc.go +++ b/internal/app/grpc.go @@ -63,7 +63,7 @@ func runGRPCAPIServer(cfg config.Config, node *centrifuge.Node, useAPIOpenteleme log.Info().Msgf("serving GRPC API service on %s", grpcAPIAddr) go func() { if err := grpcAPIServer.Serve(grpcAPIConn); err != nil { - log.Fatal().Msgf("serve GRPC API: %v", err) + log.Fatal().Err(err).Msg("serve GRPC API") } }() return grpcAPIServer, nil @@ -106,7 +106,7 @@ func runGRPCUniServer(cfg config.Config, node *centrifuge.Node) (*grpc.Server, e log.Info().Msgf("serving unidirectional GRPC on %s", grpcUniAddr) go func() { if err := grpcUniServer.Serve(grpcUniConn); err != nil { - log.Fatal().Msgf("serve uni GRPC: %v", err) + log.Fatal().Err(err).Msg("serve uni GRPC") } }() return grpcUniServer, nil diff --git a/internal/app/mux.go b/internal/app/mux.go index 5e33ac1867..d3c0fc8635 100644 --- a/internal/app/mux.go +++ b/internal/app/mux.go @@ -218,7 +218,7 @@ func Mux( // register HTTP API endpoints. httpErrorMode, err := tools.OptionalStringChoice(cfg.HttpAPI.ErrorMode, []string{config.TransportErrorMode}) if err != nil { - log.Fatal().Msgf("error in config: %v", err) + log.Fatal().Err(err).Msg("error in config") } useOpenTelemetry := cfg.OpenTelemetry.Enabled && cfg.OpenTelemetry.API apiHandler := api.NewHandler(n, apiExecutor, api.Config{ @@ -441,13 +441,13 @@ func runHTTPServers( tlsConfig, err := GetTLSConfig(cfg) if err != nil { - log.Fatal().Msgf("can not get TLS config: %v", err) + log.Fatal().Err(err).Msg("can not get TLS config") } var internalTLSConfig *tls.Config if cfg.InternalTLS.Enabled { internalTLSConfig, err = cfg.InternalTLS.ToGoTLSConfig("internal_tls") if err != nil { - log.Fatal().Msgf("can not get internal TLS config: %v", err) + log.Fatal().Err(err).Msg("can not get internal TLS config") } } @@ -518,21 +518,21 @@ func runHTTPServers( udpAddr, err := net.ResolveUDPAddr("udp", addr) if err != nil { - log.Fatal().Msgf("can not start HTTP/3, resolve UDP: %v", err) + log.Fatal().Err(err).Msg("can not start HTTP/3, resolve UDP") } udpConn, err := net.ListenUDP("udp", udpAddr) if err != nil { - log.Fatal().Msgf("can not start HTTP/3, listen UDP: %v", err) + log.Fatal().Err(err).Msg("can not start HTTP/3, listen UDP") } defer func() { _ = udpConn.Close() }() tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { - log.Fatal().Msgf("can not start HTTP/3, resolve TCP: %v", err) + log.Fatal().Err(err).Msg("can not start HTTP/3, resolve TCP") } tcpConn, err := net.ListenTCP("tcp", tcpAddr) if err != nil { - log.Fatal().Msgf("can not start HTTP/3, listen TCP: %v", err) + log.Fatal().Err(err).Msg("can not start HTTP/3, listen TCP") } defer func() { _ = tcpConn.Close() }() @@ -552,23 +552,23 @@ func runHTTPServers( case err := <-hErr: _ = wtServer.Close() if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Msgf("ListenAndServe: %v", err) + log.Fatal().Err(err).Msg("error ListenAndServe") } case err := <-qErr: // Cannot close the HTTP server or wait for requests to complete properly. - log.Fatal().Msgf("ListenAndServe HTTP/3: %v", err) + log.Fatal().Err(err).Msg("error ListenAndServe HTTP/3") } } else { if addrTLSConfig != nil { if err := server.ListenAndServeTLS("", ""); err != nil { if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Msgf("ListenAndServe: %v", err) + log.Fatal().Err(err).Msg("error ListenAndServeTLS") } } } else { if err := server.ListenAndServe(); err != nil { if !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Msgf("ListenAndServe: %v", err) + log.Fatal().Err(err).Msgf("error ListenAndServe") } } } diff --git a/internal/app/origin.go b/internal/app/origin.go index 41531890ed..e5b12ad5dd 100644 --- a/internal/app/origin.go +++ b/internal/app/origin.go @@ -27,7 +27,7 @@ func getCheckOrigin(cfg config.Config) func(r *http.Request) bool { } originChecker, err := origin.NewPatternChecker(allowedOrigins) if err != nil { - log.Fatal().Msgf("error creating origin checker: %v", err) + log.Fatal().Err(err).Msg("error creating origin checker") } if len(allowedOrigins) == 1 && allowedOrigins[0] == "*" { // Fast path for *. diff --git a/internal/app/run.go b/internal/app/run.go index ce9d990784..85929f99c1 100644 --- a/internal/app/run.go +++ b/internal/app/run.go @@ -41,7 +41,7 @@ const edition = "oss" func Run(cmd *cobra.Command, configFile string) { cfg, cfgMeta, err := config.GetConfig(cmd, configFile) if err != nil { - log.Fatal().Msgf("error getting config: %v", err) + log.Fatal().Err(err).Msg("error getting config") } ctx, serviceCancel := context.WithCancel(context.Background()) @@ -59,7 +59,7 @@ func Run(cmd *cobra.Command, configFile string) { } err = tools.WritePidFile(cfg.PidFile) if err != nil { - log.Fatal().Msgf("error writing PID: %v", err) + log.Fatal().Err(err).Msg("error writing PID") } _, _ = maxprocs.Set(maxprocs.Logger(func(s string, i ...interface{}) { log.Info().Msgf(strings.ToLower(s), i...) @@ -88,46 +88,45 @@ func Run(cmd *cobra.Command, configFile string) { err = cfg.Validate() if err != nil { - log.Fatal().Msgf("error validating config: %v", err) + log.Fatal().Err(err).Msg("error validating config") } cfgContainer, err := config.NewContainer(cfg) if err != nil { - log.Fatal().Msgf("error creating config: %v", err) + log.Fatal().Err(err).Msg("error creating config") } cfgContainer.ChannelOptionsCacheTTL = 200 * time.Millisecond proxyMap, keepHeadersInContext, err := buildProxyMap(cfg) if err != nil { - log.Fatal().Msgf("error building proxy map: %v", err) + log.Fatal().Err(err).Msg("error building proxy map") } nodeCfg := centrifugeNodeConfig(build.Version, cfgContainer, centrifugeLogHandler) - node, err := centrifuge.New(nodeCfg) if err != nil { - log.Fatal().Msgf("error creating Centrifuge Node: %v", err) + log.Fatal().Err(err).Msg("error creating Centrifuge Node") } if cfg.OpenTelemetry.Enabled { _, err := telemetry.SetupTracing(context.Background()) if err != nil { - log.Fatal().Msgf("error setting up opentelemetry tracing: %v", err) + log.Fatal().Err(err).Msg("error setting up opentelemetry tracing") } } modes, err := configureEngines(node, cfgContainer) if err != nil { - log.Fatal().Msgf("%v", err) + log.Fatal().Err(err).Msg("configure engines error") } verifierConfig, err := confighelpers.MakeVerifierConfig(cfg.Client.Token) if err != nil { - log.Fatal().Msgf("error creating JWT verifier config: %v", err) + log.Fatal().Err(err).Msg("error creating JWT verifier config") } tokenVerifier, err := jwtverify.NewTokenVerifierJWT(verifierConfig, cfgContainer) if err != nil { - log.Fatal().Msgf("error creating token verifier: %v", err) + log.Fatal().Err(err).Msg("error creating token verifier") } var subTokenVerifier *jwtverify.VerifierJWT @@ -135,18 +134,18 @@ func Run(cmd *cobra.Command, configFile string) { log.Info().Msg("initializing separate verifier for subscription tokens") subVerifier, err := confighelpers.MakeVerifierConfig(cfg.Client.SubscriptionToken.Token) if err != nil { - log.Fatal().Msgf("error creating subscription JWT verifier config: %v", err) + log.Fatal().Err(err).Msg("error creating subscription JWT verifier config") } subTokenVerifier, err = jwtverify.NewTokenVerifierJWT(subVerifier, cfgContainer) if err != nil { - log.Fatal().Msgf("error creating token verifier: %v", err) + log.Fatal().Err(err).Msg("error creating token verifier") } } clientHandler := client.NewHandler(node, cfgContainer, tokenVerifier, subTokenVerifier, proxyMap) err = clientHandler.Setup() if err != nil { - log.Fatal().Msgf("error setting up client handler: %v", err) + log.Fatal().Err(err).Msg("error setting up client handler") } if cfg.RPC.Ping { log.Info().Str("method", cfg.RPC.PingMethod).Msg("RPC ping extension enabled") @@ -179,7 +178,7 @@ func Run(cmd *cobra.Command, configFile string) { consumingServices, err := consuming.New(node.ID(), consumingHandler, cfg.Consumers) if err != nil { - log.Fatal().Msgf("error initializing consumers: %v", err) + log.Fatal().Err(err).Msg("error initializing consumers") } serviceManager.Register(consumingServices...) @@ -236,7 +235,7 @@ func Run(cmd *cobra.Command, configFile string) { notify.RegisterHandlers(node, statsSender) if err = node.Run(); err != nil { - log.Fatal().Msgf("error running node: %v", err) + log.Fatal().Err(err).Msg("error running node") } serviceManager.Run(ctx) @@ -246,7 +245,7 @@ func Run(cmd *cobra.Command, configFile string) { var err error grpcAPIServer, err = runGRPCAPIServer(cfg, node, useAPIOpentelemetry, grpcAPIExecutor) if err != nil { - log.Fatal().Msgf("error creating GRPC API server: %v", err) + log.Fatal().Err(err).Msg("error creating GRPC API server") } } @@ -255,13 +254,13 @@ func Run(cmd *cobra.Command, configFile string) { var err error grpcAPIServer, err = runGRPCUniServer(cfg, node) if err != nil { - log.Fatal().Msgf("error creating GRPC API server: %v", err) + log.Fatal().Err(err).Msg("error creating GRPC API server") } } httpServers, err := runHTTPServers(node, cfgContainer, httpAPIExecutor, keepHeadersInContext) if err != nil { - log.Fatal().Msgf("error running HTTP server: %v", err) + log.Fatal().Err(err).Msg("error running HTTP server") } logStartWarnings(cfg, cfgMeta) diff --git a/internal/app/tls.go b/internal/app/tls.go index 27a16e1915..703353b645 100644 --- a/internal/app/tls.go +++ b/internal/app/tls.go @@ -48,7 +48,7 @@ func GetTLSConfig(cfg config.Config) (*tls.Config, error) { go func() { log.Info().Msgf("serving ACME http_01 challenge on %s", tlsAutocertHTTPAddr) if err := acmeHTTPServer.ListenAndServe(); err != nil { - log.Fatal().Msgf("can't create server on %s to serve acme http challenge: %v", tlsAutocertHTTPAddr, err) + log.Fatal().Err(err).Msgf("can't create server on %s to serve acme http challenge", tlsAutocertHTTPAddr) } }() }) diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 2ac1cb8ad1..5ebc9dc161 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -14,6 +14,8 @@ import ( "github.com/rs/zerolog/log" ) +type Level = zerolog.Level + const ( TraceLevel = zerolog.TraceLevel DebugLevel = zerolog.DebugLevel @@ -119,7 +121,7 @@ func Setup(cfg config.Config) (centrifuge.LogHandler, func()) { if cfg.LogFile != "" { f, err := os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { - log.Fatal().Msgf("error opening log file: %v", err) + log.Fatal().Err(err).Msg("error opening log file") } log.Logger = log.Output(f) return newCentrifugeLogHandler().Handle, func() { @@ -130,6 +132,6 @@ func Setup(cfg config.Config) (centrifuge.LogHandler, func()) { } // Enabled checks if a specific logging level is enabled -func Enabled(level zerolog.Level) bool { +func Enabled(level Level) bool { return level >= zerolog.GlobalLevel() } diff --git a/internal/usage/usage.go b/internal/usage/usage.go index 18a759c36f..6374f1a5b0 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -16,14 +16,13 @@ import ( "sync" "time" - "github.com/rs/zerolog/log" - "github.com/centrifugal/centrifugo/v5/internal/build" "github.com/centrifugal/centrifugo/v5/internal/config" "github.com/centrifugal/centrifugo/v5/internal/configtypes" "github.com/centrifugal/centrifugo/v5/internal/consuming" "github.com/centrifugal/centrifuge" + "github.com/rs/zerolog/log" ) var statsRand *rand.Rand