mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
smtp: Use zerolog hooks for warns/errors expvars #90
This commit is contained in:
@@ -113,7 +113,9 @@ func (s *Session) String() string {
|
||||
* 5. Goto 2
|
||||
*/
|
||||
func (s *Server) startSession(id int, conn net.Conn) {
|
||||
logger := log.With().Str("module", "smtp").Str("remote", conn.RemoteAddr().String()).
|
||||
logger := log.Hook(logHook{}).With().
|
||||
Str("module", "smtp").
|
||||
Str("remote", conn.RemoteAddr().String()).
|
||||
Int("session", id).Logger()
|
||||
logger.Info().Msg("Starting SMTP session")
|
||||
expConnectsCurrent.Add(1)
|
||||
|
||||
@@ -36,6 +36,27 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
// Raw stat collectors
|
||||
expConnectsTotal = new(expvar.Int)
|
||||
expConnectsCurrent = new(expvar.Int)
|
||||
expReceivedTotal = new(expvar.Int)
|
||||
expErrorsTotal = new(expvar.Int)
|
||||
expWarnsTotal = new(expvar.Int)
|
||||
|
||||
// History of certain stats
|
||||
deliveredHist = list.New()
|
||||
connectsHist = list.New()
|
||||
errorsHist = list.New()
|
||||
warnsHist = list.New()
|
||||
|
||||
// History rendered as comma delim string
|
||||
expReceivedHist = new(expvar.String)
|
||||
expConnectsHist = new(expvar.String)
|
||||
expErrorsHist = new(expvar.String)
|
||||
expWarnsHist = new(expvar.String)
|
||||
)
|
||||
|
||||
// Server holds the configuration and state of our SMTP server
|
||||
type Server struct {
|
||||
// TODO(#91) Refactor config items out of this struct
|
||||
@@ -59,27 +80,6 @@ type Server struct {
|
||||
waitgroup *sync.WaitGroup // Waitgroup tracks individual sessions
|
||||
}
|
||||
|
||||
var (
|
||||
// Raw stat collectors
|
||||
expConnectsTotal = new(expvar.Int)
|
||||
expConnectsCurrent = new(expvar.Int)
|
||||
expReceivedTotal = new(expvar.Int)
|
||||
expErrorsTotal = new(expvar.Int)
|
||||
expWarnsTotal = new(expvar.Int)
|
||||
|
||||
// History of certain stats
|
||||
deliveredHist = list.New()
|
||||
connectsHist = list.New()
|
||||
errorsHist = list.New()
|
||||
warnsHist = list.New()
|
||||
|
||||
// History rendered as comma delim string
|
||||
expReceivedHist = new(expvar.String)
|
||||
expConnectsHist = new(expvar.String)
|
||||
expErrorsHist = new(expvar.String)
|
||||
expWarnsHist = new(expvar.String)
|
||||
)
|
||||
|
||||
// NewServer creates a new Server instance with the specificed config
|
||||
func NewServer(
|
||||
cfg config.SMTP,
|
||||
|
||||
15
pkg/server/smtp/loghook.go
Normal file
15
pkg/server/smtp/loghook.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package smtp
|
||||
|
||||
import "github.com/rs/zerolog"
|
||||
|
||||
type logHook struct{}
|
||||
|
||||
// Run implements a zerolog hook that updates the SMTP warning/error expvars.
|
||||
func (h logHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
||||
switch level {
|
||||
case zerolog.WarnLevel:
|
||||
expWarnsTotal.Add(1)
|
||||
case zerolog.ErrorLevel:
|
||||
expErrorsTotal.Add(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user