Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Discord start/stop logs #15

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion cmd/dmsg-discovery/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"log"
"net/http"
"os"
Expand All @@ -13,6 +14,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"
Expand Down Expand Up @@ -44,14 +46,33 @@ var rootCmd = &cobra.Command{

log := sf.Logger()

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()

db := prepareDB(log)

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()
},
}

Expand Down
29 changes: 24 additions & 5 deletions cmd/dmsg-server/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"log"
"net"
"net/http"
Expand All @@ -15,6 +16,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"
)
Expand All @@ -30,10 +32,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) {
if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.Printf("Failed to output build info: %v", err)
}

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
Expand All @@ -57,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()
},
}

Expand Down
23 changes: 21 additions & 2 deletions discord/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import (
"github.com/sirupsen/logrus"
)

const webhookURLEnvName = "DISCORD_WEBHOOK_URL"

const (
loggedLevel = logrus.ErrorLevel
startStopLogLevel = logrus.InfoLevel
)

const (
webhookURLEnvName = "DISCORD_WEBHOOK_URL"
// StartLogMessage defines a message on binary starting.
StartLogMessage = "Starting"
// StopLogMessage defines a message on binary stopping.
StopLogMessage = "Stopping"
)

// Hook is a Discord logger hook.
Expand All @@ -32,7 +42,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,
Expand All @@ -47,6 +57,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)
}
Expand Down