From fc93f6754d61a9a1fb969859058f76126b83129f Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 7 Oct 2020 20:21:22 +0300 Subject: [PATCH 1/4] Add Discord start/stop logs --- cmd/dmsg-discovery/commands/root.go | 5 +++++ cmd/dmsg-server/commands/root.go | 5 +++++ discord/hook.go | 21 +++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cmd/dmsg-discovery/commands/root.go b/cmd/dmsg-discovery/commands/root.go index 1f0571c0e..9651afcbe 100644 --- a/cmd/dmsg-discovery/commands/root.go +++ b/cmd/dmsg-discovery/commands/root.go @@ -13,6 +13,7 @@ import ( "github.com/skycoin/dmsg/cmd/dmsg-discovery/internal/api" "github.com/skycoin/dmsg/cmd/dmsg-discovery/internal/store" "github.com/skycoin/dmsg/cmdutil" + "github.com/skycoin/dmsg/discord" ) const redisPasswordEnvName = "REDIS_PASSWORD" @@ -44,6 +45,10 @@ var rootCmd = &cobra.Command{ log := sf.Logger() + // Workaround for Discord logger hook. Actually, it's Info. + log.Error(discord.StartLogMessage) + defer log.Error(discord.StopLogMessage) + m := sf.HTTPMetrics() db := prepareDB(log) diff --git a/cmd/dmsg-server/commands/root.go b/cmd/dmsg-server/commands/root.go index 3f9afc37a..a90bf794b 100644 --- a/cmd/dmsg-server/commands/root.go +++ b/cmd/dmsg-server/commands/root.go @@ -15,6 +15,7 @@ import ( "github.com/skycoin/dmsg/cipher" "github.com/skycoin/dmsg/cmdutil" "github.com/skycoin/dmsg/disc" + "github.com/skycoin/dmsg/discord" "github.com/skycoin/dmsg/promutil" "github.com/skycoin/dmsg/servermetrics" ) @@ -32,6 +33,10 @@ var rootCmd = &cobra.Command{ Run: func(_ *cobra.Command, args []string) { log := sf.Logger() + // Workaround for Discord logger hook. Actually, it's Info. + log.Error(discord.StartLogMessage) + defer log.Error(discord.StopLogMessage) + if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil { log.WithError(err).Warn("Failed to output build info.") } diff --git a/discord/hook.go b/discord/hook.go index b4639ae73..edeb0591e 100644 --- a/discord/hook.go +++ b/discord/hook.go @@ -8,8 +8,16 @@ import ( "github.com/sirupsen/logrus" ) +const webhookURLEnvName = "DISCORD_WEBHOOK_URL" + +const ( + loggedLevel = logrus.ErrorLevel + startStopLogLevel = logrus.InfoLevel +) + const ( - webhookURLEnvName = "DISCORD_WEBHOOK_URL" + StartLogMessage = "Starting" + StopLogMessage = "Stopping" ) // Hook is a Discord logger hook. @@ -32,7 +40,7 @@ func WithLimit(limit time.Duration) Option { // NewHook returns a new Hook. func NewHook(tag, webHookURL string, opts ...Option) logrus.Hook { - parent := discordrus.NewHook(webHookURL, logrus.ErrorLevel, discordOpts(tag)) + parent := discordrus.NewHook(webHookURL, loggedLevel, discordOpts(tag)) hook := &Hook{ Hook: parent, @@ -47,6 +55,15 @@ func NewHook(tag, webHookURL string, opts ...Option) logrus.Hook { // Fire checks whether rate is fine and fires the underlying hook. func (h *Hook) Fire(entry *logrus.Entry) error { + switch entry.Message { + case StartLogMessage, StopLogMessage: + // Start and stop messages should be logged by Hook but they should have Info level. + // With Info level, they would not be passed to hook. + // So we can use Error level in the codebase and change level to Info in the hook, + // then it appears as Info in logs. + entry.Level = startStopLogLevel + } + if h.shouldFire(entry) { return h.Hook.Fire(entry) } From 1b53d76567d666fdd867bfed3b2d153228de7f81 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 7 Oct 2020 20:27:30 +0300 Subject: [PATCH 2/4] Fix linter --- discord/hook.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/discord/hook.go b/discord/hook.go index edeb0591e..61a9f2988 100644 --- a/discord/hook.go +++ b/discord/hook.go @@ -16,8 +16,10 @@ const ( ) const ( + // StartLogMessage defines a message on binary starting. StartLogMessage = "Starting" - StopLogMessage = "Stopping" + // StopLogMessage defines a message on binary stopping. + StopLogMessage = "Stopping" ) // Hook is a Discord logger hook. From 5bbae70997b87d7c9cc5253ac3da4b4eb345b75e Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 7 Oct 2020 22:07:51 +0300 Subject: [PATCH 3/4] Info level for start/stop messages if Discord hook is not added --- cmd/dmsg-discovery/commands/root.go | 12 +++++++++--- cmd/dmsg-server/commands/root.go | 17 +++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/dmsg-discovery/commands/root.go b/cmd/dmsg-discovery/commands/root.go index 9651afcbe..71412ca02 100644 --- a/cmd/dmsg-discovery/commands/root.go +++ b/cmd/dmsg-discovery/commands/root.go @@ -1,6 +1,7 @@ package commands import ( + "context" "log" "net/http" "os" @@ -45,9 +46,14 @@ var rootCmd = &cobra.Command{ log := sf.Logger() - // Workaround for Discord logger hook. Actually, it's Info. - log.Error(discord.StartLogMessage) - defer log.Error(discord.StopLogMessage) + if discordWebhookURL := discord.GetWebhookURLFromEnv(); discordWebhookURL != "" { + // Workaround for Discord logger hook. Actually, it's Info. + log.Error(discord.StartLogMessage) + defer log.Error(discord.StopLogMessage) + } else { + log.Info(discord.StartLogMessage) + defer log.Info(discord.StopLogMessage) + } m := sf.HTTPMetrics() diff --git a/cmd/dmsg-server/commands/root.go b/cmd/dmsg-server/commands/root.go index a90bf794b..fdbd87515 100644 --- a/cmd/dmsg-server/commands/root.go +++ b/cmd/dmsg-server/commands/root.go @@ -31,14 +31,19 @@ var rootCmd = &cobra.Command{ Short: "Dmsg Server for Skywire.", PreRunE: func(cmd *cobra.Command, args []string) error { return sf.Check() }, Run: func(_ *cobra.Command, args []string) { - log := sf.Logger() + if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil { + log.Printf("Failed to output build info: %v", err) + } - // Workaround for Discord logger hook. Actually, it's Info. - log.Error(discord.StartLogMessage) - defer log.Error(discord.StopLogMessage) + log := sf.Logger() - if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil { - log.WithError(err).Warn("Failed to output build info.") + if discordWebhookURL := discord.GetWebhookURLFromEnv(); discordWebhookURL != "" { + // Workaround for Discord logger hook. Actually, it's Info. + log.Error(discord.StartLogMessage) + defer log.Error(discord.StopLogMessage) + } else { + log.Info(discord.StartLogMessage) + defer log.Info(discord.StopLogMessage) } var conf Config From 53ee4fb646707331a9f215f7f9901dfaa0491877 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 7 Oct 2020 22:24:48 +0300 Subject: [PATCH 4/4] Implement graceful shutdown for correct output of stopping logs --- cmd/dmsg-discovery/commands/root.go | 12 +++++++++++- cmd/dmsg-server/commands/root.go | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/dmsg-discovery/commands/root.go b/cmd/dmsg-discovery/commands/root.go index 71412ca02..7a78b1790 100644 --- a/cmd/dmsg-discovery/commands/root.go +++ b/cmd/dmsg-discovery/commands/root.go @@ -61,8 +61,18 @@ var rootCmd = &cobra.Command{ a := api.New(log, db, testMode) + ctx, cancel := cmdutil.SignalContext(context.Background(), log) + defer cancel() + log.WithField("addr", addr).Info("Serving discovery API...") - log.Fatal(http.ListenAndServe(addr, m.Handle(a))) + go func() { + if err := http.ListenAndServe(addr, m.Handle(a)); err != nil { + log.Errorf("ListenAndServe: %v", err) + cancel() + } + }() + + <-ctx.Done() }, } diff --git a/cmd/dmsg-server/commands/root.go b/cmd/dmsg-server/commands/root.go index fdbd87515..02b94fb42 100644 --- a/cmd/dmsg-server/commands/root.go +++ b/cmd/dmsg-server/commands/root.go @@ -1,6 +1,7 @@ package commands import ( + "context" "log" "net" "net/http" @@ -67,9 +68,17 @@ var rootCmd = &cobra.Command{ defer func() { log.WithError(srv.Close()).Info("Closed server.") }() - if err := srv.Serve(lis, conf.PublicAddress); err != nil { - log.Fatal(err) - } + ctx, cancel := cmdutil.SignalContext(context.Background(), log) + defer cancel() + + go func() { + if err := srv.Serve(lis, conf.PublicAddress); err != nil { + log.Errorf("Serve: %v", err) + cancel() + } + }() + + <-ctx.Done() }, }